maximvelichko / pyvera

A python library to control devices via the Vera hub
GNU General Public License v2.0
26 stars 31 forks source link

Unhandled exceptions in pyvera (HA AssertionError) #136

Closed GaryOkie closed 4 years ago

GaryOkie commented 4 years ago

Not sure if this is a pyvera issue or Home Assistant Vera integration issue. Started getting these with the update to HA .113.0 and .113.1.

The entities that are encountering errors are from CADDX/Vera plugin (security door switches). Not all switches are throwing this error.

Please let me know if this needs to be logged to HA Core Github as an issue instead.

2020-07-25 11:38:33 ERROR (SyncWorker_4) [pyvera] Unhandled exception in callback for device #11 (Util Room Entry Door)
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pyvera/__init__.py", line 1623, in _event_device
    callback(device)
  File "/usr/src/homeassistant/homeassistant/components/vera/__init__.py", line 201, in _update_callback
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 416, in schedule_update_ha_state
    assert self.hass is not None
AssertionError

2020-07-25 11:38:33 ERROR (SyncWorker_4) [pyvera] Unhandled exception in callback for device #11 (Util Room Entry Door)
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pyvera/__init__.py", line 1623, in _event_device
    callback(device)
  File "/usr/src/homeassistant/homeassistant/components/vera/__init__.py", line 201, in _update_callback
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 416, in schedule_update_ha_state
    assert self.hass is not None
AssertionError

2020-07-25 11:38:33 ERROR (SyncWorker_4) [pyvera] Unhandled exception in callback for device #12 (Deck French Doors)
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pyvera/__init__.py", line 1623, in _event_device
    callback(device)
  File "/usr/src/homeassistant/homeassistant/components/vera/__init__.py", line 201, in _update_callback
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 416, in schedule_update_ha_state
    assert self.hass is not None
AssertionError

2020-07-25 11:38:33 ERROR (SyncWorker_4) [pyvera] Unhandled exception in callback for device #12 (Deck French Doors)
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pyvera/__init__.py", line 1623, in _event_device
    callback(device)
  File "/usr/src/homeassistant/homeassistant/components/vera/__init__.py", line 201, in _update_callback
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 416, in schedule_update_ha_state
    assert self.hass is not None
AssertionError

2020-07-25 11:38:33 ERROR (SyncWorker_4) [pyvera] Unhandled exception in callback for device #13 (Garage Side Door)
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pyvera/__init__.py", line 1623, in _event_device
    callback(device)
  File "/usr/src/homeassistant/homeassistant/components/vera/__init__.py", line 201, in _update_callback
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 416, in schedule_update_ha_state
    assert self.hass is not None
AssertionError
pavoni commented 4 years ago

This looks like a repeat of something similar I fixed for another library I maintain.

It's a home assistant core code bug exposed by some HA changes that expose timing / setup errors. So what's happening is that update callbacks are happening before the HA entity being called is properly setup. It was always a potential race condition.

Unfortunately this causes an exception which kills the monitoring thread.

Here is what I did to fix it in loopenergy - something similar will be needed in the HA pyvera wrapper.

https://github.com/home-assistant/core/pull/37650/files

@vangorra greatly improved the HA pyvera wrapper code that I'd written - so he may want to do this - otherwise I'm happy to take a look.

GaryOkie commented 4 years ago

I'm a bit hyper-alert to new errors after an update so I thought this was something new related to HA .113.x as I had not encountered this particular error before. Interestingly, after a restart, these errors did not repeat, and all the switch states from Vera to HA are apparently being handled just fine.

So it may have been something related to this .113 update taking longer for HA to setup the entities upon first load?

I guess it is best to keep this open as your comment that the exception "kills the monitoring thread" sounds significant.

pavoni commented 4 years ago

I was in the code anyway - so have raise a simple PR https://github.com/home-assistant/core/pull/38251

@vangorra Can you check you're happy?