mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Wait for mqtt client to become available (#92524)
This commit is contained in:
parent
677ab5837f
commit
56dcb908bc
@ -101,6 +101,11 @@ async def async_setup_platform(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the ARWN platform."""
|
"""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
|
@callback
|
||||||
def async_sensor_event_received(msg: mqtt.ReceiveMessage) -> None:
|
def async_sensor_event_received(msg: mqtt.ReceiveMessage) -> None:
|
||||||
"""Process events as sensors.
|
"""Process events as sensors.
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""Connect two Home Assistant instances via MQTT."""
|
"""Connect two Home Assistant instances via MQTT."""
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -21,6 +22,8 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
from homeassistant.helpers.json import JSONEncoder
|
from homeassistant.helpers.json import JSONEncoder
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DOMAIN = "mqtt_eventstream"
|
DOMAIN = "mqtt_eventstream"
|
||||||
CONF_PUBLISH_TOPIC = "publish_topic"
|
CONF_PUBLISH_TOPIC = "publish_topic"
|
||||||
CONF_SUBSCRIBE_TOPIC = "subscribe_topic"
|
CONF_SUBSCRIBE_TOPIC = "subscribe_topic"
|
||||||
@ -54,6 +57,11 @@ BLOCKED_EVENTS = [
|
|||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the MQTT eventstream component."""
|
"""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, {})
|
conf = config.get(DOMAIN, {})
|
||||||
pub_topic = conf.get(CONF_PUBLISH_TOPIC)
|
pub_topic = conf.get(CONF_PUBLISH_TOPIC)
|
||||||
sub_topic = conf.get(CONF_SUBSCRIBE_TOPIC)
|
sub_topic = conf.get(CONF_SUBSCRIBE_TOPIC)
|
||||||
|
@ -41,6 +41,11 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the MQTT state feed."""
|
"""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]
|
conf: ConfigType = config[DOMAIN]
|
||||||
publish_filter = convert_include_exclude_filter(conf)
|
publish_filter = convert_include_exclude_filter(conf)
|
||||||
base_topic: str = conf[CONF_BASE_TOPIC]
|
base_topic: str = conf[CONF_BASE_TOPIC]
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
import json
|
import json
|
||||||
from unittest.mock import ANY, patch
|
from unittest.mock import ANY, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
import homeassistant.components.mqtt_eventstream as eventstream
|
import homeassistant.components.mqtt_eventstream as eventstream
|
||||||
from homeassistant.const import EVENT_STATE_CHANGED, MATCH_ALL
|
from homeassistant.const import EVENT_STATE_CHANGED, MATCH_ALL
|
||||||
from homeassistant.core import HomeAssistant, State, callback
|
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)
|
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:
|
async def test_setup_with_pub(hass: HomeAssistant, mqtt_mock: MqttMockHAClient) -> None:
|
||||||
"""Test the setup with subscription."""
|
"""Test the setup with subscription."""
|
||||||
# Should start off with no listeners for all events
|
# Should start off with no listeners for all events
|
||||||
|
Loading…
x
Reference in New Issue
Block a user