Dependencies for mqtt broken since move to internal mqtt component

Hello,
recently (2022.11) homeassistant started giving deprecation warnings for some mqtt config options and the advice to move from the mqtt component to the internal mqtt functionality, which I did. Since then, all custom components depending on it seem to have started failing. While debugging this, it looks like it’s a race condition, that the custom component is already getting initialised while the internal mqtt component isn’t loaded yet. It successfully loaded only once after restarting maybe 20 times. It seems to me that for custom components, which have “mqtt” in their dependencies in the manifest, one might expect that custom component not to get loaded until mqtt is loaded and ready.

code

async def async_setup(hass, config):
    """Set up the MQTT eventstream component."""
    mqtt = hass.components.mqtt
    await mqtt.async_subscribe(hass, topic,message_received)

error

    await mqtt.async_subscribe(hass, topic,message_received)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/client.py", line 196, in async_subscribe
    raise HomeAssistantError(
homeassistant.exceptions.HomeAssistantError: Cannot subscribe to topic 'homeassistant/tele/iotbridge_DBFBED/SENSOR', MQTT is not enabled

I do see my custom component getting set up after mqtt in any case, but it seems that’s not enough anymore

...
2022-11-26 22:21:52.160 INFO (MainThread) [homeassistant.setup] Setting up mqtt
2022-11-26 22:21:52.160 INFO (MainThread) [homeassistant.setup] Setup of domain mqtt took 0.0 seconds
...
2022-11-26 22:21:52.437 INFO (MainThread) [homeassistant.setup] Setting up mqtt_testcomponent
2022-11-26 22:21:52.439 ERROR (MainThread) [homeassistant.setup] Error during setup of component mqtt_testcomponent
Traceback (most recent call last):
...

when I add something else to the manifest dependencies, e.g. “hacs”, which is loaded quite a bit later, and makes the custom component thus initialize after it, it finds the mqtt working, in my case : every time I tried.

  "dependencies": ["mqtt","hacs"],

Seems like this is related :

I found that this :

so having this in async_setup() seems to work


    # Make sure MQTT is available and the entry is loaded
    if not hass.config_entries.async_entries(
        mqtt.DOMAIN
    ) or not await hass.config_entries.async_wait_component(
        hass.config_entries.async_entries(mqtt.DOMAIN)[0]
    ):
        _LOGGER.error("MQTT integration is not available")
        return False