-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
64 lines (47 loc) · 2.15 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""The Elro Connects integration."""
from __future__ import annotations
import logging
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry, format_mac
from .const import DOMAIN
from .device import ElroConnectsK1
_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.SIREN]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Elro Connects from a config entry."""
elro_connects_api = ElroConnectsK1(hass, _LOGGER, entry)
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = elro_connects_api
await elro_connects_api.async_config_entry_first_refresh()
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(
entry.add_update_listener(elro_connects_api.async_update_settings)
)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
elro_connects_api: ElroConnectsK1 = hass.data[DOMAIN][entry.entry_id]
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
await elro_connects_api.async_disconnect()
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
async def async_remove_config_entry_device(
hass: HomeAssistant, entry: ConfigEntry, device_entry: DeviceEntry
) -> bool:
"""Allow manual removal of a device if not in use."""
elro_connects_api: ElroConnectsK1 = hass.data[DOMAIN][entry.entry_id]
device_unique_id: str = device_entry.identifiers.copy().pop()[1]
mac_address = format_mac(elro_connects_api.connector_id[3:])
if mac_address == device_unique_id:
return False
device_id_str = device_unique_id[len(elro_connects_api.connector_id) + 1 :]
device_id = int(device_id_str)
# Do not remove if the device_id is in the connector_data
if (
elro_connects_api.connector_data
and device_id in elro_connects_api.connector_data
):
return False
return True