zewelor / bt-mqtt-gateway

A simple Python script which provides a Bluetooth to MQTT gateway, easily extensible via custom workers. See https://github.com/zewelor/bt-mqtt-gateway/wiki for more information.
MIT License
549 stars 116 forks source link

avoiding data errors (e.g. device out of reach/battery) #249

Closed gingerlime closed 2 years ago

gingerlime commented 2 years ago

Description

In some cases (e.g. device is completely out of battery), the whole execution will fail with a TypeError, preventing live devices from updating as well.

Before the change, bt-mqtt-gateway would completely crash with this error:

bt-mqtt-gateway_1  | 14:46:36 Fatal error while executing worker command: TypeError
bt-mqtt-gateway_1  | Traceback (most recent call last):
bt-mqtt-gateway_1  |   File "./gateway.py", line 108, in <module>
bt-mqtt-gateway_1  |     raise e
bt-mqtt-gateway_1  |   File "./gateway.py", line 90, in <module>
bt-mqtt-gateway_1  |     mqtt.publish(_WORKERS_QUEUE.get(timeout=10).execute())
bt-mqtt-gateway_1  |   File "/application/workers_manager.py", line 45, in execute
bt-mqtt-gateway_1  |     for message in self._callback(*self._args):
bt-mqtt-gateway_1  |   File "/application/workers/lywsd03mmc_homeassistant.py", line 116, in status_update
bt-mqtt-gateway_1  |     yield self.update_device_state(name, device)
bt-mqtt-gateway_1  |   File "/application/workers/lywsd03mmc_homeassistant.py", line 162, in update_device_state
bt-mqtt-gateway_1  |     payload=self.true_false_to_ha_on_off(device.getBattery() < 3),
bt-mqtt-gateway_1  | TypeError: '<' not supported between instances of 'NoneType' and 'int'

With this change, the error is logged, but execution can continue as usual.

Log would look like this

14:48:36 Data error during update of lywsd03mmc_homeassistant device 'my_device' (a4:c1:38:**:**:**)

Type of change

Please delete options that are not relevant.

zewelor commented 2 years ago

Thanks !