Wait for mqtt client to become available (#92524)

pull/92543/head
Jan Bouwhuis 2023-05-04 19:48:32 +02:00 committed by GitHub
parent 677ab5837f
commit 56dcb908bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 0 deletions

View File

@ -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.

View File

@ -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)

View File

@ -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]

View File

@ -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