Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for HomematicIP Cloud devices."""
2 
3 import voluptuous as vol
4 
5 from homeassistant import config_entries
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import CONF_NAME, EVENT_HOMEASSISTANT_STOP
8 from homeassistant.core import HomeAssistant, callback
9 from homeassistant.helpers import (
10  config_validation as cv,
11  device_registry as dr,
12  entity_registry as er,
13 )
14 from homeassistant.helpers.typing import ConfigType
15 
16 from .const import (
17  CONF_ACCESSPOINT,
18  CONF_AUTHTOKEN,
19  DOMAIN,
20  HMIPC_AUTHTOKEN,
21  HMIPC_HAPID,
22  HMIPC_NAME,
23 )
24 from .hap import HomematicipHAP
25 from .services import async_setup_services, async_unload_services
26 
27 CONFIG_SCHEMA = vol.Schema(
28  {
29  vol.Optional(DOMAIN, default=[]): vol.All(
30  cv.ensure_list,
31  [
32  vol.Schema(
33  {
34  vol.Optional(CONF_NAME, default=""): vol.Any(cv.string),
35  vol.Required(CONF_ACCESSPOINT): cv.string,
36  vol.Required(CONF_AUTHTOKEN): cv.string,
37  }
38  )
39  ],
40  )
41  },
42  extra=vol.ALLOW_EXTRA,
43 )
44 
45 
46 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
47  """Set up the HomematicIP Cloud component."""
48  hass.data[DOMAIN] = {}
49 
50  accesspoints = config.get(DOMAIN, [])
51 
52  for conf in accesspoints:
53  if conf[CONF_ACCESSPOINT] not in {
54  entry.data[HMIPC_HAPID]
55  for entry in hass.config_entries.async_entries(DOMAIN)
56  }:
57  hass.async_create_task(
58  hass.config_entries.flow.async_init(
59  DOMAIN,
60  context={"source": config_entries.SOURCE_IMPORT},
61  data={
62  HMIPC_HAPID: conf[CONF_ACCESSPOINT],
63  HMIPC_AUTHTOKEN: conf[CONF_AUTHTOKEN],
64  HMIPC_NAME: conf[CONF_NAME],
65  },
66  )
67  )
68 
69  return True
70 
71 
72 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
73  """Set up an access point from a config entry."""
74 
75  # 0.104 introduced config entry unique id, this makes upgrading possible
76  if entry.unique_id is None:
77  new_data = dict(entry.data)
78 
79  hass.config_entries.async_update_entry(
80  entry, unique_id=new_data[HMIPC_HAPID], data=new_data
81  )
82 
83  hap = HomematicipHAP(hass, entry)
84  hass.data[DOMAIN][entry.unique_id] = hap
85 
86  if not await hap.async_setup():
87  return False
88 
89  await async_setup_services(hass)
90  _async_remove_obsolete_entities(hass, entry, hap)
91 
92  # Register on HA stop event to gracefully shutdown HomematicIP Cloud connection
93  hap.reset_connection_listener = hass.bus.async_listen_once(
94  EVENT_HOMEASSISTANT_STOP, hap.shutdown
95  )
96 
97  # Register hap as device in registry.
98  device_registry = dr.async_get(hass)
99 
100  home = hap.home
101  hapname = home.label if home.label != entry.unique_id else f"Home-{home.label}"
102 
103  device_registry.async_get_or_create(
104  config_entry_id=entry.entry_id,
105  identifiers={(DOMAIN, home.id)},
106  manufacturer="eQ-3",
107  # Add the name from config entry.
108  name=hapname,
109  )
110  return True
111 
112 
113 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
114  """Unload a config entry."""
115  hap = hass.data[DOMAIN].pop(entry.unique_id)
116  hap.reset_connection_listener()
117 
118  await async_unload_services(hass)
119 
120  return await hap.async_reset()
121 
122 
123 @callback
125  hass: HomeAssistant, entry: ConfigEntry, hap: HomematicipHAP
126 ):
127  """Remove obsolete entities from entity registry."""
128 
129  if hap.home.currentAPVersion < "2.2.12":
130  return
131 
132  entity_registry = er.async_get(hass)
133  er_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
134  for er_entry in er_entries:
135  if er_entry.unique_id.startswith("HomematicipAccesspointStatus"):
136  entity_registry.async_remove(er_entry.entity_id)
137  continue
138 
139  for hapid in hap.home.accessPointUpdateStates:
140  if er_entry.unique_id == f"HomematicipBatterySensor_{hapid}":
141  entity_registry.async_remove(er_entry.entity_id)
None async_unload_services(HomeAssistant hass)
Definition: services.py:85
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:113
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:72
def _async_remove_obsolete_entities(HomeAssistant hass, ConfigEntry entry, HomematicipHAP hap)
Definition: __init__.py:126
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:46
None async_setup_services(HomeAssistant hass)
Definition: __init__.py:72