|
| 1 | +"""The nVent RAYCHEM SENZ integration.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from datetime import timedelta |
| 5 | +import logging |
| 6 | + |
| 7 | +from aiosenz import AUTHORIZATION_ENDPOINT, SENZAPI, TOKEN_ENDPOINT, Thermostat |
| 8 | +from httpx import RequestError |
| 9 | +import voluptuous as vol |
| 10 | + |
| 11 | +from homeassistant.config_entries import ConfigEntry |
| 12 | +from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, Platform |
| 13 | +from homeassistant.core import HomeAssistant |
| 14 | +from homeassistant.exceptions import ConfigEntryNotReady |
| 15 | +from homeassistant.helpers import ( |
| 16 | + config_entry_oauth2_flow, |
| 17 | + config_validation as cv, |
| 18 | + httpx_client, |
| 19 | +) |
| 20 | +from homeassistant.helpers.typing import ConfigType |
| 21 | +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed |
| 22 | + |
| 23 | +from . import config_flow |
| 24 | +from .api import SENZConfigEntryAuth |
| 25 | +from .const import DOMAIN |
| 26 | + |
| 27 | +UPDATE_INTERVAL = timedelta(seconds=30) |
| 28 | + |
| 29 | +_LOGGER = logging.getLogger(__name__) |
| 30 | + |
| 31 | +CONFIG_SCHEMA = vol.Schema( |
| 32 | + { |
| 33 | + DOMAIN: vol.Schema( |
| 34 | + { |
| 35 | + vol.Required(CONF_CLIENT_ID): cv.string, |
| 36 | + vol.Required(CONF_CLIENT_SECRET): cv.string, |
| 37 | + } |
| 38 | + ) |
| 39 | + }, |
| 40 | + extra=vol.ALLOW_EXTRA, |
| 41 | +) |
| 42 | + |
| 43 | +PLATFORMS = [Platform.CLIMATE] |
| 44 | + |
| 45 | +SENZDataUpdateCoordinator = DataUpdateCoordinator[dict[str, Thermostat]] |
| 46 | + |
| 47 | + |
| 48 | +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: |
| 49 | + """Set up the SENZ OAuth2 configuration.""" |
| 50 | + hass.data[DOMAIN] = {} |
| 51 | + |
| 52 | + if DOMAIN not in config: |
| 53 | + return True |
| 54 | + |
| 55 | + config_flow.OAuth2FlowHandler.async_register_implementation( |
| 56 | + hass, |
| 57 | + config_entry_oauth2_flow.LocalOAuth2Implementation( |
| 58 | + hass, |
| 59 | + DOMAIN, |
| 60 | + config[DOMAIN][CONF_CLIENT_ID], |
| 61 | + config[DOMAIN][CONF_CLIENT_SECRET], |
| 62 | + AUTHORIZATION_ENDPOINT, |
| 63 | + TOKEN_ENDPOINT, |
| 64 | + ), |
| 65 | + ) |
| 66 | + |
| 67 | + return True |
| 68 | + |
| 69 | + |
| 70 | +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 71 | + """Set up SENZ from a config entry.""" |
| 72 | + implementation = ( |
| 73 | + await config_entry_oauth2_flow.async_get_config_entry_implementation( |
| 74 | + hass, entry |
| 75 | + ) |
| 76 | + ) |
| 77 | + session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation) |
| 78 | + auth = SENZConfigEntryAuth(httpx_client.get_async_client(hass), session) |
| 79 | + senz_api = SENZAPI(auth) |
| 80 | + |
| 81 | + async def update_thermostats() -> dict[str, Thermostat]: |
| 82 | + """Fetch SENZ thermostats data.""" |
| 83 | + try: |
| 84 | + thermostats = await senz_api.get_thermostats() |
| 85 | + except RequestError as err: |
| 86 | + raise UpdateFailed from err |
| 87 | + return {thermostat.serial_number: thermostat for thermostat in thermostats} |
| 88 | + |
| 89 | + try: |
| 90 | + account = await senz_api.get_account() |
| 91 | + except RequestError as err: |
| 92 | + raise ConfigEntryNotReady from err |
| 93 | + |
| 94 | + coordinator = SENZDataUpdateCoordinator( |
| 95 | + hass, |
| 96 | + _LOGGER, |
| 97 | + name=account.username, |
| 98 | + update_interval=UPDATE_INTERVAL, |
| 99 | + update_method=update_thermostats, |
| 100 | + ) |
| 101 | + |
| 102 | + await coordinator.async_config_entry_first_refresh() |
| 103 | + |
| 104 | + hass.data[DOMAIN][entry.entry_id] = coordinator |
| 105 | + |
| 106 | + hass.config_entries.async_setup_platforms(entry, PLATFORMS) |
| 107 | + |
| 108 | + return True |
| 109 | + |
| 110 | + |
| 111 | +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 112 | + """Unload a config entry.""" |
| 113 | + if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): |
| 114 | + hass.data[DOMAIN].pop(entry.entry_id) |
| 115 | + |
| 116 | + return unload_ok |
0 commit comments