Fix Hydrawise data not refreshing (#105923)
Co-authored-by: Robert Resch <robert@resch.dev>pull/106870/head
parent
1cbacd13aa
commit
6f339541c6
|
@ -70,7 +70,7 @@ async def async_setup_entry(
|
|||
config_entry.entry_id
|
||||
]
|
||||
entities = []
|
||||
for controller in coordinator.data.controllers:
|
||||
for controller in coordinator.data.controllers.values():
|
||||
entities.append(
|
||||
HydrawiseBinarySensor(coordinator, BINARY_SENSOR_STATUS, controller)
|
||||
)
|
||||
|
|
|
@ -2,10 +2,11 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
|
||||
from pydrawise import HydrawiseBase
|
||||
from pydrawise.schema import User
|
||||
from pydrawise.schema import Controller, User, Zone
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
@ -13,9 +14,20 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|||
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."""
|
||||
|
||||
api: HydrawiseBase
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, api: HydrawiseBase, scan_interval: timedelta
|
||||
) -> None:
|
||||
|
@ -23,6 +35,13 @@ class HydrawiseDataUpdateCoordinator(DataUpdateCoordinator[User]):
|
|||
super().__init__(hass, LOGGER, name=DOMAIN, update_interval=scan_interval)
|
||||
self.api = api
|
||||
|
||||
async def _async_update_data(self) -> User:
|
||||
async def _async_update_data(self) -> HydrawiseData:
|
||||
"""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)
|
||||
|
|
|
@ -48,5 +48,8 @@ class HydrawiseEntity(CoordinatorEntity[HydrawiseDataUpdateCoordinator]):
|
|||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""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()
|
||||
super()._handle_coordinator_update()
|
||||
|
|
|
@ -76,7 +76,7 @@ async def async_setup_entry(
|
|||
]
|
||||
async_add_entities(
|
||||
HydrawiseSensor(coordinator, description, controller, zone)
|
||||
for controller in coordinator.data.controllers
|
||||
for controller in coordinator.data.controllers.values()
|
||||
for zone in controller.zones
|
||||
for description in SENSOR_TYPES
|
||||
)
|
||||
|
|
|
@ -81,7 +81,7 @@ async def async_setup_entry(
|
|||
]
|
||||
async_add_entities(
|
||||
HydrawiseSwitch(coordinator, description, controller, zone)
|
||||
for controller in coordinator.data.controllers
|
||||
for controller in coordinator.data.controllers.values()
|
||||
for zone in controller.zones
|
||||
for description in SWITCH_TYPES
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue