mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Improve coordinator in Ondilo ico (#116596)
* Improve coordinator in Ondilo ico * Improve coordinator in Ondilo ico
This commit is contained in:
parent
1fc8fdf9ff
commit
a2bd045c9d
@ -939,6 +939,7 @@ omit =
|
|||||||
homeassistant/components/omnilogic/switch.py
|
homeassistant/components/omnilogic/switch.py
|
||||||
homeassistant/components/ondilo_ico/__init__.py
|
homeassistant/components/ondilo_ico/__init__.py
|
||||||
homeassistant/components/ondilo_ico/api.py
|
homeassistant/components/ondilo_ico/api.py
|
||||||
|
homeassistant/components/ondilo_ico/coordinator.py
|
||||||
homeassistant/components/ondilo_ico/sensor.py
|
homeassistant/components/ondilo_ico/sensor.py
|
||||||
homeassistant/components/onkyo/media_player.py
|
homeassistant/components/onkyo/media_player.py
|
||||||
homeassistant/components/onvif/__init__.py
|
homeassistant/components/onvif/__init__.py
|
||||||
|
@ -7,6 +7,7 @@ from homeassistant.helpers import config_entry_oauth2_flow
|
|||||||
|
|
||||||
from . import api, config_flow
|
from . import api, config_flow
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import OndiloIcoCoordinator
|
||||||
from .oauth_impl import OndiloOauth2Implementation
|
from .oauth_impl import OndiloOauth2Implementation
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
@ -26,8 +27,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
coordinator = OndiloIcoCoordinator(
|
||||||
hass.data[DOMAIN][entry.entry_id] = api.OndiloClient(hass, entry, implementation)
|
hass, api.OndiloClient(hass, entry, implementation)
|
||||||
|
)
|
||||||
|
|
||||||
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
|
37
homeassistant/components/ondilo_ico/coordinator.py
Normal file
37
homeassistant/components/ondilo_ico/coordinator.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
"""Define an object to coordinate fetching Ondilo ICO data."""
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from ondilo import OndiloError
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
|
from . import DOMAIN
|
||||||
|
from .api import OndiloClient
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class OndiloIcoCoordinator(DataUpdateCoordinator[list[dict[str, Any]]]):
|
||||||
|
"""Class to manage fetching Ondilo ICO data from API."""
|
||||||
|
|
||||||
|
def __init__(self, hass: HomeAssistant, api: OndiloClient) -> None:
|
||||||
|
"""Initialize."""
|
||||||
|
super().__init__(
|
||||||
|
hass,
|
||||||
|
logger=_LOGGER,
|
||||||
|
name=DOMAIN,
|
||||||
|
update_interval=timedelta(minutes=5),
|
||||||
|
)
|
||||||
|
self.api = api
|
||||||
|
|
||||||
|
async def _async_update_data(self) -> list[dict[str, Any]]:
|
||||||
|
"""Fetch data from API endpoint."""
|
||||||
|
try:
|
||||||
|
return await self.hass.async_add_executor_job(self.api.get_all_pools_data)
|
||||||
|
|
||||||
|
except OndiloError as err:
|
||||||
|
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
@ -2,12 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
|
||||||
import logging
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from ondilo import OndiloError
|
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
@ -24,14 +18,10 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
CoordinatorEntity,
|
|
||||||
DataUpdateCoordinator,
|
|
||||||
UpdateFailed,
|
|
||||||
)
|
|
||||||
|
|
||||||
from .api import OndiloClient
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import OndiloIcoCoordinator
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
@ -78,66 +68,30 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(minutes=5)
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Ondilo ICO sensors."""
|
"""Set up the Ondilo ICO sensors."""
|
||||||
|
|
||||||
api: OndiloClient = hass.data[DOMAIN][entry.entry_id]
|
coordinator: OndiloIcoCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async def async_update_data() -> list[dict[str, Any]]:
|
async_add_entities(
|
||||||
"""Fetch data from API endpoint.
|
OndiloICO(coordinator, poolidx, description)
|
||||||
|
for poolidx, pool in enumerate(coordinator.data)
|
||||||
This is the place to pre-process the data to lookup tables
|
for sensor in pool["sensors"]
|
||||||
so entities can quickly look up their data.
|
for description in SENSOR_TYPES
|
||||||
"""
|
if description.key == sensor["data_type"]
|
||||||
try:
|
|
||||||
return await hass.async_add_executor_job(api.get_all_pools_data)
|
|
||||||
|
|
||||||
except OndiloError as err:
|
|
||||||
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
|
||||||
|
|
||||||
coordinator = DataUpdateCoordinator(
|
|
||||||
hass,
|
|
||||||
_LOGGER,
|
|
||||||
# Name of the data. For logging purposes.
|
|
||||||
name="sensor",
|
|
||||||
update_method=async_update_data,
|
|
||||||
# Polling interval. Will only be polled if there are subscribers.
|
|
||||||
update_interval=SCAN_INTERVAL,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fetch initial data so we have data when entities subscribe
|
|
||||||
await coordinator.async_refresh()
|
|
||||||
|
|
||||||
entities = []
|
class OndiloICO(CoordinatorEntity[OndiloIcoCoordinator], SensorEntity):
|
||||||
for poolidx, pool in enumerate(coordinator.data):
|
|
||||||
entities.extend(
|
|
||||||
[
|
|
||||||
OndiloICO(coordinator, poolidx, description)
|
|
||||||
for sensor in pool["sensors"]
|
|
||||||
for description in SENSOR_TYPES
|
|
||||||
if description.key == sensor["data_type"]
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(entities)
|
|
||||||
|
|
||||||
|
|
||||||
class OndiloICO(
|
|
||||||
CoordinatorEntity[DataUpdateCoordinator[list[dict[str, Any]]]], SensorEntity
|
|
||||||
):
|
|
||||||
"""Representation of a Sensor."""
|
"""Representation of a Sensor."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator[list[dict[str, Any]]],
|
coordinator: OndiloIcoCoordinator,
|
||||||
poolidx: int,
|
poolidx: int,
|
||||||
description: SensorEntityDescription,
|
description: SensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user