Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Livisi Smart Home integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Final
6 
7 from aiohttp import ClientConnectorError
8 from aiolivisi import AioLivisi
9 
10 from homeassistant import core
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ConfigEntryNotReady
15 from homeassistant.helpers import aiohttp_client, device_registry as dr
16 
17 from .const import DOMAIN
18 from .coordinator import LivisiDataUpdateCoordinator
19 
20 PLATFORMS: Final = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.SWITCH]
21 
22 
23 async def async_setup_entry(hass: core.HomeAssistant, entry: ConfigEntry) -> bool:
24  """Set up Livisi Smart Home from a config entry."""
25  web_session = aiohttp_client.async_get_clientsession(hass)
26  aiolivisi = AioLivisi(web_session)
27  coordinator = LivisiDataUpdateCoordinator(hass, entry, aiolivisi)
28  try:
29  await coordinator.async_setup()
30  await coordinator.async_set_all_rooms()
31  except ClientConnectorError as exception:
32  raise ConfigEntryNotReady from exception
33 
34  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
35  device_registry = dr.async_get(hass)
36  device_registry.async_get_or_create(
37  config_entry_id=entry.entry_id,
38  identifiers={(DOMAIN, entry.entry_id)},
39  manufacturer="Livisi",
40  name=f"SHC {coordinator.controller_type} {coordinator.serial_number}",
41  )
42 
43  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
44  await coordinator.async_config_entry_first_refresh()
45  entry.async_create_background_task(
46  hass, coordinator.ws_connect(), "livisi-ws_connect"
47  )
48  return True
49 
50 
51 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
52  """Unload a config entry."""
53  coordinator = hass.data[DOMAIN][entry.entry_id]
54 
55  unload_success = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
56  await coordinator.websocket.disconnect()
57  if unload_success:
58  hass.data[DOMAIN].pop(entry.entry_id)
59 
60  return unload_success
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:51
bool async_setup_entry(core.HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:23