Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Nobø Ecohub integration."""
2 
3 from __future__ import annotations
4 
5 from pynobo import nobo
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_IP_ADDRESS, EVENT_HOMEASSISTANT_STOP, Platform
9 from homeassistant.core import HomeAssistant
10 import homeassistant.util.dt as dt_util
11 
12 from .const import CONF_AUTO_DISCOVERED, CONF_SERIAL, DOMAIN
13 
14 PLATFORMS = [Platform.CLIMATE, Platform.SELECT, Platform.SENSOR]
15 
16 
17 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
18  """Set up Nobø Ecohub from a config entry."""
19 
20  serial = entry.data[CONF_SERIAL]
21  discover = entry.data[CONF_AUTO_DISCOVERED]
22  ip_address = None if discover else entry.data[CONF_IP_ADDRESS]
23  hub = nobo(
24  serial=serial,
25  ip=ip_address,
26  discover=discover,
27  synchronous=False,
28  timezone=dt_util.get_default_time_zone(),
29  )
30  await hub.connect()
31 
32  hass.data.setdefault(DOMAIN, {})
33 
34  async def _async_close(event):
35  """Close the Nobø Ecohub socket connection when HA stops."""
36  await hub.stop()
37 
38  entry.async_on_unload(
39  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_close)
40  )
41  hass.data[DOMAIN][entry.entry_id] = hub
42 
43  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
44 
45  entry.async_on_unload(entry.add_update_listener(options_update_listener))
46 
47  await hub.start()
48 
49  return True
50 
51 
52 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
53  """Unload a config entry."""
54 
55  hub: nobo = hass.data[DOMAIN][entry.entry_id]
56  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
57  await hub.stop()
58  hass.data[DOMAIN].pop(entry.entry_id)
59 
60  return unload_ok
61 
62 
64  hass: HomeAssistant, config_entry: ConfigEntry
65 ) -> None:
66  """Handle options update."""
67  await hass.config_entries.async_reload(config_entry.entry_id)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:17
None options_update_listener(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:65
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:52