seanrees / prometheus-dyson

Prometheus client for DysonLink fans (Pure Hot+Cool)
MIT License
13 stars 9 forks source link

AttributeError: 'NoneType' object has no attribute 'publish' #17

Closed oggust closed 2 years ago

oggust commented 2 years ago

Hello and thanks for this project!

I ran into an issue the other night:

[aug@ogg prometheus-dyson]./main.py  --config=aug-config
2022/02/20 18:56:51 [139731927258944]       INFO Starting up on port=8091
2022/02/20 18:56:51 [139731927258944]       INFO Reading "aug-config"
2022/02/20 18:56:51 [139731927258944]       INFO Starting discovery...
2022/02/20 18:56:51 [139731927258944]       INFO Attempting to discover device "Kitchen" (serial=VS9-EU-NBK1300A) via zeroconf
2022/02/20 18:56:51 [139731927258944]       INFO Attempting to discover device "Bedroom" (serial=VS9-EU-MHA5873A) via zeroconf
2022/02/20 18:56:52 [139731775612480]       INFO Discovered VS9-EU-NBK1300A on 192.168.1.240
2022/02/20 18:56:53 [139731775612480]       INFO Connected to device VS9-EU-NBK1300A
2022/02/23 21:07:00 [139731767219776]       INFO Device VS9-EU-NBK1300A is now disconnected, clearing it and re-adding
2022/02/23 21:07:00 [139731767219776]       INFO Attempting to discover device "Kitchen" (serial=VS9-EU-NBK1300A) via zeroconf
2022/02/23 21:07:00 [139731767219776]       INFO Discovered VS9-EU-NBK1300A on 192.168.1.240
2022/02/23 21:07:00 [139731767219776]       INFO Connected to device VS9-EU-NBK1300A
2022/02/23 21:07:00 [139731767219776]       INFO Device VS9-EU-NBK1300A is now disconnected, clearing it and re-adding
2022/02/23 21:07:00 [139731750434368]       INFO Device VS9-EU-NBK1300A is now disconnected, clearing it and re-adding
2022/02/23 21:07:00 [139731750434368]       INFO Attempting to discover device "Kitchen" (serial=VS9-EU-NBK1300A) via zeroconf
2022/02/23 21:07:00 [139731750434368]       INFO Discovered VS9-EU-NBK1300A on 192.168.1.240
2022/02/23 21:07:01 [139731750434368]       INFO Connected to device VS9-EU-NBK1300A
2022/02/23 21:07:01 [139731767219776]       INFO Attempting to discover device "Kitchen" (serial=VS9-EU-NBK1300A) via zeroconf
2022/02/23 21:07:01 [139731767219776]       INFO Discovered VS9-EU-NBK1300A on 192.168.1.240
2022/02/23 21:07:01 [139731767219776]       INFO Already connected to 192.168.1.240 (VS9-EU-NBK1300A); no need to reconnect.
Exception in thread Thread-26704:
Traceback (most recent call last):
  File "/usr/lib64/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/usr/lib64/python3.10/threading.py", line 1371, in run
    self.function(*self.args, **self.kwargs)
  File "/home/aug/src/prometheus-dyson/./main.py", line 88, in _timer_callback
    self.libdyson.request_environmental_data()
  File "/home/aug/.local/lib/python3.10/site-packages/libdyson/dyson_device.py", line 339, in request_environmental_data
    self._mqtt_client.publish(self._command_topic, json.dumps(payload))
AttributeError: 'NoneType' object has no attribute 'publish'
Exception in thread Thread-26708:
Traceback (most recent call last):
  File "/usr/lib64/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/usr/lib64/python3.10/threading.py", line 1371, in run
    self.function(*self.args, **self.kwargs)
  File "/home/aug/src/prometheus-dyson/./main.py", line 88, in _timer_callback
    self.libdyson.request_environmental_data()
  File "/home/aug/.local/lib/python3.10/site-packages/libdyson/dyson_device.py", line 339, in request_environmental_data
    self._mqtt_client.publish(self._command_topic, json.dumps(payload))
AttributeError: 'NoneType' object has no attribute 'publish'
Exception in thread Thread-26712:
Traceback (most recent call last):
  File "/usr/lib64/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/usr/lib64/python3.10/threading.py", line 1371, in run
    self.function(*self.args, **self.kwargs)
  File "/home/aug/src/prometheus-dyson/./main.py", line 88, in _timer_callback
    self.libdyson.request_environmental_data()
  File "/home/aug/.local/lib/python3.10/site-packages/libdyson/dyson_device.py", line 339, in request_environmental_data
    self._mqtt_client.publish(self._command_topic, json.dumps(payload))
AttributeError: 'NoneType' object has no attribute 'publish'
^C[aug@ogg prometheus-dyson]$ ./main.py  --config=aug-config
2022/02/26 20:08:30 [140364008355648]       INFO Starting up on port=8091
[...]

It hangs there, publishing no new data, but not dying. (Makes sense, the timer doesn't get reset)

I demoed the Dyson device (a TP04) a bit to someone, and ramped up the fan speeds etc, probably restarted it too, at about that time. I have not been able to reproduce it. (The other device, Bedroom was down with mechanical issues throughout.)

I had a quick look at the code, and while this seems like something that should be handled on the libdyson side (it's it's internal _mqtt_client that's None) but I figured it wouldn't hurt to paper it over on this side as well.

I'm running locally now with

$ git diff 
diff --git a/main.py b/main.py
index 332281e..3310b4d 100755
--- a/main.py
+++ b/main.py
@@ -85,7 +85,10 @@ class DeviceWrapper:
         if self.is_connected:
             logging.debug(
                 'Requesting updated environmental data from %s', self.serial)
-            self.libdyson.request_environmental_data()
+            try:
+                self.libdyson.request_environmental_data()
+            except AttributeError:
+                logging.error('Race with a disconnect? Skipping  an iteration.')
             self._refresh_timer()
         else:
             logging.debug('Device %s is disconnected.', self.serial)

to see if that fixes the symptom.