Warn when receiving message on illegal MQTT discovery topic (#52106)

* Warn when receiving message on illegal MQTT discovery topic

* Fix test
pull/50296/head^2
Erik Montnemery 2021-06-23 15:53:17 +02:00 committed by GitHub
parent db5bf8ab23
commit 2351f2d95e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -95,6 +95,10 @@ async def async_start( # noqa: C901
match = TOPIC_MATCHER.match(topic_trimmed)
if not match:
if topic_trimmed.endswith("config"):
_LOGGER.warning(
"Received message on illegal discovery topic '%s'", topic
)
return
component, node_id, object_id = match.groups()

View File

@ -55,18 +55,30 @@ async def test_subscribing_config_topic(hass, mqtt_mock):
assert discovery_topic + "/+/+/+/config" in topics
async def test_invalid_topic(hass, mqtt_mock):
@pytest.mark.parametrize(
"topic, log",
[
("homeassistant/binary_sensor/bla/not_config", False),
("homeassistant/binary_sensor/rörkrökare/config", True),
],
)
async def test_invalid_topic(hass, mqtt_mock, caplog, topic, log):
"""Test sending to invalid topic."""
with patch(
"homeassistant.components.mqtt.discovery.async_dispatcher_send"
) as mock_dispatcher_send:
mock_dispatcher_send = AsyncMock(return_value=None)
async_fire_mqtt_message(
hass, "homeassistant/binary_sensor/bla/not_config", "{}"
)
async_fire_mqtt_message(hass, topic, "{}")
await hass.async_block_till_done()
assert not mock_dispatcher_send.called
if log:
assert (
f"Received message on illegal discovery topic '{topic}'" in caplog.text
)
else:
assert "Received message on illegal discovery topic'" not in caplog.text
caplog.clear()
async def test_invalid_json(hass, mqtt_mock, caplog):