2022-11-07 15:40:23 +02:00
|
|
|
"""The Livisi Smart Home integration."""
|
2024-03-08 15:01:29 +01:00
|
|
|
|
2022-11-07 15:40:23 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Final
|
|
|
|
|
|
|
|
from aiohttp import ClientConnectorError
|
2024-12-01 22:41:01 +01:00
|
|
|
from livisi.aiolivisi import AioLivisi
|
2022-11-07 15:40:23 +02:00
|
|
|
|
|
|
|
from homeassistant import core
|
2023-02-27 11:38:52 +02:00
|
|
|
from homeassistant.const import Platform
|
2022-11-07 15:40:23 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers import aiohttp_client, device_registry as dr
|
|
|
|
|
2023-02-27 11:38:52 +02:00
|
|
|
from .const import DOMAIN
|
2025-06-23 19:09:38 +02:00
|
|
|
from .coordinator import LivisiConfigEntry, LivisiDataUpdateCoordinator
|
2022-11-07 15:40:23 +02:00
|
|
|
|
2023-03-24 14:52:50 +01:00
|
|
|
PLATFORMS: Final = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.SWITCH]
|
2022-11-07 15:40:23 +02:00
|
|
|
|
|
|
|
|
2025-06-23 19:09:38 +02:00
|
|
|
async def async_setup_entry(hass: core.HomeAssistant, entry: LivisiConfigEntry) -> bool:
|
2022-11-07 15:40:23 +02:00
|
|
|
"""Set up Livisi Smart Home from a config entry."""
|
|
|
|
web_session = aiohttp_client.async_get_clientsession(hass)
|
|
|
|
aiolivisi = AioLivisi(web_session)
|
|
|
|
coordinator = LivisiDataUpdateCoordinator(hass, entry, aiolivisi)
|
|
|
|
try:
|
|
|
|
await coordinator.async_setup()
|
|
|
|
await coordinator.async_set_all_rooms()
|
|
|
|
except ClientConnectorError as exception:
|
|
|
|
raise ConfigEntryNotReady from exception
|
|
|
|
|
2025-06-23 19:09:38 +02:00
|
|
|
entry.runtime_data = coordinator
|
2022-11-07 15:40:23 +02:00
|
|
|
device_registry = dr.async_get(hass)
|
|
|
|
device_registry.async_get_or_create(
|
2023-09-07 12:45:47 +02:00
|
|
|
config_entry_id=entry.entry_id,
|
2022-11-07 15:40:23 +02:00
|
|
|
identifiers={(DOMAIN, entry.entry_id)},
|
|
|
|
manufacturer="Livisi",
|
|
|
|
name=f"SHC {coordinator.controller_type} {coordinator.serial_number}",
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2023-02-17 14:57:00 -05:00
|
|
|
entry.async_create_background_task(
|
|
|
|
hass, coordinator.ws_connect(), "livisi-ws_connect"
|
|
|
|
)
|
2025-06-23 19:09:38 +02:00
|
|
|
entry.async_on_unload(coordinator.websocket.disconnect)
|
2022-11-07 15:40:23 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2025-06-23 19:09:38 +02:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: LivisiConfigEntry) -> bool:
|
2022-11-07 15:40:23 +02:00
|
|
|
"""Unload a config entry."""
|
2025-06-23 19:09:38 +02:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|