Files
core/homeassistant/components/uptimerobot/coordinator.py
2025-10-20 20:42:15 +02:00

81 lines
2.7 KiB
Python

"""DataUpdateCoordinator for the uptimerobot integration."""
from __future__ import annotations
from typing import TYPE_CHECKING
from pyuptimerobot import (
UptimeRobot,
UptimeRobotAuthenticationException,
UptimeRobotException,
UptimeRobotMonitor,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import API_ATTR_OK, COORDINATOR_UPDATE_INTERVAL, DOMAIN, LOGGER
type UptimeRobotConfigEntry = ConfigEntry[UptimeRobotDataUpdateCoordinator]
class UptimeRobotDataUpdateCoordinator(DataUpdateCoordinator[list[UptimeRobotMonitor]]):
"""Data update coordinator for UptimeRobot."""
config_entry: UptimeRobotConfigEntry
def __init__(
self,
hass: HomeAssistant,
config_entry: UptimeRobotConfigEntry,
api: UptimeRobot,
) -> None:
"""Initialize coordinator."""
super().__init__(
hass,
LOGGER,
config_entry=config_entry,
name=DOMAIN,
update_interval=COORDINATOR_UPDATE_INTERVAL,
)
self.api = api
async def _async_update_data(self) -> list[UptimeRobotMonitor]:
"""Update data."""
try:
response = await self.api.async_get_monitors()
except UptimeRobotAuthenticationException as exception:
raise ConfigEntryAuthFailed(exception) from exception
except UptimeRobotException as exception:
raise UpdateFailed(exception) from exception
if response.status != API_ATTR_OK:
raise UpdateFailed(
response.error.message if response.error else "Unknown error"
)
if TYPE_CHECKING:
assert isinstance(response.data, list)
monitors: list[UptimeRobotMonitor] = response.data
current_monitors = (
{str(monitor.id) for monitor in self.data} if self.data else set()
)
new_monitors = {str(monitor.id) for monitor in monitors}
if stale_monitors := current_monitors - new_monitors:
for monitor_id in stale_monitors:
device_registry = dr.async_get(self.hass)
if device := device_registry.async_get_device(
identifiers={(DOMAIN, monitor_id)}
):
device_registry.async_update_device(
device_id=device.id,
remove_config_entry_id=self.config_entry.entry_id,
)
return monitors