elad-bar / ha-blueiris

Integration with Blue Iris Video Security Software
184 stars 43 forks source link

Only subscribe to MQTT topics if MQTT is enabled in Home Assistant #171

Open nemec opened 2 years ago

nemec commented 2 years ago

So I'm getting the following error every time I boot Home Assistant and it seems to stem from the binary sensor of this project calling the async_subscribe method of homeassistant.components.mqtt. I do not have MQTT installed/enabled. The error happens within Home Assistant code, but if you can detect that MQTT is not enabled (I'm not sure how to do this TBH), maybe this code can avoid trying to subscribe?

This is on Ubuntu 22.04 with a brand new Home Assistant install (via git/virtualenv). ha-blueiris is installed in custom_components folder with default settings (though I see the same in my main HA instance installed via HACS) ETA: running Python 3.10

2022-06-02 20:14:52 ERROR (MainThread) [homeassistant.components.binary_sensor] Error adding entities for domain binary_sensor with platform blueiris
Traceback (most recent call last):
  File "/home/me/prg/src/home-assistant-core/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/home/me/prg/src/home-assistant-core/homeassistant/helpers/entity_platform.py", line 619, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/home/me/prg/src/home-assistant-core/homeassistant/helpers/entity.py", line 809, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/home/me/prg/src/home-assistant-core/config/custom_components/blueiris/models/base_entity.py", line 102, in async_added_to_hass
    await self.async_added_to_hass_local()
  File "/home/me/prg/src/home-assistant-core/config/custom_components/blueiris/binary_sensors/main.py", line 59, in async_added_to_hass_local
    self.remove_subscription = await async_subscribe(
  File "/home/me/prg/src/home-assistant-core/homeassistant/components/mqtt/client.py", line 198, in async_subscribe
    async_remove = await hass.data[DATA_MQTT].async_subscribe(
KeyError: 'mqtt'
nemec commented 2 years ago

Ok, so I did some testing on my main instance by commenting out the async_subscribe and it also solves my issue with #85. I don't know exactly what's happened, but it seems like the exception causes the internal EntityManager database to get out of sync with what's actually in Home Assistant, causing the BI plugin to try adding the entity a second time.

kramttocs commented 2 years ago

@nemec That is very promising!