diff --git a/custom_components/reos_integration/__init__.py b/custom_components/reos_integration/__init__.py index a0db113..56f81e7 100755 --- a/custom_components/reos_integration/__init__.py +++ b/custom_components/reos_integration/__init__.py @@ -3,6 +3,7 @@ from homeassistant.core import HomeAssistant import logging from .reos_api import ReosApi +from .coordinator import ReosCoordinator from .const import DOMAIN _LOGGER = logging.getLogger(__name__) @@ -37,7 +38,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: api.set_update_token_callback(__update_entry) - hass.data[DOMAIN][username] = api + + hass.data[DOMAIN][username] = {} + + hass.data[DOMAIN][username]["api"] = api + + coordinator = ReosCoordinator(hass, api) + hass.data[DOMAIN][username]["coordinator"] = coordinator + await coordinator.async_config_entry_first_refresh() await hass.config_entries.async_forward_entry_setup(entry, "button") return True diff --git a/custom_components/reos_integration/button.py b/custom_components/reos_integration/button.py index f459496..605c874 100644 --- a/custom_components/reos_integration/button.py +++ b/custom_components/reos_integration/button.py @@ -1,30 +1,41 @@ from __future__ import annotations +import logging + from homeassistant.components.button import ButtonEntity from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant -import homeassistant.helpers.config_validation as cv +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN from .reos_api import ReosApi, ReosLockModel +_LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: - api: ReosApi = hass.data[DOMAIN][config_entry.data["username"]] + coordinator = hass.data[DOMAIN][config_entry.data["username"]]["coordinator"] - async_add_entities(ReosDoorButton(lock) for lock in await api.get_locks()) + async_add_entities(ReosDoorButton(lock, coordinator) for _, lock in coordinator.data.items()) -class ReosDoorButton(ButtonEntity): +class ReosDoorButton(CoordinatorEntity, ButtonEntity): - def __init__(self, model: ReosLockModel) -> None: + def __init__(self, model: ReosLockModel, coordinator) -> None: + super().__init__(coordinator, context=model.lock_id) self._model = model + self.idx = self._model.lock_id self._attr_unique_id = f"reos_lock_{self._model.lock_id}" self._attr_name = self._model.display self._attr_device_class = "door" self._attr_icon = "mdi:door" + @callback + def _handle_coordinator_update(self) -> None: + self._model = self.coordinator.data[self.idx] + _LOGGER.info(f"_handle_coordinator_update for {self.idx}") + self.async_write_ha_state() + def press(self, **kwargs) -> None: self.hass.create_task(self._model.open()) diff --git a/custom_components/reos_integration/coordinator.py b/custom_components/reos_integration/coordinator.py new file mode 100644 index 0000000..0329aac --- /dev/null +++ b/custom_components/reos_integration/coordinator.py @@ -0,0 +1,33 @@ +from datetime import timedelta +import logging + +import async_timeout + +from homeassistant.components.light import LightEntity +from homeassistant.core import callback +from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.helpers.update_coordinator import ( + CoordinatorEntity, + DataUpdateCoordinator, + UpdateFailed, +) + +from .const import DOMAIN +from .reos_api import ReosApi + +_LOGGER = logging.getLogger(__name__) + +class ReosCoordinator(DataUpdateCoordinator): + def __init__(self, hass, api: ReosApi): + super().__init__( + hass, + _LOGGER, + name="Reos Locks", + update_interval=timedelta(seconds=30), + ) + self.api = api + + async def _async_update_data(self): + _LOGGER.info("_async_update_data") + locks = await self.api.get_locks() + return {lock.lock_id: lock for lock in locks}