Fix Tasmota MQTT discovery flow (#55140)

pull/55145/head
Erik Montnemery 2021-08-24 13:23:26 +02:00 committed by GitHub
parent 547ede1e91
commit 828d862339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View File

@ -1,12 +1,12 @@
"""Config flow for Tasmota.""" """Config flow for Tasmota."""
from __future__ import annotations from __future__ import annotations
from typing import Any, cast from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.mqtt import ReceiveMessage, valid_subscribe_topic from homeassistant.components.mqtt import valid_subscribe_topic
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.typing import DiscoveryInfoType from homeassistant.helpers.typing import DiscoveryInfoType
@ -30,7 +30,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
await self.async_set_unique_id(DOMAIN) await self.async_set_unique_id(DOMAIN)
# Validate the topic, will throw if it fails # Validate the topic, will throw if it fails
prefix = cast(ReceiveMessage, discovery_info).subscribed_topic prefix = discovery_info["subscribed_topic"]
if prefix.endswith("/#"): if prefix.endswith("/#"):
prefix = prefix[:-2] prefix = prefix[:-2]
try: try:

View File

@ -1,6 +1,5 @@
"""Test config flow.""" """Test config flow."""
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.mqtt.models import ReceiveMessage
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -19,9 +18,14 @@ async def test_mqtt_abort_if_existing_entry(hass, mqtt_mock):
async def test_mqtt_abort_invalid_topic(hass, mqtt_mock): async def test_mqtt_abort_invalid_topic(hass, mqtt_mock):
"""Check MQTT flow aborts if discovery topic is invalid.""" """Check MQTT flow aborts if discovery topic is invalid."""
discovery_info = ReceiveMessage( discovery_info = {
"", "", 0, False, subscribed_topic="custom_prefix/##" "topic": "",
) "payload": "",
"qos": 0,
"retain": False,
"subscribed_topic": "custom_prefix/##",
"timestamp": None,
}
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
"tasmota", context={"source": config_entries.SOURCE_MQTT}, data=discovery_info "tasmota", context={"source": config_entries.SOURCE_MQTT}, data=discovery_info
) )
@ -31,9 +35,14 @@ async def test_mqtt_abort_invalid_topic(hass, mqtt_mock):
async def test_mqtt_setup(hass, mqtt_mock) -> None: async def test_mqtt_setup(hass, mqtt_mock) -> None:
"""Test we can finish a config flow through MQTT with custom prefix.""" """Test we can finish a config flow through MQTT with custom prefix."""
discovery_info = ReceiveMessage( discovery_info = {
"", "", 0, False, subscribed_topic="custom_prefix/123/#" "topic": "",
) "payload": "",
"qos": 0,
"retain": False,
"subscribed_topic": "custom_prefix/123/#",
"timestamp": None,
}
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
"tasmota", context={"source": config_entries.SOURCE_MQTT}, data=discovery_info "tasmota", context={"source": config_entries.SOURCE_MQTT}, data=discovery_info
) )