Skip to content

Commit

Permalink
Use runtime_data in elmax (#136803)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jan 29, 2025
1 parent 5038847 commit 0c6c9e0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 48 deletions.
30 changes: 8 additions & 22 deletions homeassistant/components/elmax/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

from __future__ import annotations

from datetime import timedelta
import logging

from elmax_api.exceptions import ElmaxBadLoginError
from elmax_api.http import Elmax, ElmaxLocal, GenericElmax
from elmax_api.model.panel import PanelEntry

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
Expand All @@ -27,17 +23,13 @@
CONF_ELMAX_PANEL_PIN,
CONF_ELMAX_PASSWORD,
CONF_ELMAX_USERNAME,
DOMAIN,
ELMAX_PLATFORMS,
POLLING_SECONDS,
)
from .coordinator import ElmaxCoordinator

_LOGGER = logging.getLogger(__name__)
from .coordinator import ElmaxConfigEntry, ElmaxCoordinator


async def _load_elmax_panel_client(
entry: ConfigEntry,
entry: ElmaxConfigEntry,
) -> tuple[GenericElmax, PanelEntry]:
# Connection mode was not present in initial version, default to cloud if not set
mode = entry.data.get(CONF_ELMAX_MODE, CONF_ELMAX_MODE_CLOUD)
Expand Down Expand Up @@ -87,7 +79,7 @@ async def _check_cloud_panel_status(client: Elmax, panel_id: str) -> PanelEntry:
return panel


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: ElmaxConfigEntry) -> bool:
"""Set up elmax-cloud from a config entry."""
try:
client, panel = await _load_elmax_panel_client(entry)
Expand All @@ -98,11 +90,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# if there is something wrong with user credentials
coordinator = ElmaxCoordinator(
hass=hass,
logger=_LOGGER,
entry=entry,
elmax_api_client=client,
panel=panel,
name=f"Elmax Cloud {entry.entry_id}",
update_interval=timedelta(seconds=POLLING_SECONDS),
)

async def _async_on_hass_stop(_: Event) -> None:
Expand All @@ -117,7 +107,7 @@ async def _async_on_hass_stop(_: Event) -> None:
await coordinator.async_config_entry_first_refresh()

# Store a global reference to the coordinator for later use
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
entry.runtime_data = coordinator

entry.async_on_unload(entry.add_update_listener(async_reload_entry))

Expand All @@ -126,15 +116,11 @@ async def _async_on_hass_stop(_: Event) -> None:
return True


async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
async def async_reload_entry(hass: HomeAssistant, entry: ElmaxConfigEntry) -> None:
"""Handle an options update."""
await hass.config_entries.async_reload(entry.entry_id)


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ElmaxConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, ELMAX_PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok
return await hass.config_entries.async_unload_platforms(entry, ELMAX_PLATFORMS)
7 changes: 3 additions & 4 deletions homeassistant/components/elmax/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@
AlarmControlPanelState,
CodeFormat,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError, InvalidStateError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import ElmaxCoordinator
from .coordinator import ElmaxConfigEntry
from .entity import ElmaxEntity


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: ElmaxConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Elmax area platform."""
coordinator: ElmaxCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
known_devices = set()

def _discover_new_devices():
Expand Down
8 changes: 3 additions & 5 deletions homeassistant/components/elmax/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import ElmaxCoordinator
from .coordinator import ElmaxConfigEntry
from .entity import ElmaxEntity


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: ElmaxConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Elmax sensor platform."""
coordinator: ElmaxCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
known_devices = set()

def _discover_new_devices():
Expand Down
19 changes: 13 additions & 6 deletions homeassistant/components/elmax/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from asyncio import timeout
from datetime import timedelta
from logging import Logger
import logging

from elmax_api.exceptions import (
ElmaxApiError,
Expand All @@ -22,11 +22,16 @@
from elmax_api.push.push import PushNotificationHandler
from httpx import ConnectError, ConnectTimeout

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, HomeAssistantError
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DEFAULT_TIMEOUT
from .const import DEFAULT_TIMEOUT, POLLING_SECONDS

_LOGGER = logging.getLogger(__name__)

type ElmaxConfigEntry = ConfigEntry[ElmaxCoordinator]


class ElmaxCoordinator(DataUpdateCoordinator[PanelStatus]):
Expand All @@ -37,19 +42,21 @@ class ElmaxCoordinator(DataUpdateCoordinator[PanelStatus]):
def __init__(
self,
hass: HomeAssistant,
logger: Logger,
entry: ElmaxConfigEntry,
elmax_api_client: GenericElmax,
panel: PanelEntry,
name: str,
update_interval: timedelta,
) -> None:
"""Instantiate the object."""
self._client = elmax_api_client
self._panel_entry = panel
self._state_by_endpoint = {}
self._push_notification_handler = None
super().__init__(
hass=hass, logger=logger, name=name, update_interval=update_interval
hass=hass,
config_entry=entry,
logger=_LOGGER,
name=f"Elmax Cloud {entry.entry_id}",
update_interval=timedelta(seconds=POLLING_SECONDS),
)

@property
Expand Down
8 changes: 3 additions & 5 deletions homeassistant/components/elmax/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
from elmax_api.model.cover_status import CoverStatus

from homeassistant.components.cover import CoverEntity, CoverEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import ElmaxCoordinator
from .coordinator import ElmaxConfigEntry
from .entity import ElmaxEntity

_LOGGER = logging.getLogger(__name__)
Expand All @@ -28,11 +26,11 @@

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: ElmaxConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Elmax cover platform."""
coordinator: ElmaxCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
# Add the cover feature only if supported by the current panel.
if coordinator.data is None or not coordinator.data.cover_feature:
return
Expand Down
8 changes: 3 additions & 5 deletions homeassistant/components/elmax/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@
from elmax_api.model.panel import PanelStatus

from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import ElmaxCoordinator
from .coordinator import ElmaxConfigEntry
from .entity import ElmaxEntity

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: ElmaxConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Elmax switch platform."""
coordinator: ElmaxCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
known_devices = set()

def _discover_new_devices():
Expand Down
2 changes: 1 addition & 1 deletion tests/components/elmax/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from syrupy import SnapshotAssertion

from homeassistant.components.elmax import POLLING_SECONDS
from homeassistant.components.elmax.const import POLLING_SECONDS
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
Expand Down

0 comments on commit 0c6c9e0

Please sign in to comment.