2022-09-04 19:42:08 +00:00
|
|
|
"""The Sensibo component."""
|
2024-03-08 13:33:51 +00:00
|
|
|
|
2021-12-22 20:32:50 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-02-27 21:38:39 +00:00
|
|
|
from pysensibo.exceptions import AuthenticationError
|
|
|
|
|
2023-10-15 16:14:38 +00:00
|
|
|
from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
|
2021-12-22 20:32:50 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-02-27 21:38:39 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
2021-12-22 20:32:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-10-15 16:14:38 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
from homeassistant.helpers.device_registry import DeviceEntry
|
2021-12-22 20:32:50 +00:00
|
|
|
|
2022-02-27 21:38:39 +00:00
|
|
|
from .const import DOMAIN, LOGGER, PLATFORMS
|
2022-02-02 12:09:42 +00:00
|
|
|
from .coordinator import SensiboDataUpdateCoordinator
|
2022-02-27 21:38:39 +00:00
|
|
|
from .util import NoDevicesError, NoUsernameError, async_validate_api
|
2021-12-22 20:32:50 +00:00
|
|
|
|
2024-05-01 20:02:36 +00:00
|
|
|
SensiboConfigEntry = ConfigEntry["SensiboDataUpdateCoordinator"]
|
2021-12-22 20:32:50 +00:00
|
|
|
|
2024-05-01 20:02:36 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: SensiboConfigEntry) -> bool:
|
2021-12-22 20:32:50 +00:00
|
|
|
"""Set up Sensibo from a config entry."""
|
2022-02-02 12:09:42 +00:00
|
|
|
|
2024-05-01 20:02:36 +00:00
|
|
|
coordinator = SensiboDataUpdateCoordinator(hass)
|
2022-02-02 12:09:42 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2024-05-01 20:02:36 +00:00
|
|
|
entry.runtime_data = coordinator
|
2021-12-22 20:32:50 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2022-02-02 12:09:42 +00:00
|
|
|
|
2021-12-22 20:32:50 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload Sensibo config entry."""
|
2024-05-01 20:02:36 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2022-02-27 21:38:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Migrate old entry."""
|
|
|
|
# Change entry unique id from api_key to username
|
|
|
|
if entry.version == 1:
|
|
|
|
api_key = entry.data[CONF_API_KEY]
|
|
|
|
|
|
|
|
try:
|
|
|
|
new_unique_id = await async_validate_api(hass, api_key)
|
|
|
|
except (AuthenticationError, ConnectionError, NoDevicesError, NoUsernameError):
|
|
|
|
return False
|
|
|
|
|
|
|
|
LOGGER.debug("Migrate Sensibo config entry unique id to %s", new_unique_id)
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
entry,
|
|
|
|
unique_id=new_unique_id,
|
2024-02-12 18:46:30 +00:00
|
|
|
version=2,
|
2022-02-27 21:38:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
2023-10-15 16:14:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_remove_config_entry_device(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
|
|
|
|
) -> bool:
|
|
|
|
"""Remove Sensibo config entry from a device."""
|
|
|
|
entity_registry = er.async_get(hass)
|
|
|
|
for identifier in device.identifiers:
|
|
|
|
if identifier[0] == DOMAIN and entity_registry.async_get_entity_id(
|
|
|
|
CLIMATE_DOMAIN, DOMAIN, identifier[1]
|
|
|
|
):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|