Fix regression in MQTT discovery (#58684)
* Fix regression in MQTT discovery * Update testpull/58701/head
parent
72d7817dbf
commit
f3bd13d179
|
@ -10,6 +10,7 @@ import time
|
||||||
from homeassistant.const import CONF_DEVICE, CONF_PLATFORM
|
from homeassistant.const import CONF_DEVICE, CONF_PLATFORM
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import RESULT_TYPE_ABORT
|
from homeassistant.data_entry_flow import RESULT_TYPE_ABORT
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import (
|
||||||
async_dispatcher_connect,
|
async_dispatcher_connect,
|
||||||
async_dispatcher_send,
|
async_dispatcher_send,
|
||||||
|
@ -141,7 +142,9 @@ async def async_start( # noqa: C901
|
||||||
if value[-1] == TOPIC_BASE and key.endswith("topic"):
|
if value[-1] == TOPIC_BASE and key.endswith("topic"):
|
||||||
payload[key] = f"{value[:-1]}{base}"
|
payload[key] = f"{value[:-1]}{base}"
|
||||||
if payload.get(CONF_AVAILABILITY):
|
if payload.get(CONF_AVAILABILITY):
|
||||||
for availability_conf in payload[CONF_AVAILABILITY]:
|
for availability_conf in cv.ensure_list(payload[CONF_AVAILABILITY]):
|
||||||
|
if not isinstance(availability_conf, dict):
|
||||||
|
continue
|
||||||
if topic := availability_conf.get(CONF_TOPIC):
|
if topic := availability_conf.get(CONF_TOPIC):
|
||||||
if topic[0] == TOPIC_BASE:
|
if topic[0] == TOPIC_BASE:
|
||||||
availability_conf[CONF_TOPIC] = f"{base}{topic[1:]}"
|
availability_conf[CONF_TOPIC] = f"{base}{topic[1:]}"
|
||||||
|
|
|
@ -504,6 +504,76 @@ async def test_discovery_expansion(hass, mqtt_mock, caplog):
|
||||||
assert state.state == STATE_UNAVAILABLE
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
|
||||||
|
async def test_discovery_expansion_2(hass, mqtt_mock, caplog):
|
||||||
|
"""Test expansion of abbreviated discovery payload."""
|
||||||
|
data = (
|
||||||
|
'{ "~": "some/base/topic",'
|
||||||
|
' "name": "DiscoveryExpansionTest1",'
|
||||||
|
' "stat_t": "test_topic/~",'
|
||||||
|
' "cmd_t": "~/test_topic",'
|
||||||
|
' "availability": {'
|
||||||
|
' "topic":"~/avail_item1",'
|
||||||
|
' "payload_available": "available",'
|
||||||
|
' "payload_not_available": "not_available"'
|
||||||
|
" },"
|
||||||
|
' "dev":{'
|
||||||
|
' "ids":["5706DF"],'
|
||||||
|
' "name":"DiscoveryExpansionTest1 Device",'
|
||||||
|
' "mdl":"Generic",'
|
||||||
|
' "sw":"1.2.3.4",'
|
||||||
|
' "mf":"None",'
|
||||||
|
' "sa":"default_area"'
|
||||||
|
" }"
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, "homeassistant/switch/bla/config", data)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get("switch.DiscoveryExpansionTest1")
|
||||||
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, "some/base/topic/avail_item1", "available")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get("switch.DiscoveryExpansionTest1")
|
||||||
|
assert state is not None
|
||||||
|
assert state.name == "DiscoveryExpansionTest1"
|
||||||
|
assert ("switch", "bla") in hass.data[ALREADY_DISCOVERED]
|
||||||
|
assert state.state == STATE_OFF
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.no_fail_on_log_exception
|
||||||
|
async def test_discovery_expansion_3(hass, mqtt_mock, caplog):
|
||||||
|
"""Test expansion of broken discovery payload."""
|
||||||
|
data = (
|
||||||
|
'{ "~": "some/base/topic",'
|
||||||
|
' "name": "DiscoveryExpansionTest1",'
|
||||||
|
' "stat_t": "test_topic/~",'
|
||||||
|
' "cmd_t": "~/test_topic",'
|
||||||
|
' "availability": "incorrect",'
|
||||||
|
' "dev":{'
|
||||||
|
' "ids":["5706DF"],'
|
||||||
|
' "name":"DiscoveryExpansionTest1 Device",'
|
||||||
|
' "mdl":"Generic",'
|
||||||
|
' "sw":"1.2.3.4",'
|
||||||
|
' "mf":"None",'
|
||||||
|
' "sa":"default_area"'
|
||||||
|
" }"
|
||||||
|
"}"
|
||||||
|
)
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, "homeassistant/switch/bla/config", data)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get("switch.DiscoveryExpansionTest1") is None
|
||||||
|
# Make sure the malformed availability data does not trip up discovery by asserting
|
||||||
|
# there are schema valdiation errors in the log
|
||||||
|
assert (
|
||||||
|
"voluptuous.error.MultipleInvalid: expected a dictionary @ data['availability'][0]"
|
||||||
|
in caplog.text
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
ABBREVIATIONS_WHITE_LIST = [
|
ABBREVIATIONS_WHITE_LIST = [
|
||||||
# MQTT client/server/trigger settings
|
# MQTT client/server/trigger settings
|
||||||
"CONF_BIRTH_MESSAGE",
|
"CONF_BIRTH_MESSAGE",
|
||||||
|
|
Loading…
Reference in New Issue