Skip to content

Commit

Permalink
Wait for mqtt client to become available (#92524)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed May 4, 2023
1 parent 677ab58 commit 56dcb90
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions homeassistant/components/arwn/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ async def async_setup_platform(
) -> None:
"""Set up the ARWN platform."""

# Make sure MQTT integration is enabled and the client is available
if not await mqtt.async_wait_for_mqtt_client(hass):
_LOGGER.error("MQTT integration is not available")
return

@callback
def async_sensor_event_received(msg: mqtt.ReceiveMessage) -> None:
"""Process events as sensors.
Expand Down
8 changes: 8 additions & 0 deletions homeassistant/components/mqtt_eventstream/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Connect two Home Assistant instances via MQTT."""
import json
import logging

import voluptuous as vol

Expand All @@ -21,6 +22,8 @@
from homeassistant.helpers.json import JSONEncoder
from homeassistant.helpers.typing import ConfigType

_LOGGER = logging.getLogger(__name__)

DOMAIN = "mqtt_eventstream"
CONF_PUBLISH_TOPIC = "publish_topic"
CONF_SUBSCRIBE_TOPIC = "subscribe_topic"
Expand Down Expand Up @@ -54,6 +57,11 @@

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the MQTT eventstream component."""
# Make sure MQTT integration is enabled and the client is available
if not await mqtt.async_wait_for_mqtt_client(hass):
_LOGGER.error("MQTT integration is not available")
return False

conf = config.get(DOMAIN, {})
pub_topic = conf.get(CONF_PUBLISH_TOPIC)
sub_topic = conf.get(CONF_SUBSCRIBE_TOPIC)
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/mqtt_statestream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the MQTT state feed."""
# Make sure MQTT integration is enabled and the client is available
if not await mqtt.async_wait_for_mqtt_client(hass):
_LOGGER.error("MQTT integration is not available")
return False

conf: ConfigType = config[DOMAIN]
publish_filter = convert_include_exclude_filter(conf)
base_topic: str = conf[CONF_BASE_TOPIC]
Expand Down
10 changes: 10 additions & 0 deletions tests/components/mqtt_eventstream/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
from unittest.mock import ANY, patch

import pytest

import homeassistant.components.mqtt_eventstream as eventstream
from homeassistant.const import EVENT_STATE_CHANGED, MATCH_ALL
from homeassistant.core import HomeAssistant, State, callback
Expand Down Expand Up @@ -36,6 +38,14 @@ async def test_setup_succeeds(hass: HomeAssistant, mqtt_mock: MqttMockHAClient)
assert await add_eventstream(hass)


async def test_setup_no_mqtt(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test the failure of the setup if mqtt is not set up."""
assert not await add_eventstream(hass)
assert "MQTT integration is not available" in caplog.text


async def test_setup_with_pub(hass: HomeAssistant, mqtt_mock: MqttMockHAClient) -> None:
"""Test the setup with subscription."""
# Should start off with no listeners for all events
Expand Down

0 comments on commit 56dcb90

Please sign in to comment.