Zigbee MQTT backend not handling empty topics #36

Closed
opened 2022-07-05 01:38:41 +02:00 by blacklight · 0 comments
Owner

Platypush V0.20.7 and Zigbee2MQTT V1.18.2.

When a zigbee device is renamed, zigbee2mqtt publishes a couple of messages without topics:

zigbee2mqtt/bridge/request/device/rename {"from": "TestSensor", "to": "TestSensor-12"}
zigbee2mqtt/TestSensor (null)
zigbee2mqtt/bridge/devices [{"definition":null,"endpoints":{"1":{...
zigbee2mqtt/TestSensor/availability (null)
zigbee2mqtt/TestSensor-12 {"battery":29,"illuminance":24292,"illuminance_lux":269,...

The zigbee.mqtt backend doesn't like this:

2021-04-01 15:32:59,284|WARNING|platypush|Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.7/dist-packages/platypush/backend/mqtt.py", line 80, in run
    self.loop_forever()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1779, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1181, in loop
    rc = self.loop_read(max_packets)
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1572, in loop_read
    rc = self._packet_read()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 2310, in _packet_read
    rc = self._packet_handle()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 2936, in _packet_handle
    return self._handle_publish()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3216, in _handle_publish
    self._handle_on_message(message)
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3444, in _handle_on_message
    self.on_message(self, self._userdata, message)
  File "/usr/local/lib/python3.7/dist-packages/platypush/backend/zigbee/mqtt.py", line 255, in handler
    changed_props = {k: v for k, v in data.items() if v != self._devices[name].get(k)}
AttributeError: 'str' object has no attribute 'items'

This causes the zigbee mqtt event handling to stop and no more zigbee events are sent.

To get round this, I've added a check which stops further processing of the message if the data is empty:

    def on_mqtt_message(self):
        def handler(client, _, msg):
            topic = msg.topic[len(self.base_topic)+1:]
            data = msg.payload.decode()

            if (not data): # <-- Check whether topic empty
                return

            # noinspection PyBroadException
            try:
                data = json.loads(data)
            except:
                pass

Not sure whether this is the best approach, but fixes things in this case.

Chris

Platypush V0.20.7 and Zigbee2MQTT V1.18.2. When a zigbee device is renamed, zigbee2mqtt publishes a couple of messages without topics: ``` zigbee2mqtt/bridge/request/device/rename {"from": "TestSensor", "to": "TestSensor-12"} zigbee2mqtt/TestSensor (null) zigbee2mqtt/bridge/devices [{"definition":null,"endpoints":{"1":{... zigbee2mqtt/TestSensor/availability (null) zigbee2mqtt/TestSensor-12 {"battery":29,"illuminance":24292,"illuminance_lux":269,... ``` The zigbee.mqtt backend doesn't like this: ``` 2021-04-01 15:32:59,284|WARNING|platypush|Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner self.run() File "/usr/local/lib/python3.7/dist-packages/platypush/backend/mqtt.py", line 80, in run self.loop_forever() File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1779, in loop_forever rc = self.loop(timeout, max_packets) File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1181, in loop rc = self.loop_read(max_packets) File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1572, in loop_read rc = self._packet_read() File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 2310, in _packet_read rc = self._packet_handle() File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 2936, in _packet_handle return self._handle_publish() File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3216, in _handle_publish self._handle_on_message(message) File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3444, in _handle_on_message self.on_message(self, self._userdata, message) File "/usr/local/lib/python3.7/dist-packages/platypush/backend/zigbee/mqtt.py", line 255, in handler changed_props = {k: v for k, v in data.items() if v != self._devices[name].get(k)} AttributeError: 'str' object has no attribute 'items' ``` This causes the zigbee mqtt event handling to stop and no more zigbee events are sent. To get round this, I've added a check which stops further processing of the message if the data is empty: ``` def on_mqtt_message(self): def handler(client, _, msg): topic = msg.topic[len(self.base_topic)+1:] data = msg.payload.decode() if (not data): # <-- Check whether topic empty return # noinspection PyBroadException try: data = json.loads(data) except: pass ``` Not sure whether this is the best approach, but fixes things in this case. Chris
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: platypush/platypush#36
No description provided.