2022-01-11 00:23:31 +00:00
|
|
|
"""Update coordinator for HomeWizard."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
from homewizard_energy import HomeWizardEnergy
|
|
|
|
from homewizard_energy.errors import DisabledError
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
2022-01-31 11:49:18 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2022-01-11 00:23:31 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
|
|
|
|
from .const import DOMAIN, UPDATE_INTERVAL, DeviceResponseEntry
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-01-21 09:44:56 +00:00
|
|
|
class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]):
|
2022-01-11 00:23:31 +00:00
|
|
|
"""Gather data for the energy device."""
|
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
api: HomeWizardEnergy
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
host: str,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize Update Coordinator."""
|
|
|
|
|
|
|
|
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
|
2022-05-25 07:05:11 +00:00
|
|
|
self.api = HomeWizardEnergy(host, clientsession=async_get_clientsession(hass))
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
async def _async_update_data(self) -> DeviceResponseEntry:
|
|
|
|
"""Fetch all device and sensor data from api."""
|
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
# Update all properties
|
|
|
|
try:
|
2022-01-11 00:23:31 +00:00
|
|
|
data: DeviceResponseEntry = {
|
2022-05-25 07:05:11 +00:00
|
|
|
"device": await self.api.device(),
|
|
|
|
"data": await self.api.data(),
|
|
|
|
"state": await self.api.state(),
|
2022-01-11 00:23:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
except DisabledError as ex:
|
|
|
|
raise UpdateFailed("API disabled, API must be enabled in the app") from ex
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
return data
|