Skip to content

Commit

Permalink
Add reboot button as entity
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl committed Jan 7, 2024
1 parent cf0f3b0 commit ed664ec
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
23 changes: 4 additions & 19 deletions custom_components/sagemcom_fast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client, service
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from sagemcom_api.client import SagemcomClient
from sagemcom_api.enums import EncryptionMethod
Expand All @@ -37,8 +37,6 @@
)
from .coordinator import SagemcomDataUpdateCoordinator

SERVICE_REBOOT = "reboot"


@dataclass
class HomeAssistantSagemcomFastData:
Expand All @@ -50,7 +48,6 @@ class HomeAssistantSagemcomFastData:

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up Sagemcom F@st from a config entry."""

host = entry.data[CONF_HOST]
username = entry.data[CONF_USERNAME]
password = entry.data[CONF_PASSWORD]
Expand Down Expand Up @@ -121,26 +118,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
configuration_url=f"{'https' if ssl else 'http'}://{host}",
)

# Register components
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

# Handle gateway device services
async def async_command_reboot(call):
"""Handle reboot service call."""
await client.reboot()

service.async_register_admin_service(
hass, DOMAIN, SERVICE_REBOOT, async_command_reboot
)

entry.async_on_unload(entry.add_update_listener(update_listener))

return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""

if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)

Expand All @@ -150,9 +135,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
"""Update when entry options update."""
if entry.options[CONF_SCAN_INTERVAL]:
coordinator = hass.data[DOMAIN][entry.entry_id]["coordinator"]
coordinator.update_interval = timedelta(
data: HomeAssistantSagemcomFastData = hass.data[DOMAIN][entry.entry_id]
data.coordinator.update_interval = timedelta(
seconds=entry.options[CONF_SCAN_INTERVAL]
)

await coordinator.async_refresh()
await data.coordinator.async_refresh()
53 changes: 53 additions & 0 deletions custom_components/sagemcom_fast/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Support for Sagencom F@st buttons."""
from __future__ import annotations

from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from sagemcom_api.client import SagemcomClient
from sagemcom_api.models import DeviceInfo as GatewayDeviceInfo

from . import HomeAssistantSagemcomFastData
from .const import DOMAIN


async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Sagemcom F@st button from a config entry."""
data: HomeAssistantSagemcomFastData = hass.data[DOMAIN][entry.entry_id]
entities: list[ButtonEntity] = []
entities.append(SagemcomFastRebootButton(data.gateway, data.coordinator.client))

async_add_entities(entities)


class SagemcomFastRebootButton(ButtonEntity):
"""Representation of an Sagemcom F@st Button."""

_attr_has_entity_name = True
_attr_name = "Reboot"
_attr_device_class = ButtonDeviceClass.RESTART
_attr_entity_category = EntityCategory.CONFIG

def __init__(self, gateway: GatewayDeviceInfo, client: SagemcomClient) -> None:
"""Initialize the button."""
self.gateway = gateway
self.client = client
self._attr_unique_id = f"{self.gateway.serial_number}_reboot"

async def async_press(self) -> None:
"""Handle the button press."""
await self.client.reboot()

@property
def device_info(self) -> DeviceInfo:
"""Return device registry information for this entity."""
return DeviceInfo(
identifiers={(DOMAIN, self.gateway.serial_number)},
)
2 changes: 1 addition & 1 deletion custom_components/sagemcom_fast/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
MIN_SCAN_INTERVAL: Final = 10
DEFAULT_SCAN_INTERVAL: Final = 10

PLATFORMS: list[Platform] = [Platform.DEVICE_TRACKER]
PLATFORMS: list[Platform] = [Platform.DEVICE_TRACKER, Platform.BUTTON]
8 changes: 4 additions & 4 deletions custom_components/sagemcom_fast/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ def __init__(
)
self.data = {}
self.hosts: dict[str, Device] = {}
self._client = client
self.client = client

async def _async_update_data(self) -> dict[str, Device]:
"""Update hosts data."""
try:
async with async_timeout.timeout(10):
try:
await self._client.login()
hosts = await self._client.get_hosts(only_active=True)
await self.client.login()
hosts = await self.client.get_hosts(only_active=True)
finally:
await self._client.logout()
await self.client.logout()

"""Mark all device as non-active."""
for idx, host in self.hosts.items():
Expand Down
1 change: 0 additions & 1 deletion custom_components/sagemcom_fast/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up from config entry."""

data: HomeAssistantSagemcomFastData = hass.data[DOMAIN][config_entry.entry_id]

async_add_entities(
Expand Down

0 comments on commit ed664ec

Please sign in to comment.