From c1a8ae08a25391fc002e7eb41a68e577eb677323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Matheson=20Wergeland?= Date: Mon, 10 Jun 2024 21:43:27 +0200 Subject: [PATCH] Coordinator fixes (#4) - Set up coordinator in init (preparing for multiple platforms) - Write state to HA after updating - Support unloading integration - Bump version to 0.0.6 --- custom_components/connectlife/__init__.py | 20 +++++++++++++------- custom_components/connectlife/coordinator.py | 6 ++++-- custom_components/connectlife/entity.py | 2 +- custom_components/connectlife/manifest.json | 2 +- custom_components/connectlife/sensor.py | 15 +++++++++------ 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/custom_components/connectlife/__init__.py b/custom_components/connectlife/__init__.py index 7d63551..ca1df54 100644 --- a/custom_components/connectlife/__init__.py +++ b/custom_components/connectlife/__init__.py @@ -7,25 +7,31 @@ from homeassistant.core import HomeAssistant from connectlife.api import ConnectLifeApi - from .const import DOMAIN +from .coordinator import ConnectLifeCoordinator PLATFORMS: list[Platform] = [Platform.SENSOR] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up ConnectLife from a config entry.""" - hass.data.setdefault(DOMAIN, {}) - # TODO 1. Create API instance - # TODO 2. Validate the API connection (and authentication) - # TODO 3. Store an API object for your platforms to access - # hass.data[DOMAIN][entry.entry_id] = MyApi(...) api = ConnectLifeApi(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]) await api.login() - hass.data[DOMAIN][entry.entry_id] = api + coordinator = ConnectLifeCoordinator(hass, api) + await coordinator.async_config_entry_first_refresh() + hass.data[DOMAIN][entry.entry_id] = coordinator + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) 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) + + return unload_ok diff --git a/custom_components/connectlife/coordinator.py b/custom_components/connectlife/coordinator.py index 8fe0f45..5cb93ff 100644 --- a/custom_components/connectlife/coordinator.py +++ b/custom_components/connectlife/coordinator.py @@ -7,6 +7,8 @@ from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from .const import DOMAIN + _LOGGER = logging.getLogger(__name__) class ConnectLifeCoordinator(DataUpdateCoordinator): @@ -16,13 +18,13 @@ class ConnectLifeCoordinator(DataUpdateCoordinator): def __init__(self, hass, api: ConnectLifeApi): """Initialize coordinator.""" + self.api = api super().__init__( hass, _LOGGER, - name="ConnectLife", + name=DOMAIN, update_interval=timedelta(seconds=60), ) - self.api = api async def _async_update_data(self): """Fetch data from API endpoint.""" diff --git a/custom_components/connectlife/entity.py b/custom_components/connectlife/entity.py index 3739fcf..5ebbb12 100644 --- a/custom_components/connectlife/entity.py +++ b/custom_components/connectlife/entity.py @@ -9,7 +9,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity -class ConnectLifeEntity(CoordinatorEntity): +class ConnectLifeEntity(CoordinatorEntity[ConnectLifeCoordinator]): """Generic ConnectLife entity (base class).""" def __init__(self, coordinator: ConnectLifeCoordinator, appliance: ConnectLifeAppliance): diff --git a/custom_components/connectlife/manifest.json b/custom_components/connectlife/manifest.json index 1404462..56ffa73 100644 --- a/custom_components/connectlife/manifest.json +++ b/custom_components/connectlife/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://github.com/oyvindwe/connectlife-ha", "iot_class": "cloud_polling", "requirements": ["connectlife==0.0.5"], - "version": "0.0.5" + "version": "0.0.6" } diff --git a/custom_components/connectlife/sensor.py b/custom_components/connectlife/sensor.py index c58be5e..a8da67b 100644 --- a/custom_components/connectlife/sensor.py +++ b/custom_components/connectlife/sensor.py @@ -24,9 +24,7 @@ async def async_setup_entry( ) -> None: """Set up ConnectLife sensors.""" - api = hass.data[DOMAIN][config_entry.entry_id] - coordinator = ConnectLifeCoordinator(hass, api) - await coordinator.async_refresh() + coordinator = hass.data[DOMAIN][config_entry.entry_id] for appliance in coordinator.appliances.values(): async_add_entities( @@ -44,10 +42,15 @@ def __init__(self, coordinator: ConnectLifeCoordinator, appliance: ConnectLifeAp description = status.replace("_", " ") self._attr_name = f"{appliance._device_nickname} {description}" self._attr_unique_id = f"{appliance.device_id}-{status}" - self._handle_coordinator_update() + self.update_state() @callback - def _handle_coordinator_update(self) -> None: - """Handle updated data from the coordinator.""" + def update_state(self): self._attr_native_value = self.coordinator.appliances[self.device_id].status_list[self.status] self._attr_available = self.coordinator.appliances[self.device_id]._offline_state == 1 + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self.update_state() + self.async_write_ha_state()