Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Bosch Smart Home Controller integration."""
2 
3 import logging
4 
5 from boschshcpy import SHCSession
6 from boschshcpy.exceptions import SHCAuthenticationError, SHCConnectionError
7 
8 from homeassistant.components.zeroconf import async_get_instance
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STOP, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
13 from homeassistant.helpers import device_registry as dr
14 
15 from .const import (
16  CONF_SSL_CERTIFICATE,
17  CONF_SSL_KEY,
18  DATA_POLLING_HANDLER,
19  DATA_SESSION,
20  DOMAIN,
21 )
22 
23 PLATFORMS = [
24  Platform.BINARY_SENSOR,
25  Platform.COVER,
26  Platform.SENSOR,
27  Platform.SWITCH,
28 ]
29 
30 _LOGGER = logging.getLogger(__name__)
31 
32 
33 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
34  """Set up Bosch SHC from a config entry."""
35  data = entry.data
36 
37  zeroconf = await async_get_instance(hass)
38  try:
39  session = await hass.async_add_executor_job(
40  SHCSession,
41  data[CONF_HOST],
42  data[CONF_SSL_CERTIFICATE],
43  data[CONF_SSL_KEY],
44  False,
45  zeroconf,
46  )
47  except SHCAuthenticationError as err:
48  raise ConfigEntryAuthFailed from err
49  except SHCConnectionError as err:
50  raise ConfigEntryNotReady from err
51 
52  shc_info = session.information
53  if shc_info.updateState.name == "UPDATE_AVAILABLE":
54  _LOGGER.warning("Please check for software updates in the Bosch Smart Home App")
55 
56  hass.data.setdefault(DOMAIN, {})
57  hass.data[DOMAIN][entry.entry_id] = {
58  DATA_SESSION: session,
59  }
60 
61  device_registry = dr.async_get(hass)
62  device_registry.async_get_or_create(
63  config_entry_id=entry.entry_id,
64  connections={(dr.CONNECTION_NETWORK_MAC, dr.format_mac(shc_info.unique_id))},
65  identifiers={(DOMAIN, shc_info.unique_id)},
66  manufacturer="Bosch",
67  name=entry.title,
68  model="SmartHomeController",
69  sw_version=shc_info.version,
70  )
71 
72  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
73 
74  async def stop_polling(event):
75  """Stop polling service."""
76  await hass.async_add_executor_job(session.stop_polling)
77 
78  await hass.async_add_executor_job(session.start_polling)
79  hass.data[DOMAIN][entry.entry_id][DATA_POLLING_HANDLER] = (
80  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_polling)
81  )
82 
83  return True
84 
85 
86 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
87  """Unload a config entry."""
88  session: SHCSession = hass.data[DOMAIN][entry.entry_id][DATA_SESSION]
89 
90  hass.data[DOMAIN][entry.entry_id][DATA_POLLING_HANDLER]()
91  hass.data[DOMAIN][entry.entry_id].pop(DATA_POLLING_HANDLER)
92  await hass.async_add_executor_job(session.stop_polling)
93 
94  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
95  if unload_ok:
96  hass.data[DOMAIN].pop(entry.entry_id)
97 
98  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:33
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:86
HaZeroconf async_get_instance(HomeAssistant hass)
Definition: __init__.py:146