2022-04-14 22:29:31 +00:00
|
|
|
"""The nVent RAYCHEM SENZ integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
2022-05-23 12:44:45 +00:00
|
|
|
from aiosenz import SENZAPI, Thermostat
|
2022-04-14 22:29:31 +00:00
|
|
|
from httpx import RequestError
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-07-19 04:14:55 +00:00
|
|
|
from homeassistant.const import Platform
|
2022-04-14 22:29:31 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers import (
|
|
|
|
config_entry_oauth2_flow,
|
|
|
|
config_validation as cv,
|
|
|
|
httpx_client,
|
|
|
|
)
|
2022-08-25 13:33:05 +00:00
|
|
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
2022-07-26 18:01:15 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2022-04-14 22:29:31 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
|
|
|
|
from .api import SENZConfigEntryAuth
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
UPDATE_INTERVAL = timedelta(seconds=30)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2022-07-19 04:14:55 +00:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2022-04-14 22:29:31 +00:00
|
|
|
|
|
|
|
PLATFORMS = [Platform.CLIMATE]
|
|
|
|
|
|
|
|
SENZDataUpdateCoordinator = DataUpdateCoordinator[dict[str, Thermostat]]
|
|
|
|
|
|
|
|
|
2022-07-26 18:01:15 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
|
|
"""Set up the SENZ integration."""
|
|
|
|
if DOMAIN in config:
|
|
|
|
async_create_issue(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
"removed_yaml",
|
|
|
|
breaks_in_ha_version="2022.8.0",
|
|
|
|
is_fixable=False,
|
|
|
|
severity=IssueSeverity.WARNING,
|
|
|
|
translation_key="removed_yaml",
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-04-14 22:29:31 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up SENZ from a config entry."""
|
|
|
|
implementation = (
|
|
|
|
await config_entry_oauth2_flow.async_get_config_entry_implementation(
|
|
|
|
hass, entry
|
|
|
|
)
|
|
|
|
)
|
|
|
|
session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
|
|
|
|
auth = SENZConfigEntryAuth(httpx_client.get_async_client(hass), session)
|
|
|
|
senz_api = SENZAPI(auth)
|
|
|
|
|
|
|
|
async def update_thermostats() -> dict[str, Thermostat]:
|
|
|
|
"""Fetch SENZ thermostats data."""
|
|
|
|
try:
|
|
|
|
thermostats = await senz_api.get_thermostats()
|
|
|
|
except RequestError as err:
|
|
|
|
raise UpdateFailed from err
|
|
|
|
return {thermostat.serial_number: thermostat for thermostat in thermostats}
|
|
|
|
|
|
|
|
try:
|
|
|
|
account = await senz_api.get_account()
|
|
|
|
except RequestError as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
|
|
|
|
coordinator = SENZDataUpdateCoordinator(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=account.username,
|
|
|
|
update_interval=UPDATE_INTERVAL,
|
|
|
|
update_method=update_thermostats,
|
|
|
|
)
|
|
|
|
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
2022-07-19 04:14:55 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
2022-04-14 22:29:31 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2022-04-14 22:29:31 +00:00
|
|
|
|
|
|
|
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
|