Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The devolo_home_control integration."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from functools import partial
7 from types import MappingProxyType
8 from typing import Any
9 
10 from devolo_home_control_api.exceptions.gateway import GatewayOfflineError
11 from devolo_home_control_api.homecontrol import HomeControl
12 from devolo_home_control_api.mydevolo import Mydevolo
13 
14 from homeassistant.components import zeroconf
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, EVENT_HOMEASSISTANT_STOP
17 from homeassistant.core import Event, HomeAssistant
18 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
19 from homeassistant.helpers.device_registry import DeviceEntry
20 
21 from .const import CONF_MYDEVOLO, DEFAULT_MYDEVOLO, GATEWAY_SERIAL_PATTERN, PLATFORMS
22 
23 type DevoloHomeControlConfigEntry = ConfigEntry[list[HomeControl]]
24 
25 
27  hass: HomeAssistant, entry: DevoloHomeControlConfigEntry
28 ) -> bool:
29  """Set up the devolo account from a config entry."""
30  mydevolo = configure_mydevolo(entry.data)
31 
32  credentials_valid = await hass.async_add_executor_job(mydevolo.credentials_valid)
33 
34  if not credentials_valid:
35  raise ConfigEntryAuthFailed
36 
37  if await hass.async_add_executor_job(mydevolo.maintenance):
38  raise ConfigEntryNotReady
39 
40  gateway_ids = await hass.async_add_executor_job(mydevolo.get_gateway_ids)
41 
42  if entry.unique_id and GATEWAY_SERIAL_PATTERN.match(entry.unique_id):
43  uuid = await hass.async_add_executor_job(mydevolo.uuid)
44  hass.config_entries.async_update_entry(entry, unique_id=uuid)
45 
46  def shutdown(event: Event) -> None:
47  for gateway in entry.runtime_data:
48  gateway.websocket_disconnect(
49  f"websocket disconnect requested by {EVENT_HOMEASSISTANT_STOP}"
50  )
51 
52  # Listen when EVENT_HOMEASSISTANT_STOP is fired
53  entry.async_on_unload(
54  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown)
55  )
56 
57  try:
58  zeroconf_instance = await zeroconf.async_get_instance(hass)
59  entry.runtime_data = []
60  for gateway_id in gateway_ids:
61  entry.runtime_data.append(
62  await hass.async_add_executor_job(
63  partial(
64  HomeControl,
65  gateway_id=str(gateway_id),
66  mydevolo_instance=mydevolo,
67  zeroconf_instance=zeroconf_instance,
68  )
69  )
70  )
71  except GatewayOfflineError as err:
72  raise ConfigEntryNotReady from err
73 
74  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
75 
76  return True
77 
78 
80  hass: HomeAssistant, entry: DevoloHomeControlConfigEntry
81 ) -> bool:
82  """Unload a config entry."""
83  unload = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
84  await asyncio.gather(
85  *(
86  hass.async_add_executor_job(gateway.websocket_disconnect)
87  for gateway in entry.runtime_data
88  )
89  )
90  return unload
91 
92 
94  hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
95 ) -> bool:
96  """Remove a config entry from a device."""
97  return True
98 
99 
100 def configure_mydevolo(conf: dict[str, Any] | MappingProxyType[str, Any]) -> Mydevolo:
101  """Configure mydevolo."""
102  mydevolo = Mydevolo()
103  mydevolo.user = conf[CONF_USERNAME]
104  mydevolo.password = conf[CONF_PASSWORD]
105  mydevolo.url = conf.get(CONF_MYDEVOLO, DEFAULT_MYDEVOLO)
106  return mydevolo
bool async_unload_entry(HomeAssistant hass, DevoloHomeControlConfigEntry entry)
Definition: __init__.py:81
bool async_remove_config_entry_device(HomeAssistant hass, ConfigEntry config_entry, DeviceEntry device_entry)
Definition: __init__.py:95
bool async_setup_entry(HomeAssistant hass, DevoloHomeControlConfigEntry entry)
Definition: __init__.py:28
Mydevolo configure_mydevolo(dict[str, Any]|MappingProxyType[str, Any] conf)
Definition: __init__.py:100