diff --git a/homeassistant/components/arwn/sensor.py b/homeassistant/components/arwn/sensor.py index f03734e63df..d468a93eca0 100644 --- a/homeassistant/components/arwn/sensor.py +++ b/homeassistant/components/arwn/sensor.py @@ -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. diff --git a/homeassistant/components/mqtt_eventstream/__init__.py b/homeassistant/components/mqtt_eventstream/__init__.py index 62a10c1bd00..af370fe82f3 100644 --- a/homeassistant/components/mqtt_eventstream/__init__.py +++ b/homeassistant/components/mqtt_eventstream/__init__.py @@ -1,5 +1,6 @@ """Connect two Home Assistant instances via MQTT.""" import json +import logging import voluptuous as vol @@ -21,6 +22,8 @@ import homeassistant.helpers.config_validation as cv 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" @@ -54,6 +57,11 @@ BLOCKED_EVENTS = [ 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) diff --git a/homeassistant/components/mqtt_statestream/__init__.py b/homeassistant/components/mqtt_statestream/__init__.py index aa4c2c628b4..32836825876 100644 --- a/homeassistant/components/mqtt_statestream/__init__.py +++ b/homeassistant/components/mqtt_statestream/__init__.py @@ -41,6 +41,11 @@ _LOGGER = logging.getLogger(__name__) 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] diff --git a/tests/components/mqtt_eventstream/test_init.py b/tests/components/mqtt_eventstream/test_init.py index 6e382036a1e..a61ea692bf2 100644 --- a/tests/components/mqtt_eventstream/test_init.py +++ b/tests/components/mqtt_eventstream/test_init.py @@ -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 @@ -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