Support for parent/children relationships on zwave.mqtt entities

This commit is contained in:
Fabio Manganiello 2022-11-28 21:42:11 +01:00
parent 0edd73690b
commit cc156a53a1
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -1,8 +1,8 @@
from collections import OrderedDict
import json import json
import queue import queue
import re import re
from collections import OrderedDict
from datetime import datetime from datetime import datetime
from threading import Timer from threading import Timer
from typing import ( from typing import (
@ -18,7 +18,9 @@ from typing import (
Union, Union,
) )
from platypush.entities import Entity
from platypush.entities.batteries import Battery from platypush.entities.batteries import Battery
from platypush.entities.devices import Device
from platypush.entities.dimmers import Dimmer from platypush.entities.dimmers import Dimmer
from platypush.entities.electricity import ( from platypush.entities.electricity import (
EnergySensor, EnergySensor,
@ -663,8 +665,41 @@ class ZwaveMqttPlugin(MqttPlugin, ZwaveBasePlugin):
if entity_type: if entity_type:
entities.append(entity_type(**entity_args)) entities.append(entity_type(**entity_args))
self._process_parent_entities(values, entities)
return super().transform_entities(entities) # type: ignore return super().transform_entities(entities) # type: ignore
def _process_parent_entities(
self, values: Iterable[Mapping], entities: List[Entity]
):
parent_entities: Dict[int, Device] = {}
entities_by_id: Dict[str, Entity] = {str(e.id): e for e in entities}
for value in values:
entity = entities_by_id.get(value['id'])
if not entity:
continue
parent_id = value.get('parent_id')
if parent_id is None:
continue
node = self._nodes_cache['by_id'].get(parent_id)
if not node:
continue
node_id = node['node_id']
if node_id not in parent_entities:
parent = parent_entities[node_id] = Device(
id=node['device_id'],
name=node.get('name'),
reachable=(
node.get('is_available', False) and node.get('is_ready', False)
),
)
entities.append(parent)
entity.parent = parent_entities[node_id]
@staticmethod @staticmethod
def _merge_current_and_target_values(values: Iterable[dict]) -> List[dict]: def _merge_current_and_target_values(values: Iterable[dict]) -> List[dict]:
values_by_id = OrderedDict({v.get('id'): v for v in values}) values_by_id = OrderedDict({v.get('id'): v for v in values})