Skip to content

Commit

Permalink
Coordinator fixes (#4)
Browse files Browse the repository at this point in the history
- Set up coordinator in init (preparing for multiple platforms)
- Write state to HA after updating
- Support unloading integration
- Bump version to 0.0.6
  • Loading branch information
oyvindwe authored Jun 10, 2024
1 parent 82ba141 commit c1a8ae0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
20 changes: 13 additions & 7 deletions custom_components/connectlife/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions custom_components/connectlife/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/connectlife/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/connectlife/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
15 changes: 9 additions & 6 deletions custom_components/connectlife/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()

0 comments on commit c1a8ae0

Please sign in to comment.