diff --git a/homeassistant/components/ozw/__init__.py b/homeassistant/components/ozw/__init__.py index 0636671188d..a75c05416dc 100644 --- a/homeassistant/components/ozw/__init__.py +++ b/homeassistant/components/ozw/__init__.py @@ -21,7 +21,7 @@ from openzwavemqtt.util.mqtt_client import MQTTClient from homeassistant.components import mqtt from homeassistant.components.hassio.handler import HassioAPIError -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import ENTRY_STATE_LOADED, ConfigEntry from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady @@ -95,12 +95,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): manager_options["send_message"] = mqtt_client.send_message else: - if "mqtt" not in hass.config.components: + mqtt_entries = hass.config_entries.async_entries("mqtt") + if not mqtt_entries or mqtt_entries[0].state != ENTRY_STATE_LOADED: _LOGGER.error("MQTT integration is not set up") return False + mqtt_entry = mqtt_entries[0] # MQTT integration only has one entry. + @callback def send_message(topic, payload): + if mqtt_entry.state != ENTRY_STATE_LOADED: + _LOGGER.error("MQTT integration is not set up") + return + mqtt.async_publish(hass, topic, json.dumps(payload)) manager_options["send_message"] = send_message diff --git a/homeassistant/components/ozw/config_flow.py b/homeassistant/components/ozw/config_flow.py index 14d875e0a70..00917c0609c 100644 --- a/homeassistant/components/ozw/config_flow.py +++ b/homeassistant/components/ozw/config_flow.py @@ -97,7 +97,11 @@ class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): This is the entry point for the logic that is needed when this integration will depend on the MQTT integration. """ - if "mqtt" not in self.hass.config.components: + mqtt_entries = self.hass.config_entries.async_entries("mqtt") + if ( + not mqtt_entries + or mqtt_entries[0].state != config_entries.ENTRY_STATE_LOADED + ): return self.async_abort(reason="mqtt_required") return self._async_create_entry_from_vars() diff --git a/tests/components/ozw/common.py b/tests/components/ozw/common.py index 6b44d364413..1467d619afe 100644 --- a/tests/components/ozw/common.py +++ b/tests/components/ozw/common.py @@ -10,7 +10,8 @@ from tests.common import MockConfigEntry async def setup_ozw(hass, entry=None, fixture=None): """Set up OZW and load a dump.""" - hass.config.components.add("mqtt") + mqtt_entry = MockConfigEntry(domain="mqtt", state=config_entries.ENTRY_STATE_LOADED) + mqtt_entry.add_to_hass(hass) if entry is None: entry = MockConfigEntry( diff --git a/tests/components/ozw/conftest.py b/tests/components/ozw/conftest.py index 00f8d8e52d2..a59388f118f 100644 --- a/tests/components/ozw/conftest.py +++ b/tests/components/ozw/conftest.py @@ -4,9 +4,11 @@ from unittest.mock import patch import pytest +from homeassistant.config_entries import ENTRY_STATE_LOADED + from .common import MQTTMessage -from tests.common import load_fixture +from tests.common import MockConfigEntry, load_fixture from tests.components.light.conftest import mock_light_profiles # noqa @@ -268,3 +270,11 @@ def mock_get_addon_discovery_info(): "homeassistant.components.hassio.async_get_addon_discovery_info" ) as get_addon_discovery_info: yield get_addon_discovery_info + + +@pytest.fixture(name="mqtt") +async def mock_mqtt_fixture(hass): + """Mock the MQTT integration.""" + mqtt_entry = MockConfigEntry(domain="mqtt", state=ENTRY_STATE_LOADED) + mqtt_entry.add_to_hass(hass) + return mqtt_entry diff --git a/tests/components/ozw/test_config_flow.py b/tests/components/ozw/test_config_flow.py index d1ac413270d..0a746398cf9 100644 --- a/tests/components/ozw/test_config_flow.py +++ b/tests/components/ozw/test_config_flow.py @@ -79,9 +79,8 @@ def mock_start_addon(): yield start_addon -async def test_user_not_supervisor_create_entry(hass): +async def test_user_not_supervisor_create_entry(hass, mqtt): """Test the user step creates an entry not on Supervisor.""" - hass.config.components.add("mqtt") await setup.async_setup_component(hass, "persistent_notification", {}) with patch( @@ -128,9 +127,8 @@ async def test_one_instance_allowed(hass): assert result["reason"] == "single_instance_allowed" -async def test_not_addon(hass, supervisor): +async def test_not_addon(hass, supervisor, mqtt): """Test opting out of add-on on Supervisor.""" - hass.config.components.add("mqtt") await setup.async_setup_component(hass, "persistent_notification", {}) result = await hass.config_entries.flow.async_init( diff --git a/tests/components/ozw/test_init.py b/tests/components/ozw/test_init.py index ac7ad59f3cb..c76bfd4a3a0 100644 --- a/tests/components/ozw/test_init.py +++ b/tests/components/ozw/test_init.py @@ -37,6 +37,26 @@ async def test_setup_entry_without_mqtt(hass): assert not await hass.config_entries.async_setup(entry.entry_id) +async def test_publish_without_mqtt(hass, caplog): + """Test publish without mqtt integration setup.""" + with patch("homeassistant.components.ozw.OZWOptions") as ozw_options: + await setup_ozw(hass) + + send_message = ozw_options.call_args[1]["send_message"] + + mqtt_entries = hass.config_entries.async_entries("mqtt") + mqtt_entry = mqtt_entries[0] + await hass.config_entries.async_remove(mqtt_entry.entry_id) + await hass.async_block_till_done() + + assert not hass.config_entries.async_entries("mqtt") + + # Sending a message should not error with the MQTT integration not set up. + send_message("test_topic", "test_payload") + + assert "MQTT integration is not set up" in caplog.text + + async def test_unload_entry(hass, generic_data, switch_msg, caplog): """Test unload the config entry.""" entry = MockConfigEntry(