Skip to content

Commit 104f821

Browse files
committed
Use action: instead of state: and update existing entities
1 parent dc11a0a commit 104f821

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

enoceanmqtt/overlays/homeassistant/ha_communicator.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,25 @@ def custom_merge(mapping_dict: Dict[int, Any], extra_mapping_dict: Dict[int, Any
400400
for type_, val_type in val_func.items():
401401
if 'entities' in val_type:
402402
for element in val_type['entities']:
403-
if 'state' not in element or element['state'] == 'present':
404-
element.pop('state', None)
405-
mapping_copy[rorg][func][type_]['entities'].append(element)
406-
elif element['state'] == 'absent':
403+
if 'action' in element and element['action'] == 'remove':
407404
mapping_copy[rorg][func][type_]['entities'] = list(
408405
filter(
409-
lambda entity: (entity['component'], entity['name']) != (element['component'], element['name']),
406+
lambda entity: (entity['component'],
407+
entity['name']) != (element['component'],
408+
element['name']),
410409
mapping_copy[rorg][func][type_]['entities'])
411410
)
411+
if 'action' not in element or element['action'] == 'add':
412+
element.pop('action', None)
413+
try:
414+
identical_entity = next(entity
415+
for entity in mapping_copy[rorg][func][type_]['entities']
416+
if (entity['component'],
417+
entity['name']) == (element['component'],
418+
element['name'])
419+
)
420+
identical_entity.update(element)
421+
except StopIteration:
422+
mapping_copy[rorg][func][type_]['entities'].append(element)
423+
412424
return mapping_copy

tests/resources/extra_mapping.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
device_class: outlet
1818
- component: "button"
1919
name: "query_status"
20-
state: "absent"
20+
action: "remove"

tests/test_mapping.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_add_mapping_2(self):
4040
entity_to_add = {'component': "cover",
4141
'name': "cover2",
4242
'config': {},
43-
'state': 'present'}
43+
'action': 'add'}
4444
extra_mapping = {
4545
0xD2: {
4646
0x05: {
@@ -60,13 +60,40 @@ def test_add_mapping_2(self):
6060
self.assertTrue(entity_to_add not in mapping[0xD2][0x05][0x00]['entities'])
6161
self.assertTrue('state' not in merged_mapping[0xD2][0x05][0x00]['entities'][-1])
6262

63+
def test_add_identical_mapping(self):
64+
mapping_path = Path(__file__).parent.parent / 'enoceanmqtt' / 'overlays' / 'homeassistant' / 'mapping.yaml'
65+
with open(mapping_path, 'r', encoding='utf-8') as mapping_file:
66+
mapping = yaml.safe_load(mapping_file)
67+
entity_to_add = {'component': "cover",
68+
'name': "cover",
69+
'added_key': 'added_value',
70+
'action': 'add'}
71+
extra_mapping = {
72+
0xD2: {
73+
0x05: {
74+
0x00: {
75+
'entities':
76+
[
77+
entity_to_add,
78+
]
79+
}
80+
}
81+
}
82+
}
83+
merged_mapping = custom_merge(mapping, extra_mapping)
84+
print(mapping[0xD2][0x05][0x00]['entities'])
85+
print(merged_mapping[0xD2][0x05][0x00]['entities'])
86+
self.assertTrue((entity_to_add | mapping[0xD2][0x05][0x00]['entities'][0]) in merged_mapping[0xD2][0x05][0x00]['entities']) # add assertion here
87+
self.assertTrue(entity_to_add not in mapping[0xD2][0x05][0x00]['entities'])
88+
self.assertTrue('action' not in merged_mapping[0xD2][0x05][0x00]['entities'][-1])
89+
6390
def test_remove_mapping(self):
6491
mapping_path = Path(__file__).parent.parent / 'enoceanmqtt' / 'overlays' / 'homeassistant' / 'mapping.yaml'
6592
with open(mapping_path, 'r', encoding='utf-8') as mapping_file:
6693
mapping = yaml.safe_load(mapping_file)
6794
entity_to_remove = {'component': "cover",
6895
'name': "cover",
69-
'state': 'absent'}
96+
'action': 'remove'}
7097
extra_mapping = {
7198
0xD2: {
7299
0x05: {

0 commit comments

Comments
 (0)