-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.py
73 lines (65 loc) · 2.43 KB
/
helpers.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
65
66
67
68
69
70
71
72
73
"""Helper functions for Elro Connects."""
from __future__ import annotations
from elro.device import ATTR_DEVICE_TYPE
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN, ELRO_CONNECTS_NEW_DEVICE
from .device import ElroConnectsK1
@callback
def async_set_up_discovery_helper(
hass: HomeAssistant,
entity_class: type,
entry: ConfigEntry,
current: set[int],
descriptions: dict[str, EntityDescription],
async_add_entities: AddEntitiesCallback,
):
"""Help to set up an entity."""
@callback
def _async_add_entities():
elro_connects_api: ElroConnectsK1 = hass.data[DOMAIN][entry.entry_id]
device_data: dict[int, dict] = elro_connects_api.connector_data
if not device_data:
return
new_items = []
for device_id, attributes in device_data.items():
# description
if device_id in current:
continue
if ATTR_DEVICE_TYPE not in attributes:
# Skip a data update if there is no device type attribute provided
continue
# Ensure we only add entities once
current.add(device_id)
device_type = attributes[ATTR_DEVICE_TYPE]
if device_type in descriptions:
new_item = entity_class(
elro_connects_api,
entry,
device_id,
descriptions[device_type],
)
new_items.append(new_item)
else:
for attribute in attributes:
if attribute in descriptions:
new_item = entity_class(
elro_connects_api,
entry,
device_id,
descriptions[attribute],
)
new_items.append(new_item)
async_add_entities(new_items)
entry.async_on_unload(
async_dispatcher_connect(
hass,
ELRO_CONNECTS_NEW_DEVICE.format(entry.entry_id),
_async_add_entities,
)
)
# Initial setup on first fetch
_async_add_entities()