Add MQTT integration discovery (#41332)
* Add MQTT integration discovery * Add script/hassfest/mqtt.py * Unsubscribe if config entry exists * Add homeassistant/generated/mqtt.py * Fix bad loop * Improve tests * Improve tests * Apply suggestions from code review Co-authored-by: Fabian Affolter <mail@fabian-affolter.ch> * Prevent initiating multiple config flows Co-authored-by: Fabian Affolter <mail@fabian-affolter.ch>
This commit is contained in:
1 parent
3f263d5cbe
commit
343e5d64b8
21 files changed
+400
-24
No files matched your search
@@ -4,6 +4,7 @@ import re
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import mqtt
|
||||
from homeassistant.components.mqtt.abbreviations import (
|
||||
ABBREVIATIONS,
|
||||
@@ -13,7 +14,12 @@ from homeassistant.components.mqtt.discovery import ALREADY_DISCOVERED, async_st
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
|
||||
from tests.async_mock import AsyncMock, patch
|
||||
from tests.common import async_fire_mqtt_message, mock_device_registry, mock_registry
|
||||
from tests.common import (
|
||||
async_fire_mqtt_message,
|
||||
mock_device_registry,
|
||||
mock_entity_platform,
|
||||
mock_registry,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -436,3 +442,75 @@ async def test_complex_discovery_topic_prefix(hass, mqtt_mock, caplog):
|
||||
assert state is not None
|
||||
assert state.name == "Beer"
|
||||
assert ("binary_sensor", "node1 object1") in hass.data[ALREADY_DISCOVERED]
|
||||
|
||||
|
||||
async def test_mqtt_integration_discovery_subscribe_unsubscribe(
|
||||
hass, mqtt_client_mock, mqtt_mock
|
||||
):
|
||||
"""Check MQTT integration discovery subscribe and unsubscribe."""
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
entry = hass.config_entries.async_entries("mqtt")[0]
|
||||
mqtt_mock().connected = True
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.mqtt.discovery.async_get_mqtt",
|
||||
return_value={"comp": ["comp/discovery/#"]},
|
||||
):
|
||||
await async_start(hass, "homeassistant", entry)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mqtt_client_mock.subscribe.assert_any_call("comp/discovery/#", 0)
|
||||
assert not mqtt_client_mock.unsubscribe.called
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
"""Test flow."""
|
||||
|
||||
async def async_step_mqtt(self, discovery_info):
|
||||
"""Test mqtt step."""
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
mqtt_client_mock.subscribe.assert_any_call("comp/discovery/#", 0)
|
||||
assert not mqtt_client_mock.unsubscribe.called
|
||||
|
||||
async_fire_mqtt_message(hass, "comp/discovery/bla/config", "")
|
||||
await hass.async_block_till_done()
|
||||
mqtt_client_mock.unsubscribe.assert_called_once_with("comp/discovery/#")
|
||||
mqtt_client_mock.unsubscribe.reset_mock()
|
||||
|
||||
async_fire_mqtt_message(hass, "comp/discovery/bla/config", "")
|
||||
await hass.async_block_till_done()
|
||||
assert not mqtt_client_mock.unsubscribe.called
|
||||
|
||||
|
||||
async def test_mqtt_discovery_unsubscribe_once(hass, mqtt_client_mock, mqtt_mock):
|
||||
"""Check MQTT integration discovery unsubscribe once."""
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
entry = hass.config_entries.async_entries("mqtt")[0]
|
||||
mqtt_mock().connected = True
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.mqtt.discovery.async_get_mqtt",
|
||||
return_value={"comp": ["comp/discovery/#"]},
|
||||
):
|
||||
await async_start(hass, "homeassistant", entry)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mqtt_client_mock.subscribe.assert_any_call("comp/discovery/#", 0)
|
||||
assert not mqtt_client_mock.unsubscribe.called
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
"""Test flow."""
|
||||
|
||||
async def async_step_mqtt(self, discovery_info):
|
||||
"""Test mqtt step."""
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
async_fire_mqtt_message(hass, "comp/discovery/bla/config", "")
|
||||
async_fire_mqtt_message(hass, "comp/discovery/bla/config", "")
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
mqtt_client_mock.unsubscribe.assert_called_once_with("comp/discovery/#")
|
||||
@@ -1,8 +1,47 @@
|
||||
"""Test config flow."""
|
||||
from homeassistant.components.mqtt.models import Message
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_mqtt_abort_if_existing_entry(hass, mqtt_mock):
|
||||
"""Check MQTT flow aborts when an entry already exist."""
|
||||
MockConfigEntry(domain="tasmota").add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"tasmota", context={"source": "mqtt"}
|
||||
)
|
||||
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "single_instance_allowed"
|
||||
|
||||
|
||||
async def test_mqtt_abort_invalid_topic(hass, mqtt_mock):
|
||||
"""Check MQTT flow aborts if discovery topic is invalid."""
|
||||
discovery_info = Message("", "", 0, False, subscribed_topic="custom_prefix/##")
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"tasmota", context={"source": "mqtt"}, data=discovery_info
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "invalid_discovery_info"
|
||||
|
||||
|
||||
async def test_mqtt_setup(hass, mqtt_mock) -> None:
|
||||
"""Test we can finish a config flow through MQTT with custom prefix."""
|
||||
discovery_info = Message("", "", 0, False, subscribed_topic="custom_prefix/123/#")
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"tasmota", context={"source": "mqtt"}, data=discovery_info
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["result"].data == {
|
||||
"discovery_prefix": "custom_prefix/123",
|
||||
}
|
||||
|
||||
|
||||
async def test_user_setup(hass, mqtt_mock):
|
||||
"""Test we can finish a config flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -35,15 +74,32 @@ async def test_user_setup_advanced(hass, mqtt_mock):
|
||||
}
|
||||
|
||||
|
||||
async def test_user_setup_invalid_topic_prefix(hass, mqtt_mock):
|
||||
"""Test if connection cannot be made."""
|
||||
async def test_user_setup_advanced_strip_wildcard(hass, mqtt_mock):
|
||||
"""Test we can finish a config flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"tasmota", context={"source": "user", "show_advanced_options": True}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {"discovery_prefix": "tasmota/config/#"}
|
||||
result["flow_id"], {"discovery_prefix": "test_tasmota/discovery/#"}
|
||||
)
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["result"].data == {
|
||||
"discovery_prefix": "test_tasmota/discovery",
|
||||
}
|
||||
|
||||
|
||||
async def test_user_setup_invalid_topic_prefix(hass, mqtt_mock):
|
||||
"""Test abort on invalid discovery topic."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"tasmota", context={"source": "user", "show_advanced_options": True}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {"discovery_prefix": "tasmota/config/##"}
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
|
||||
Reference in new issue
Block a user