dib0 / elro_connects

The ELRO Connects uses a propriety protocol on the local network over UDP. This is the reversed engineered python implementation of the protocol.
MIT License
16 stars 7 forks source link

Crash on alarm when device is not yet created #14

Closed depuits closed 2 years ago

depuits commented 3 years ago

When you start the hub while there was or is an alarm message then it crashes.

The problem comes from the generic create_device_from_data method. This is called from the DEVICE_STATUS_UPDATE and DEVICE_ALARM_TRIGGER commands while their received data is not in the same format. In case of the DEVICE_ALARM_TRIGGER command the device_name which is actually the device type is missing from the data triggering an exception.

A solution would be to serialize the data before calling the create device method but I don't see where we can get the device type in the DEVICE_ALARM_TRIGGER command response.

Call stack:

INFO:root:Received data: {"msgId" : 25,"action" : "devSend","params" : {"devTid" : "ST_xxxxxxxxxxxx","appTid" :  [],"data" : {"cmdId" : 25,"answer_content" : "000BAD00030013046419A551EA" }}}
INFO:root:Handle command: {'devTid': 'ST_xxxxxxxxxxxx', 'appTid': [], 'data': {'cmdId': 25, 'answer_content': '000BAD00030013046419A551EA'}}
INFO:root:Create device.
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/elro/hub.py", line 167, in handle_command
    dev = self.devices[d_id]
KeyError: 3

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/elro", line 30, in <module>
    trio.run(main, args.hostname, args.id, args.mqtt_broker, args.base_topic)
  File "/usr/local/lib/python3.9/site-packages/trio/_core/_run.py", line 1932, in run
    raise runner.main_task_outcome.error
  File "/usr/local/bin/elro", line 17, in main
    nursery.start_soon(hub.receiver_task, name="hub_receiver")
  File "/usr/local/lib/python3.9/site-packages/trio/_core/_run.py", line 815, in __aexit__
    raise combined_error_from_nursery
  File "/usr/local/lib/python3.9/site-packages/elro/hub.py", line 65, in receiver_task
    await self.receive_data()
  File "/usr/local/lib/python3.9/site-packages/elro/hub.py", line 122, in receive_data
    await self.handle_command(dat)
  File "/usr/local/lib/python3.9/site-packages/elro/hub.py", line 169, in handle_command
    dev = await self.create_device(data)
  File "/usr/local/lib/python3.9/site-packages/elro/hub.py", line 134, in create_device
    dev = create_device_from_data(data)
  File "/usr/local/lib/python3.9/site-packages/elro/device.py", line 190, in create_device_from_data
    if data["data"]["device_name"] == DeviceType.DOOR_WINDOW_SENSOR.value:
KeyError: 'device_name
hildensia commented 3 years ago

Hi @depuits -- thanks for all the bug reports. At the moment I don't have continuously access to my elro installation, but I will come back all the tickets in Mid-/Late-July.