A more robust handling of events in the zwave.mqtt backend

This commit is contained in:
Fabio Manganiello 2023-01-21 23:44:51 +01:00
parent 3940288396
commit 32e4e60579
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -17,6 +17,7 @@ from platypush.message.event.zwave import (
ZwaveNodeEvent, ZwaveNodeEvent,
ZwaveNodeAsleepEvent, ZwaveNodeAsleepEvent,
ZwaveNodeAwakeEvent, ZwaveNodeAwakeEvent,
ZwaveValueRemovedEvent,
) )
@ -144,44 +145,29 @@ class ZwaveMqttBackend(MqttBackend):
value = kwargs['value'] = node_values[value_id] value = kwargs['value'] = node_values[value_id]
if event_type == ZwaveNodeEvent: if issubclass(event_type, ZwaveNodeEvent):
# If the node has been removed, remove it from the cache
if event_type == ZwaveNodeRemovedEvent:
self._nodes.pop(node_id, None)
# If this node_id wasn't cached before, then it's a new node # If this node_id wasn't cached before, then it's a new node
if node_id not in self._nodes: elif node_id not in self._nodes:
event_type = ZwaveNodeAddedEvent event_type = ZwaveNodeAddedEvent
# If the name has changed, we have a rename event # If the name has changed, we have a rename event
elif node['name'] != self._nodes[node_id]['name']: elif node['name'] != self._nodes[node_id]['name']:
event_type = ZwaveNodeRenamedEvent event_type = ZwaveNodeRenamedEvent
# If nothing relevant has changed, update the cached instance and return
if event_type == ZwaveNodeRemovedEvent:
self._nodes.pop(node_id, None)
else: else:
self._nodes[node_id] = node self._nodes[node_id] = node
return
evt = event_type(**kwargs) evt = event_type(**kwargs)
self._events_queue.put(evt) self._events_queue.put(evt)
if value and event_type == ZwaveValueChangedEvent: if (
value = value.copy() value
and issubclass(event_type, ZwaveValueChangedEvent)
# zwavejs2mqtt currently treats some values (e.g. binary switches) in an inconsistent way, and event_type != ZwaveValueRemovedEvent
# using two values - a read-only value called currentValue that gets updated on the ):
# node_value_updated topic, and a writable value called targetValue that doesn't get updated
# (see https://github.com/zwave-js/zwavejs2mqtt/blob/4a6a5c5f1274763fd3aced4cae2c72ea060716b5 \
# /docs/guide/migrating.md).
# To properly manage updates on writable values, propagate an event for both.
if value.get('property_id') == 'currentValue':
target_value_id = (
f'{node_id}-{value["command_class"]}-{value.get("endpoint", 0)}'
f'-targetValue'
)
target_value = node_values.get(target_value_id)
if target_value:
kwargs['value']['data'] = value['data']
kwargs['node']['values'][target_value_id] = kwargs['value']
evt = event_type(**kwargs)
self._events_queue.put(evt)
self.plugin.publish_entities([kwargs['value']]) # type: ignore self.plugin.publish_entities([kwargs['value']]) # type: ignore
def on_mqtt_message(self): def on_mqtt_message(self):