mirror of
https://github.com/home-assistant/core.git
synced 2026-08-03 20:24:55 +02:00
92 lines
3.3 KiB
Python
92 lines
3.3 KiB
Python
"""Support for Hydrawise cloud."""
|
|
|
|
from pydrawise import auth, hybrid
|
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_PASSWORD, CONF_USERNAME, Platform
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
|
from homeassistant.helpers import device_registry as dr
|
|
|
|
from .const import APP_ID, DOMAIN, MANUFACTURER
|
|
from .coordinator import (
|
|
HydrawiseConfigEntry,
|
|
HydrawiseMainDataUpdateCoordinator,
|
|
HydrawiseUpdateCoordinators,
|
|
HydrawiseWaterUseDataUpdateCoordinator,
|
|
)
|
|
|
|
PLATFORMS: list[Platform] = [
|
|
Platform.BINARY_SENSOR,
|
|
Platform.SENSOR,
|
|
Platform.SWITCH,
|
|
Platform.VALVE,
|
|
]
|
|
|
|
_REQUIRED_AUTH_KEYS = (CONF_USERNAME, CONF_PASSWORD, CONF_API_KEY)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant, config_entry: HydrawiseConfigEntry
|
|
) -> bool:
|
|
"""Set up Hydrawise from a config entry."""
|
|
if any(k not in config_entry.data for k in _REQUIRED_AUTH_KEYS):
|
|
# If we are missing any required authentication keys, trigger a reauth flow.
|
|
raise ConfigEntryAuthFailed
|
|
|
|
hydrawise = hybrid.HybridClient(
|
|
auth.HybridAuth(
|
|
config_entry.data[CONF_USERNAME],
|
|
config_entry.data[CONF_PASSWORD],
|
|
config_entry.data[CONF_API_KEY],
|
|
),
|
|
app_id=APP_ID,
|
|
)
|
|
|
|
main_coordinator = HydrawiseMainDataUpdateCoordinator(hass, config_entry, hydrawise)
|
|
await main_coordinator.async_config_entry_first_refresh()
|
|
water_use_coordinator = HydrawiseWaterUseDataUpdateCoordinator(
|
|
hass, config_entry, hydrawise, main_coordinator
|
|
)
|
|
|
|
device_registry = dr.async_get(hass)
|
|
|
|
@callback
|
|
def _async_register_controller_devices() -> None:
|
|
"""Register controller devices so children can resolve via_device_id.
|
|
|
|
Controllers can appear on later coordinator updates, so this runs on
|
|
every update before the zone-tracking listener, keeping the via_device
|
|
parents registered before their child entities are constructed.
|
|
"""
|
|
for controller in main_coordinator.data.controllers.values():
|
|
device_registry.async_get_or_create(
|
|
config_entry_id=config_entry.entry_id,
|
|
identifiers={(DOMAIN, str(controller.id))},
|
|
manufacturer=MANUFACTURER,
|
|
model=controller.hardware.model.description,
|
|
name=controller.name,
|
|
)
|
|
|
|
_async_register_controller_devices()
|
|
config_entry.async_on_unload(
|
|
main_coordinator.async_add_listener(_async_register_controller_devices)
|
|
)
|
|
|
|
# async_track_zones is registered first on water_use_coordinator,
|
|
# so the water-use coordinator's data is in sync before
|
|
# callbacks below construct entities for newly added zones.
|
|
water_use_coordinator.async_track_zones()
|
|
main_coordinator.async_track_zones()
|
|
await water_use_coordinator.async_config_entry_first_refresh()
|
|
config_entry.runtime_data = HydrawiseUpdateCoordinators(
|
|
main=main_coordinator,
|
|
water_use=water_use_coordinator,
|
|
)
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: HydrawiseConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|