koying / mqtt_discoverystream_ha

Extension of HA mqtt_statestream integration with discovery config publishing
11 stars 6 forks source link

KeyError: 'mqtt' raised during setup of component mqtt_discoverystream #12

Open jamesremuscat opened 6 months ago

jamesremuscat commented 6 months ago

I'm sporadically getting the following error logged when HA starts:

Error during setup of component mqtt_discoverystream
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/client.py", line 186, in async_subscribe
    mqtt_data = get_mqtt_data(hass)
                ^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/mqtt/util.py", line 179, in get_mqtt_data
    mqtt_data = hass.data[DATA_MQTT]
                ~~~~~~~~~^^^^^^^^^^^
KeyError: 'mqtt'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/setup.py", line 332, in _async_setup_component
    result = await task
             ^^^^^^^^^^
  File "/config/custom_components/mqtt_discoverystream/__init__.py", line 302, in async_setup
    await hass.components.mqtt.async_subscribe(f"{base_topic}switch/+/set", message_received)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/client.py", line 188, in async_subscribe
    raise HomeAssistantError(
homeassistant.exceptions.HomeAssistantError: Cannot subscribe to topic 'homeassistant/switch/+/set', make sure MQTT is set up correctly

MQTT is set up correctly, I have a large number of devices working over MQTT (and my docker-compose config makes sure the MQTT broker is up and running before HA is started). Sometimes - maybe one time in five or six - everything starts up just fine and mqtt_discoverystream works perfectly, which makes me wonder if this is some sort of race condition and that HA is sometimes trying to initialise mqtt_discoverystream before the mqtt integration.

My mqtt_discoverystream configuration is via a packages/mqtt_discoverystream.yaml file:

mqtt_discoverystream:
  base_topic: homeassistant
  publish_discovery: true
  publish_attributes: true
  include:
    entities:
      light.floor_lamp_2
jamesremuscat commented 6 months ago

Should have also mentioned: this is using the most recent release of mqtt_discoverystream, which is v0.9.

rohankapoorcom commented 1 week ago

This should be fixed by #21