2022-01-11 00:23:31 +00:00
|
|
|
"""Update coordinator for HomeWizard."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-12-16 15:53:54 +00:00
|
|
|
from datetime import timedelta
|
2022-01-11 00:23:31 +00:00
|
|
|
import logging
|
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
from homewizard_energy import HomeWizardEnergy
|
2022-06-20 08:30:57 +00:00
|
|
|
from homewizard_energy.errors import DisabledError, RequestError
|
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-11-29 13:30:22 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
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-12-16 15:53:54 +00:00
|
|
|
MAX_UPDATE_INTERVAL = timedelta(minutes=30)
|
|
|
|
|
2022-01-11 00:23:31 +00:00
|
|
|
|
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-12-16 15:53:54 +00:00
|
|
|
api_disabled: bool = False
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
2022-12-16 15:53:54 +00:00
|
|
|
entry_id: str,
|
2022-01-11 00:23:31 +00:00
|
|
|
host: str,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize Update Coordinator."""
|
|
|
|
|
|
|
|
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
|
2022-12-16 15:53:54 +00:00
|
|
|
self.entry_id = entry_id
|
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
|
|
|
|
2022-11-29 13:30:22 +00:00
|
|
|
@property
|
|
|
|
def device_info(self) -> DeviceInfo:
|
|
|
|
"""Return device_info."""
|
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self.data["device"].serial)},
|
|
|
|
)
|
|
|
|
|
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-11-27 19:26:15 +00:00
|
|
|
"system": None,
|
2022-01-11 00:23:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 19:26:15 +00:00
|
|
|
features = await self.api.features()
|
|
|
|
if features.has_system:
|
|
|
|
data["system"] = await self.api.system()
|
|
|
|
|
2022-06-20 08:30:57 +00:00
|
|
|
except RequestError as ex:
|
2022-12-16 15:53:54 +00:00
|
|
|
raise UpdateFailed(ex) from ex
|
2022-06-20 08:30:57 +00:00
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
except DisabledError as ex:
|
2022-12-16 15:53:54 +00:00
|
|
|
if not self.api_disabled:
|
|
|
|
self.api_disabled = True
|
|
|
|
|
|
|
|
# Do not reload when performing first refresh
|
|
|
|
if self.data is not None:
|
|
|
|
await self.hass.config_entries.async_reload(self.entry_id)
|
|
|
|
|
|
|
|
raise UpdateFailed(ex) from ex
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.api_disabled = False
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
return data
|