Fix Hydrawise data not refreshing (#105923)

Co-authored-by: Robert Resch <robert@resch.dev>
pull/106870/head
David Knowles 2024-01-02 06:46:39 -05:00 committed by GitHub
parent 1cbacd13aa
commit 6f339541c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 7 deletions

View File

@ -70,7 +70,7 @@ async def async_setup_entry(
config_entry.entry_id config_entry.entry_id
] ]
entities = [] entities = []
for controller in coordinator.data.controllers: for controller in coordinator.data.controllers.values():
entities.append( entities.append(
HydrawiseBinarySensor(coordinator, BINARY_SENSOR_STATUS, controller) HydrawiseBinarySensor(coordinator, BINARY_SENSOR_STATUS, controller)
) )

View File

@ -2,10 +2,11 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from pydrawise import HydrawiseBase from pydrawise import HydrawiseBase
from pydrawise.schema import User from pydrawise.schema import Controller, User, Zone
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
@ -13,9 +14,20 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DOMAIN, LOGGER from .const import DOMAIN, LOGGER
class HydrawiseDataUpdateCoordinator(DataUpdateCoordinator[User]): @dataclass
class HydrawiseData:
"""Container for data fetched from the Hydrawise API."""
user: User
controllers: dict[int, Controller]
zones: dict[int, Zone]
class HydrawiseDataUpdateCoordinator(DataUpdateCoordinator[HydrawiseData]):
"""The Hydrawise Data Update Coordinator.""" """The Hydrawise Data Update Coordinator."""
api: HydrawiseBase
def __init__( def __init__(
self, hass: HomeAssistant, api: HydrawiseBase, scan_interval: timedelta self, hass: HomeAssistant, api: HydrawiseBase, scan_interval: timedelta
) -> None: ) -> None:
@ -23,6 +35,13 @@ class HydrawiseDataUpdateCoordinator(DataUpdateCoordinator[User]):
super().__init__(hass, LOGGER, name=DOMAIN, update_interval=scan_interval) super().__init__(hass, LOGGER, name=DOMAIN, update_interval=scan_interval)
self.api = api self.api = api
async def _async_update_data(self) -> User: async def _async_update_data(self) -> HydrawiseData:
"""Fetch the latest data from Hydrawise.""" """Fetch the latest data from Hydrawise."""
return await self.api.get_user() user = await self.api.get_user()
controllers = {}
zones = {}
for controller in user.controllers:
controllers[controller.id] = controller
for zone in controller.zones:
zones[zone.id] = zone
return HydrawiseData(user=user, controllers=controllers, zones=zones)

View File

@ -48,5 +48,8 @@ class HydrawiseEntity(CoordinatorEntity[HydrawiseDataUpdateCoordinator]):
@callback @callback
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None:
"""Get the latest data and updates the state.""" """Get the latest data and updates the state."""
self.controller = self.coordinator.data.controllers[self.controller.id]
if self.zone:
self.zone = self.coordinator.data.zones[self.zone.id]
self._update_attrs() self._update_attrs()
super()._handle_coordinator_update() super()._handle_coordinator_update()

View File

@ -76,7 +76,7 @@ async def async_setup_entry(
] ]
async_add_entities( async_add_entities(
HydrawiseSensor(coordinator, description, controller, zone) HydrawiseSensor(coordinator, description, controller, zone)
for controller in coordinator.data.controllers for controller in coordinator.data.controllers.values()
for zone in controller.zones for zone in controller.zones
for description in SENSOR_TYPES for description in SENSOR_TYPES
) )

View File

@ -81,7 +81,7 @@ async def async_setup_entry(
] ]
async_add_entities( async_add_entities(
HydrawiseSwitch(coordinator, description, controller, zone) HydrawiseSwitch(coordinator, description, controller, zone)
for controller in coordinator.data.controllers for controller in coordinator.data.controllers.values()
for zone in controller.zones for zone in controller.zones
for description in SWITCH_TYPES for description in SWITCH_TYPES
) )