2022-02-06 18:03:50 +01:00
|
|
|
"""DataUpdateCoordinator for Plugwise."""
|
|
|
|
from datetime import timedelta
|
2022-10-21 16:47:04 +02:00
|
|
|
from typing import NamedTuple, cast
|
2022-02-06 18:03:50 +01:00
|
|
|
|
|
|
|
from plugwise import Smile
|
2022-10-21 16:47:04 +02:00
|
|
|
from plugwise.constants import DeviceData, GatewayData
|
2022-11-25 10:55:51 +01:00
|
|
|
from plugwise.exceptions import (
|
|
|
|
ConnectionFailedError,
|
|
|
|
InvalidAuthentication,
|
|
|
|
InvalidXMLError,
|
|
|
|
ResponseError,
|
|
|
|
UnsupportedDeviceError,
|
|
|
|
)
|
2022-02-06 18:03:50 +01:00
|
|
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
2022-02-15 01:47:39 +01:00
|
|
|
from homeassistant.helpers.debounce import Debouncer
|
2022-02-06 18:03:50 +01:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
|
2022-02-08 11:13:05 +01:00
|
|
|
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, LOGGER
|
2022-02-06 18:03:50 +01:00
|
|
|
|
|
|
|
|
2022-02-08 11:13:05 +01:00
|
|
|
class PlugwiseData(NamedTuple):
|
|
|
|
"""Plugwise data stored in the DataUpdateCoordinator."""
|
|
|
|
|
2022-10-21 16:47:04 +02:00
|
|
|
gateway: GatewayData
|
|
|
|
devices: dict[str, DeviceData]
|
2022-02-08 11:13:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PlugwiseDataUpdateCoordinator(DataUpdateCoordinator[PlugwiseData]):
|
2022-02-06 18:03:50 +01:00
|
|
|
"""Class to manage fetching Plugwise data from single endpoint."""
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant, api: Smile) -> None:
|
|
|
|
"""Initialize the coordinator."""
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
LOGGER,
|
|
|
|
name=api.smile_name or DOMAIN,
|
|
|
|
update_interval=DEFAULT_SCAN_INTERVAL.get(
|
|
|
|
str(api.smile_type), timedelta(seconds=60)
|
|
|
|
),
|
2022-02-15 01:47:39 +01:00
|
|
|
# Don't refresh immediately, give the device time to process
|
|
|
|
# the change in state before we query it.
|
|
|
|
request_refresh_debouncer=Debouncer(
|
|
|
|
hass,
|
|
|
|
LOGGER,
|
|
|
|
cooldown=1.5,
|
|
|
|
immediate=False,
|
|
|
|
),
|
2022-02-06 18:03:50 +01:00
|
|
|
)
|
|
|
|
self.api = api
|
|
|
|
|
2022-02-08 11:13:05 +01:00
|
|
|
async def _async_update_data(self) -> PlugwiseData:
|
2022-02-06 18:03:50 +01:00
|
|
|
"""Fetch data from Plugwise."""
|
|
|
|
try:
|
2022-02-08 11:13:05 +01:00
|
|
|
data = await self.api.async_update()
|
2022-11-25 10:55:51 +01:00
|
|
|
except InvalidAuthentication as err:
|
|
|
|
raise UpdateFailed("Authentication failed") from err
|
|
|
|
except (InvalidXMLError, ResponseError) as err:
|
2022-02-08 11:13:05 +01:00
|
|
|
raise UpdateFailed(
|
2022-11-25 10:55:51 +01:00
|
|
|
"Invalid XML data, or error indication received for the Plugwise Adam/Smile/Stretch"
|
2022-02-08 11:13:05 +01:00
|
|
|
) from err
|
2022-11-25 10:55:51 +01:00
|
|
|
except UnsupportedDeviceError as err:
|
|
|
|
raise UpdateFailed("Device with unsupported firmware") from err
|
|
|
|
except ConnectionFailedError as err:
|
|
|
|
raise UpdateFailed("Failed to connect") from err
|
2022-10-21 16:47:04 +02:00
|
|
|
return PlugwiseData(
|
|
|
|
gateway=cast(GatewayData, data[0]),
|
|
|
|
devices=cast(dict[str, DeviceData], data[1]),
|
|
|
|
)
|