116 lines
3.3 KiB
Python
116 lines
3.3 KiB
Python
"""Support for Rain Bird Irrigation system LNK WiFi Module."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from pyrainbird.async_client import AsyncRainbirdClient, AsyncRainbirdController
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
from homeassistant.const import (
|
|
CONF_FRIENDLY_NAME,
|
|
CONF_HOST,
|
|
CONF_PASSWORD,
|
|
CONF_TRIGGER_TIME,
|
|
Platform,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
from .const import CONF_SERIAL_NUMBER, CONF_ZONES
|
|
from .coordinator import RainbirdUpdateCoordinator
|
|
|
|
PLATFORMS = [Platform.SWITCH, Platform.SENSOR, Platform.BINARY_SENSOR, Platform.NUMBER]
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DOMAIN = "rainbird"
|
|
|
|
TRIGGER_TIME_SCHEMA = vol.All(
|
|
cv.time_period, cv.positive_timedelta, lambda td: (td.total_seconds() // 60)
|
|
)
|
|
|
|
ZONE_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
|
|
vol.Optional(CONF_TRIGGER_TIME): TRIGGER_TIME_SCHEMA,
|
|
}
|
|
)
|
|
CONTROLLER_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Required(CONF_HOST): cv.string,
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
vol.Required(CONF_TRIGGER_TIME): TRIGGER_TIME_SCHEMA,
|
|
vol.Optional(CONF_ZONES): vol.Schema({cv.positive_int: ZONE_SCHEMA}),
|
|
}
|
|
)
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
{DOMAIN: vol.Schema(vol.All(cv.ensure_list, [CONTROLLER_SCHEMA]))},
|
|
extra=vol.ALLOW_EXTRA,
|
|
)
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
"""Set up the Rain Bird component."""
|
|
if DOMAIN not in config:
|
|
return True
|
|
|
|
for controller_config in config[DOMAIN]:
|
|
hass.async_create_task(
|
|
hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_IMPORT},
|
|
data=controller_config,
|
|
)
|
|
)
|
|
|
|
async_create_issue(
|
|
hass,
|
|
DOMAIN,
|
|
"deprecated_yaml",
|
|
breaks_in_ha_version="2023.4.0",
|
|
is_fixable=False,
|
|
severity=IssueSeverity.WARNING,
|
|
translation_key="deprecated_yaml",
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Set up the config entry for Rain Bird."""
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
controller = AsyncRainbirdController(
|
|
AsyncRainbirdClient(
|
|
async_get_clientsession(hass),
|
|
entry.data[CONF_HOST],
|
|
entry.data[CONF_PASSWORD],
|
|
)
|
|
)
|
|
coordinator = RainbirdUpdateCoordinator(
|
|
hass,
|
|
name=entry.title,
|
|
controller=controller,
|
|
serial_number=entry.data[CONF_SERIAL_NUMBER],
|
|
)
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = coordinator
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
return unload_ok
|