mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Warn when receiving message on illegal MQTT discovery topic (#52106)
* Warn when receiving message on illegal MQTT discovery topic * Fix test
This commit is contained in:
parent
db5bf8ab23
commit
2351f2d95e
@ -95,6 +95,10 @@ async def async_start( # noqa: C901
|
|||||||
match = TOPIC_MATCHER.match(topic_trimmed)
|
match = TOPIC_MATCHER.match(topic_trimmed)
|
||||||
|
|
||||||
if not match:
|
if not match:
|
||||||
|
if topic_trimmed.endswith("config"):
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Received message on illegal discovery topic '%s'", topic
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
component, node_id, object_id = match.groups()
|
component, node_id, object_id = match.groups()
|
||||||
|
@ -55,18 +55,30 @@ async def test_subscribing_config_topic(hass, mqtt_mock):
|
|||||||
assert discovery_topic + "/+/+/+/config" in topics
|
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."""
|
"""Test sending to invalid topic."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.mqtt.discovery.async_dispatcher_send"
|
"homeassistant.components.mqtt.discovery.async_dispatcher_send"
|
||||||
) as mock_dispatcher_send:
|
) as mock_dispatcher_send:
|
||||||
mock_dispatcher_send = AsyncMock(return_value=None)
|
mock_dispatcher_send = AsyncMock(return_value=None)
|
||||||
|
|
||||||
async_fire_mqtt_message(
|
async_fire_mqtt_message(hass, topic, "{}")
|
||||||
hass, "homeassistant/binary_sensor/bla/not_config", "{}"
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert not mock_dispatcher_send.called
|
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):
|
async def test_invalid_json(hass, mqtt_mock, caplog):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user