Skip to content

Commit

Permalink
Simplify again
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl committed Jan 7, 2024
1 parent ebed22e commit 1e25b45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
26 changes: 18 additions & 8 deletions custom_components/sagemcom_fast/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The Sagemcom integration."""
from __future__ import annotations

from dataclasses import dataclass
from datetime import timedelta

from aiohttp.client_exceptions import ClientError
Expand All @@ -26,6 +27,7 @@
MaximumSessionCountException,
UnauthorizedException,
)
from sagemcom_api.models import DeviceInfo as GatewayDeviceInfo

from .const import (
CONF_ENCRYPTION_METHOD,
Expand All @@ -39,6 +41,14 @@
SERVICE_REBOOT = "reboot"


@dataclass
class HomeAssistantSagemcomFastData:
"""Nest Protect data stored in the Home Assistant data object."""

coordinator: SagemcomDataUpdateCoordinator
gateway: GatewayDeviceInfo | None


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Set up Sagemcom from a config entry."""

Expand Down Expand Up @@ -89,14 +99,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:

await coordinator.async_config_entry_first_refresh()

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
"coordinator": coordinator,
}

entry.async_on_unload(entry.add_update_listener(async_reload_entry))

# Many users face issues while retrieving Device Info
# So don't make this fail the start-up
gateway = None

try:
gateway = await client.get_device_info()

Expand All @@ -117,8 +123,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
finally:
await client.logout()

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = HomeAssistantSagemcomFastData(
coordinator=coordinator, gateway=gateway
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

entry.async_on_unload(entry.add_update_listener(async_reload_entry))

# Handle gateway device services
# TODO move to button entity
async def async_command_reboot(call: ServiceCall) -> None:
Expand All @@ -134,9 +146,7 @@ async def async_command_reboot(call: ServiceCall) -> None:

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][entry.entry_id]["update_listener"]()
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok
Expand Down
16 changes: 11 additions & 5 deletions custom_components/sagemcom_fast/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import async_timeout
from homeassistant.components.device_tracker import SourceType
from homeassistant.components.device_tracker.config_entry import ScannerEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import (
Expand All @@ -17,17 +19,21 @@
from sagemcom_api.client import SagemcomClient
from sagemcom_api.models import Device

from . import HomeAssistantSagemcomFastData
from .const import DOMAIN


async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up from config entry."""

coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
data: HomeAssistantSagemcomFastData = hass.data[DOMAIN][entry.entry_id]

async_add_entities(
SagemcomScannerEntity(coordinator, idx)
for idx, device in coordinator.data.items()
SagemcomScannerEntity(data.coordinator, idx)
for idx, device in data.coordinator.data.items()
)


Expand Down

0 comments on commit 1e25b45

Please sign in to comment.