Fix mqtt check in ozw (#45709)

This commit is contained in:
Martin Hjelmare 2021-01-29 17:57:39 +01:00 committed by GitHub
parent b2789621bd
commit bcc9add0b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 9 deletions

View File

@ -21,7 +21,7 @@ from openzwavemqtt.util.mqtt_client import MQTTClient
from homeassistant.components import mqtt from homeassistant.components import mqtt
from homeassistant.components.hassio.handler import HassioAPIError 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.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady 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 manager_options["send_message"] = mqtt_client.send_message
else: 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") _LOGGER.error("MQTT integration is not set up")
return False return False
mqtt_entry = mqtt_entries[0] # MQTT integration only has one entry.
@callback @callback
def send_message(topic, payload): 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)) mqtt.async_publish(hass, topic, json.dumps(payload))
manager_options["send_message"] = send_message manager_options["send_message"] = send_message

View File

@ -97,7 +97,11 @@ class DomainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
This is the entry point for the logic that is needed This is the entry point for the logic that is needed
when this integration will depend on the MQTT integration. 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_abort(reason="mqtt_required")
return self._async_create_entry_from_vars() return self._async_create_entry_from_vars()

View File

@ -10,7 +10,8 @@ from tests.common import MockConfigEntry
async def setup_ozw(hass, entry=None, fixture=None): async def setup_ozw(hass, entry=None, fixture=None):
"""Set up OZW and load a dump.""" """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: if entry is None:
entry = MockConfigEntry( entry = MockConfigEntry(

View File

@ -4,9 +4,11 @@ from unittest.mock import patch
import pytest import pytest
from homeassistant.config_entries import ENTRY_STATE_LOADED
from .common import MQTTMessage 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 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" "homeassistant.components.hassio.async_get_addon_discovery_info"
) as get_addon_discovery_info: ) as get_addon_discovery_info:
yield 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

View File

@ -79,9 +79,8 @@ def mock_start_addon():
yield 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.""" """Test the user step creates an entry not on Supervisor."""
hass.config.components.add("mqtt")
await setup.async_setup_component(hass, "persistent_notification", {}) await setup.async_setup_component(hass, "persistent_notification", {})
with patch( with patch(
@ -128,9 +127,8 @@ async def test_one_instance_allowed(hass):
assert result["reason"] == "single_instance_allowed" 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.""" """Test opting out of add-on on Supervisor."""
hass.config.components.add("mqtt")
await setup.async_setup_component(hass, "persistent_notification", {}) await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(

View File

@ -37,6 +37,26 @@ async def test_setup_entry_without_mqtt(hass):
assert not await hass.config_entries.async_setup(entry.entry_id) 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): async def test_unload_entry(hass, generic_data, switch_msg, caplog):
"""Test unload the config entry.""" """Test unload the config entry."""
entry = MockConfigEntry( entry = MockConfigEntry(