fabicodes / zigbee2mqtt_ota_updater

Quick and possibly dirty solution to update your massive Zigbee network's devices.
GNU General Public License v3.0
12 stars 3 forks source link

Problem execution zigbee2mqtt 1.40.2 #4

Open jojeju9428 opened 3 weeks ago

jojeju9428 commented 3 weeks ago

Hello,

I am experiencing an issue running the script since version 1.40.2 of Zigbee2MQTT. Here are the errors encountered when launching the script:

Starting initialization
Looking for supported devices:
Exception in thread Thread-1 (_thread_main):
Traceback (most recent call last):
  File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
         ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 1164, in _loop
    rc = self.loop_read()
         ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 1556, in loop_read
    rc = self._packet_read()
         ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 2439, in _packet_read
    rc = self._packet_handle()
         ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 3033, in _packet_handle
    return self._handle_publish()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 3327, in _handle_publish
    self._handle_on_message(message)
  File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 3570, in _handle_on_message
    on_message(self, self._userdata, message)
  File "/etc/ansible/scripts/zigbee2mqtt_ota_updater/zigbee2mqtt-1/main.py", line 51, in on_message
    handle_devicelist(obj)
  File "/etc/ansible/scripts/zigbee2mqtt_ota_updater/zigbee2mqtt-1/main.py", line 84, in handle_devicelist
    if device["definition"]:
       ~~~~~~^^^^^^^^^^^^^^
KeyError: 'definition'

Thanks for help

jojeju9428 commented 3 weeks ago

Resolved by modifying the code as follows.

def handle_devicelist(devicelist):
    print("Looking for supported devices:")
    global otadict, num_total
    for device in devicelist:
        # Vérifier si la clé "definition" est présente dans l'objet device
        if "definition" in device and device["definition"]:
            dev = OtaDevice(
                device["friendly_name"],
                device["ieee_address"],
                device["definition"]["supports_ota"],
                False,
                False,
                False,
            )
            otadict[dev.ieee_addr] = dev
            if dev.supports_ota:
                print(
                    f"  {dev.friendly_name} supports OTA Updates, checking for new updates"
                )
                num_total += 1
                check_for_update(dev)
        else:
            print(f"Device {device.get('friendly_name', 'unknown')} does not support OTA updates or lacks definition data.")