2022-12-22 21:00:17 +00:00
|
|
|
"""Update coordinators for rainbird."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-08-15 15:21:49 +00:00
|
|
|
import asyncio
|
2023-01-07 17:34:01 +00:00
|
|
|
from dataclasses import dataclass
|
2022-12-22 21:00:17 +00:00
|
|
|
import datetime
|
2023-09-26 00:27:38 +00:00
|
|
|
from functools import cached_property
|
2022-12-22 21:00:17 +00:00
|
|
|
import logging
|
|
|
|
from typing import TypeVar
|
|
|
|
|
2023-08-14 11:32:08 +00:00
|
|
|
from pyrainbird.async_client import (
|
|
|
|
AsyncRainbirdController,
|
|
|
|
RainbirdApiException,
|
|
|
|
RainbirdDeviceBusyException,
|
|
|
|
)
|
2023-09-26 00:27:38 +00:00
|
|
|
from pyrainbird.data import ModelAndVersion, Schedule
|
2022-12-22 21:00:17 +00:00
|
|
|
|
2023-09-26 00:27:38 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-22 21:00:17 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2022-12-22 21:00:17 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
|
2023-09-26 00:27:38 +00:00
|
|
|
from .const import CONF_SERIAL_NUMBER, DOMAIN, MANUFACTURER, TIMEOUT_SECONDS
|
2023-01-07 17:34:01 +00:00
|
|
|
|
2022-12-22 21:00:17 +00:00
|
|
|
UPDATE_INTERVAL = datetime.timedelta(minutes=1)
|
2023-09-26 00:27:38 +00:00
|
|
|
# The calendar data requires RPCs for each program/zone, and the data rarely
|
|
|
|
# changes, so we refresh it less often.
|
|
|
|
CALENDAR_UPDATE_INTERVAL = datetime.timedelta(minutes=15)
|
2022-12-22 21:00:17 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
_T = TypeVar("_T")
|
|
|
|
|
|
|
|
|
2023-01-07 17:34:01 +00:00
|
|
|
@dataclass
|
|
|
|
class RainbirdDeviceState:
|
|
|
|
"""Data retrieved from a Rain Bird device."""
|
|
|
|
|
|
|
|
zones: set[int]
|
|
|
|
active_zones: set[int]
|
|
|
|
rain: bool
|
|
|
|
rain_delay: int
|
|
|
|
|
|
|
|
|
|
|
|
class RainbirdUpdateCoordinator(DataUpdateCoordinator[RainbirdDeviceState]):
|
2022-12-22 21:00:17 +00:00
|
|
|
"""Coordinator for rainbird API calls."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
2023-01-07 17:34:01 +00:00
|
|
|
name: str,
|
|
|
|
controller: AsyncRainbirdController,
|
|
|
|
serial_number: str,
|
2023-07-09 23:49:44 +00:00
|
|
|
model_info: ModelAndVersion,
|
2022-12-22 21:00:17 +00:00
|
|
|
) -> None:
|
2023-09-26 00:27:38 +00:00
|
|
|
"""Initialize RainbirdUpdateCoordinator."""
|
2022-12-22 21:00:17 +00:00
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
2023-01-07 17:34:01 +00:00
|
|
|
name=name,
|
2022-12-22 21:00:17 +00:00
|
|
|
update_interval=UPDATE_INTERVAL,
|
|
|
|
)
|
2023-01-07 17:34:01 +00:00
|
|
|
self._controller = controller
|
|
|
|
self._serial_number = serial_number
|
|
|
|
self._zones: set[int] | None = None
|
2023-07-09 23:49:44 +00:00
|
|
|
self._model_info = model_info
|
2023-01-07 17:34:01 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def controller(self) -> AsyncRainbirdController:
|
|
|
|
"""Return the API client for the device."""
|
|
|
|
return self._controller
|
|
|
|
|
|
|
|
@property
|
|
|
|
def serial_number(self) -> str:
|
|
|
|
"""Return the device serial number."""
|
|
|
|
return self._serial_number
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self) -> DeviceInfo:
|
|
|
|
"""Return information about the device."""
|
|
|
|
return DeviceInfo(
|
2023-07-01 10:06:01 +00:00
|
|
|
name=f"{MANUFACTURER} Controller",
|
2023-01-07 17:34:01 +00:00
|
|
|
identifiers={(DOMAIN, self._serial_number)},
|
|
|
|
manufacturer=MANUFACTURER,
|
2023-07-09 23:49:44 +00:00
|
|
|
model=self._model_info.model_name,
|
|
|
|
sw_version=f"{self._model_info.major}.{self._model_info.minor}",
|
2023-01-07 17:34:01 +00:00
|
|
|
)
|
2022-12-22 21:00:17 +00:00
|
|
|
|
2023-01-07 17:34:01 +00:00
|
|
|
async def _async_update_data(self) -> RainbirdDeviceState:
|
|
|
|
"""Fetch data from Rain Bird device."""
|
2022-12-22 21:00:17 +00:00
|
|
|
try:
|
2023-08-15 15:21:49 +00:00
|
|
|
async with asyncio.timeout(TIMEOUT_SECONDS):
|
2023-01-07 17:34:01 +00:00
|
|
|
return await self._fetch_data()
|
2023-08-14 11:32:08 +00:00
|
|
|
except RainbirdDeviceBusyException as err:
|
|
|
|
raise UpdateFailed("Rain Bird device is busy") from err
|
2022-12-22 21:00:17 +00:00
|
|
|
except RainbirdApiException as err:
|
2023-08-14 11:32:08 +00:00
|
|
|
raise UpdateFailed("Rain Bird device failure") from err
|
2023-01-07 17:34:01 +00:00
|
|
|
|
|
|
|
async def _fetch_data(self) -> RainbirdDeviceState:
|
2023-02-07 03:57:16 +00:00
|
|
|
"""Fetch data from the Rain Bird device.
|
|
|
|
|
|
|
|
Rainbird devices can only reliably handle a single request at a time,
|
|
|
|
so the requests are sent serially.
|
|
|
|
"""
|
|
|
|
available_stations = await self._controller.get_available_stations()
|
|
|
|
states = await self._controller.get_zone_states()
|
|
|
|
rain = await self._controller.get_rain_sensor_state()
|
|
|
|
rain_delay = await self._controller.get_rain_delay()
|
2023-01-07 17:34:01 +00:00
|
|
|
return RainbirdDeviceState(
|
2023-02-07 03:57:16 +00:00
|
|
|
zones=available_stations.active_set,
|
|
|
|
active_zones=states.active_set,
|
2023-01-07 17:34:01 +00:00
|
|
|
rain=rain,
|
|
|
|
rain_delay=rain_delay,
|
|
|
|
)
|
2023-09-26 00:27:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RainbirdScheduleUpdateCoordinator(DataUpdateCoordinator[Schedule]):
|
|
|
|
"""Coordinator for rainbird irrigation schedule calls."""
|
|
|
|
|
|
|
|
config_entry: ConfigEntry
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
name: str,
|
|
|
|
controller: AsyncRainbirdController,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize ZoneStateUpdateCoordinator."""
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=name,
|
|
|
|
update_method=self._async_update_data,
|
|
|
|
update_interval=CALENDAR_UPDATE_INTERVAL,
|
|
|
|
)
|
|
|
|
self._controller = controller
|
|
|
|
|
|
|
|
async def _async_update_data(self) -> Schedule:
|
|
|
|
"""Fetch data from Rain Bird device."""
|
|
|
|
try:
|
2023-09-26 10:57:10 +00:00
|
|
|
async with asyncio.timeout(TIMEOUT_SECONDS):
|
2023-09-26 00:27:38 +00:00
|
|
|
return await self._controller.get_schedule()
|
|
|
|
except RainbirdApiException as err:
|
|
|
|
raise UpdateFailed(f"Error communicating with Device: {err}") from err
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class RainbirdData:
|
|
|
|
"""Holder for shared integration data.
|
|
|
|
|
|
|
|
The coordinators are lazy since they may only be used by some platforms when needed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
hass: HomeAssistant
|
|
|
|
entry: ConfigEntry
|
|
|
|
controller: AsyncRainbirdController
|
|
|
|
model_info: ModelAndVersion
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def coordinator(self) -> RainbirdUpdateCoordinator:
|
|
|
|
"""Return RainbirdUpdateCoordinator."""
|
|
|
|
return RainbirdUpdateCoordinator(
|
|
|
|
self.hass,
|
|
|
|
name=self.entry.title,
|
|
|
|
controller=self.controller,
|
|
|
|
serial_number=self.entry.data[CONF_SERIAL_NUMBER],
|
|
|
|
model_info=self.model_info,
|
|
|
|
)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def schedule_coordinator(self) -> RainbirdScheduleUpdateCoordinator:
|
|
|
|
"""Return RainbirdScheduleUpdateCoordinator."""
|
|
|
|
return RainbirdScheduleUpdateCoordinator(
|
|
|
|
self.hass,
|
|
|
|
name=f"{self.entry.title} Schedule",
|
|
|
|
controller=self.controller,
|
|
|
|
)
|