forked from filipvh/hass-nhc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
88 lines (74 loc) · 3.29 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Helpers for NHC2."""
import logging
from homeassistant.core import callback
_LOGGER = logging.getLogger(__name__)
def nhc2_entity_processor(hass,
config_entry,
async_add_entities,
key,
obj_create):
"""Loops the entities list and creates, updates or deletes HA entities."""
@callback
def process_entities(entities):
_LOGGER.debug('Processing of entities started')
# Collect a list of active UUIDs
active_uuids = list(map(lambda x: x.uuid,
hass.data[key][config_entry.entry_id]))
_LOGGER.debug('Active UUIDs: %s', ', '.join(active_uuids))
# Sort out existing and new entities
new_entities, existing_entities = [], []
for entity in entities:
(new_entities, existing_entities)[entity.uuid in active_uuids]\
.append(entity)
_LOGGER.debug('Existing UUIDs: %s', ', '
.join(list(map(lambda x: x.uuid, existing_entities))))
_LOGGER.debug('New UUIDs: %s', ', '
.join(list(map(lambda x: x.uuid, new_entities))))
# Process the new entities
new_hass_entities = []
for entity in new_entities:
new_entity = obj_create(entity)
hass.data[key][config_entry.entry_id].append(new_entity)
new_hass_entities.append(new_entity)
async_add_entities(new_hass_entities)
_LOGGER.debug('Adding new entities done.')
# Process the existing entities (update)
for entity in existing_entities:
entity_to_update = \
next(filter((
lambda x: x.uuid == entity.uuid),
hass.data[key][config_entry.entry_id]), None)
entity_to_update.nhc2_update(entity)
_LOGGER.debug('Update done.')
# List UUIDs that should be removed
uuids_from_entities = list(map(lambda x: x.uuid, entities))
uuids_to_remove = \
[i for i in uuids_from_entities + active_uuids
if i not in uuids_from_entities]
_LOGGER.debug('UUIDs to remove: %s', ', '.join(uuids_to_remove))
# Remove entities (the need be removed)
for uuid_to_remove in uuids_to_remove:
entity_to_remove = next(filter((
lambda x: x.uuid == uuid_to_remove),
hass.data[key][config_entry.entry_id]), None)
hass.add_job(entity_to_remove.async_remove())
hass.data[key][config_entry.entry_id].remove(entity_to_remove)
_LOGGER.debug('Removals done.')
return process_entities
# Extract version numbers from sysinfo
def extract_versions(nhc2_sysinfo):
"""Return the versions, extracted from sysinfo."""
params = nhc2_sysinfo['Params']
system_info = next(filter(
(lambda x: x and 'SystemInfo' in x),
params), None)['SystemInfo']
s_w_versions = next(filter(
(lambda x: x and 'SWversions' in x),
system_info), None)['SWversions']
coco_image = next(filter(
(lambda x: x and 'CocoImage' in x),
s_w_versions), None)['CocoImage']
nhc_version = next(filter(
(lambda x: x and 'NhcVersion' in x),
s_w_versions), None)['NhcVersion']
return coco_image, nhc_version