Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Comelit integration."""
2 
3 from aiocomelit.const import BRIDGE
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import CONF_HOST, CONF_PIN, CONF_PORT, CONF_TYPE, Platform
7 from homeassistant.core import HomeAssistant
8 
9 from .const import DEFAULT_PORT, DOMAIN
10 from .coordinator import ComelitBaseCoordinator, ComelitSerialBridge, ComelitVedoSystem
11 
12 BRIDGE_PLATFORMS = [
13  Platform.CLIMATE,
14  Platform.COVER,
15  Platform.HUMIDIFIER,
16  Platform.LIGHT,
17  Platform.SENSOR,
18  Platform.SWITCH,
19 ]
20 VEDO_PLATFORMS = [
21  Platform.ALARM_CONTROL_PANEL,
22  Platform.BINARY_SENSOR,
23  Platform.SENSOR,
24 ]
25 
26 
27 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
28  """Set up Comelit platform."""
29 
30  coordinator: ComelitBaseCoordinator
31  if entry.data.get(CONF_TYPE, BRIDGE) == BRIDGE:
32  coordinator = ComelitSerialBridge(
33  hass,
34  entry.data[CONF_HOST],
35  entry.data.get(CONF_PORT, DEFAULT_PORT),
36  entry.data[CONF_PIN],
37  )
38  platforms = BRIDGE_PLATFORMS
39  else:
40  coordinator = ComelitVedoSystem(
41  hass,
42  entry.data[CONF_HOST],
43  entry.data.get(CONF_PORT, DEFAULT_PORT),
44  entry.data[CONF_PIN],
45  )
46  platforms = VEDO_PLATFORMS
47 
48  await coordinator.async_config_entry_first_refresh()
49 
50  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
51 
52  await hass.config_entries.async_forward_entry_setups(entry, platforms)
53 
54  return True
55 
56 
57 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
58  """Unload a config entry."""
59 
60  if entry.data.get(CONF_TYPE, BRIDGE) == BRIDGE:
61  platforms = BRIDGE_PLATFORMS
62  else:
63  platforms = VEDO_PLATFORMS
64 
65  coordinator: ComelitBaseCoordinator = hass.data[DOMAIN][entry.entry_id]
66  if unload_ok := await hass.config_entries.async_unload_platforms(entry, platforms):
67  await coordinator.api.logout()
68  await coordinator.api.close()
69  hass.data[DOMAIN].pop(entry.entry_id)
70 
71  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:27
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:57