Use runtime_data in elmax (#136803)

This commit is contained in:
epenet 2025-01-29 09:04:24 +01:00 committed by GitHub
parent 5038847d67
commit 0c6c9e0ae6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 34 additions and 48 deletions

View File

@ -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
@ -27,17 +23,13 @@ from .const import (
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)
@ -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)
@ -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:
@ -117,7 +107,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
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))
@ -126,15 +116,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
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)

View File

@ -13,23 +13,22 @@ from homeassistant.components.alarm_control_panel import (
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():

View File

@ -8,22 +8,20 @@ from homeassistant.components.binary_sensor import (
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():

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from asyncio import timeout
from datetime import timedelta
from logging import Logger
import logging
from elmax_api.exceptions import (
ElmaxApiError,
@ -22,11 +22,16 @@ from elmax_api.model.panel import PanelEntry, PanelStatus
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]):
@ -37,11 +42,9 @@ 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
@ -49,7 +52,11 @@ class ElmaxCoordinator(DataUpdateCoordinator[PanelStatus]):
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

View File

@ -9,12 +9,10 @@ from elmax_api.model.command import CoverCommand
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__)
@ -28,11 +26,11 @@ _COMMAND_BY_MOTION_STATUS = { # Maps the stop command to use for every cover mo
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

View File

@ -8,12 +8,10 @@ from elmax_api.model.command import SwitchCommand
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__)
@ -21,11 +19,11 @@ _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():

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
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