davesmeghead / visonic

Visonic Custom Component for integration with Home Assistant
Apache License 2.0
90 stars 20 forks source link

Disabled Entity - AttributeError: 'NoneType' object has no attribute 'loop' #152

Open reubn opened 3 weeks ago

reubn commented 3 weeks ago

Thanks for all the time and effort you've put into this!

Just a quick issue that doesn't require much investigation. v0.9.7.10

I've disabled one of my motion sensor entities in HA. The integration now errors and loses the connection as a result. When the entity is disabled, the VisonicBinarySensor doesn't initialise with self.hass, therefore causing the error.

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/asyncio/selector_events.py", line 1017, in _read_ready__data_received
    self._protocol.data_received(data)
  File "/config/custom_components/visonic/client.py", line 193, in data_received
    super().vp_data_received(data)
  File "/config/custom_components/visonic/pyvisonic.py", line 2538, in vp_data_received
    self._handle_received_byte(databyte)
  File "/config/custom_components/visonic/pyvisonic.py", line 2635, in _handle_received_byte
    self._processReceivedMessage(ackneeded=self.pmCurrentPDU.ackneeded, debugp=self.pmCurrentPDU.debugprint, data=self.ReceiveData)
  File "/config/custom_components/visonic/pyvisonic.py", line 2732, in _processReceivedMessage
    self.packet_callback(data)
  File "/config/custom_components/visonic/pyvisonic.py", line 3760, in _processReceivedPacket
    self.handle_msgtypeA5(packet[2:-2])
  File "/config/custom_components/visonic/pyvisonic.py", line 4229, in handle_msgtypeA5
    self._updateSensor(i = i, enrolled = True)
  File "/config/custom_components/visonic/pyvisonic.py", line 3313, in _updateSensor
    self.SensorList[i].pushChange(AlSensorCondition.RESET)
  File "/config/custom_components/visonic/pyhelper.py", line 276, in pushChange
    cb(self, s)
  File "/config/custom_components/visonic/binary_sensor.py", line 141, in onChange
    self.schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1242, in schedule_update_ha_state
    self.hass.loop.call_soon_threadsafe(
    ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'loop'

Full Log

Quick fix is to check that self.hass is defined, but you may want to make use of the entity registry to check the disabled status before adding the sensor entity e.g. here

/custom_components/visonic/binary_sensor.py

@@ -146,7 +146,7 @@ class VisonicBinarySensor(BinarySensorEntity):
    self._is_available = self._visonic_device.isEnrolled()
    #_LOGGER.debug(f"   In binary sensor VisonicSensor onchange self._is_available = {self._is_available}    self._current_value = 
    {self._current_value}")
    # Ask HA to schedule an update
-   if self.entity_id is not None:
+   if self.hass is not None and self.entity_id is not None:
        self.schedule_update_ha_state()
else:
    _LOGGER.debug("changeHandler: binary sensor on change called but sensor is not defined")
davesmeghead commented 2 weeks ago

Hi, I'll use your quick fix solution until I can look in to it further, just released dev_B0 0.9.8.0 as per the main forum https://community.home-assistant.io/t/visonic-powermax-and-powermaster-integration/316702/538

Thanks for the comment, please give the release a try Dave

davesmeghead commented 1 day ago

I've just created a HACS Master release 0.9.9.3. Can you check it out and try it and let me know if this is still a problem please?