Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for mqtt client to become available #92524

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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