2024-12-18 18:11:13 +00:00
|
|
|
"""Data update coordinator for Peblar EV chargers."""
|
|
|
|
|
2024-12-21 09:55:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
2024-12-18 18:11:13 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2024-12-21 13:23:16 +00:00
|
|
|
from peblar import (
|
|
|
|
Peblar,
|
|
|
|
PeblarApi,
|
|
|
|
PeblarError,
|
2024-12-21 19:16:18 +00:00
|
|
|
PeblarEVInterface,
|
2024-12-21 13:23:16 +00:00
|
|
|
PeblarMeter,
|
|
|
|
PeblarUserConfiguration,
|
|
|
|
PeblarVersions,
|
|
|
|
)
|
2024-12-18 18:11:13 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
2024-12-21 09:55:00 +00:00
|
|
|
from tests.components.peblar.conftest import PeblarSystemInformation
|
2024-12-18 18:11:13 +00:00
|
|
|
|
|
|
|
from .const import LOGGER
|
|
|
|
|
2024-12-21 09:55:00 +00:00
|
|
|
|
|
|
|
@dataclass(kw_only=True)
|
|
|
|
class PeblarRuntimeData:
|
|
|
|
"""Class to hold runtime data."""
|
|
|
|
|
2024-12-21 19:16:18 +00:00
|
|
|
data_coordinator: PeblarDataUpdateCoordinator
|
2024-12-21 13:23:16 +00:00
|
|
|
system_information: PeblarSystemInformation
|
|
|
|
user_configuraton_coordinator: PeblarUserConfigurationDataUpdateCoordinator
|
2024-12-21 09:55:00 +00:00
|
|
|
version_coordinator: PeblarVersionDataUpdateCoordinator
|
|
|
|
|
|
|
|
|
|
|
|
type PeblarConfigEntry = ConfigEntry[PeblarRuntimeData]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(kw_only=True, frozen=True)
|
|
|
|
class PeblarVersionInformation:
|
|
|
|
"""Class to hold version information."""
|
|
|
|
|
|
|
|
current: PeblarVersions
|
|
|
|
available: PeblarVersions
|
|
|
|
|
|
|
|
|
2024-12-21 19:16:18 +00:00
|
|
|
@dataclass(kw_only=True)
|
|
|
|
class PeblarData:
|
|
|
|
"""Class to hold active charging related information of Peblar.
|
|
|
|
|
|
|
|
This is data that needs to be polled and updated at a relatively high
|
|
|
|
frequency in order for this integration to function correctly.
|
|
|
|
All this data is updated at the same time by a single coordinator.
|
|
|
|
"""
|
|
|
|
|
|
|
|
ev: PeblarEVInterface
|
|
|
|
meter: PeblarMeter
|
|
|
|
|
|
|
|
|
2024-12-21 09:55:00 +00:00
|
|
|
class PeblarVersionDataUpdateCoordinator(
|
|
|
|
DataUpdateCoordinator[PeblarVersionInformation]
|
|
|
|
):
|
|
|
|
"""Class to manage fetching Peblar version information."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, hass: HomeAssistant, entry: PeblarConfigEntry, peblar: Peblar
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the coordinator."""
|
|
|
|
self.peblar = peblar
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
LOGGER,
|
|
|
|
config_entry=entry,
|
|
|
|
name=f"Peblar {entry.title} version",
|
|
|
|
update_interval=timedelta(hours=2),
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _async_update_data(self) -> PeblarVersionInformation:
|
|
|
|
"""Fetch data from the Peblar device."""
|
|
|
|
try:
|
|
|
|
return PeblarVersionInformation(
|
|
|
|
current=await self.peblar.current_versions(),
|
|
|
|
available=await self.peblar.available_versions(),
|
|
|
|
)
|
|
|
|
except PeblarError as err:
|
|
|
|
raise UpdateFailed(err) from err
|
2024-12-18 18:11:13 +00:00
|
|
|
|
|
|
|
|
2024-12-21 19:16:18 +00:00
|
|
|
class PeblarDataUpdateCoordinator(DataUpdateCoordinator[PeblarData]):
|
|
|
|
"""Class to manage fetching Peblar active data."""
|
2024-12-18 18:11:13 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, hass: HomeAssistant, entry: PeblarConfigEntry, api: PeblarApi
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the coordinator."""
|
|
|
|
self.api = api
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
LOGGER,
|
|
|
|
config_entry=entry,
|
|
|
|
name=f"Peblar {entry.title} meter",
|
|
|
|
update_interval=timedelta(seconds=10),
|
|
|
|
)
|
|
|
|
|
2024-12-21 19:16:18 +00:00
|
|
|
async def _async_update_data(self) -> PeblarData:
|
2024-12-18 18:11:13 +00:00
|
|
|
"""Fetch data from the Peblar device."""
|
|
|
|
try:
|
2024-12-21 19:16:18 +00:00
|
|
|
return PeblarData(
|
|
|
|
ev=await self.api.ev_interface(),
|
|
|
|
meter=await self.api.meter(),
|
|
|
|
)
|
2024-12-18 18:11:13 +00:00
|
|
|
except PeblarError as err:
|
|
|
|
raise UpdateFailed(err) from err
|
2024-12-21 13:23:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PeblarUserConfigurationDataUpdateCoordinator(
|
|
|
|
DataUpdateCoordinator[PeblarUserConfiguration]
|
|
|
|
):
|
|
|
|
"""Class to manage fetching Peblar user configuration data."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, hass: HomeAssistant, entry: PeblarConfigEntry, peblar: Peblar
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the coordinator."""
|
|
|
|
self.peblar = peblar
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
LOGGER,
|
|
|
|
config_entry=entry,
|
|
|
|
name=f"Peblar {entry.title} user configuration",
|
|
|
|
update_interval=timedelta(minutes=5),
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _async_update_data(self) -> PeblarUserConfiguration:
|
|
|
|
"""Fetch data from the Peblar device."""
|
|
|
|
try:
|
|
|
|
return await self.peblar.user_configuration()
|
|
|
|
except PeblarError as err:
|
|
|
|
raise UpdateFailed(err) from err
|