Include QoS and retain in MQTT debug info (#35011)

This commit is contained in:
Erik Montnemery 2020-05-01 17:41:57 +02:00 committed by GitHub
parent ea70d71e8f
commit 2b13a8cde4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 6 deletions

View file

@ -137,7 +137,13 @@ async def info_for_device(hass, device_id):
{ {
"topic": topic, "topic": topic,
"messages": [ "messages": [
{"payload": msg.payload, "time": msg.timestamp, "topic": msg.topic} {
"payload": msg.payload,
"qos": msg.qos,
"retain": msg.retain,
"time": msg.timestamp,
"topic": msg.topic,
}
for msg in list(subscription["messages"]) for msg in list(subscription["messages"])
], ],
} }

View file

@ -601,7 +601,13 @@ async def help_test_entity_debug_info_max_messages(hass, mqtt_mock, domain, conf
== debug_info.STORED_MESSAGES == debug_info.STORED_MESSAGES
) )
messages = [ messages = [
{"topic": "test-topic", "payload": f"{i}", "time": start_dt} {
"payload": f"{i}",
"qos": 0,
"retain": False,
"time": start_dt,
"topic": "test-topic",
}
for i in range(1, debug_info.STORED_MESSAGES + 1) for i in range(1, debug_info.STORED_MESSAGES + 1)
] ]
assert {"topic": "test-topic", "messages": messages} in debug_info_data["entities"][ assert {"topic": "test-topic", "messages": messages} in debug_info_data["entities"][
@ -656,7 +662,15 @@ async def help_test_entity_debug_info_message(
assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1 assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1
assert { assert {
"topic": topic, "topic": topic,
"messages": [{"topic": topic, "payload": payload, "time": start_dt}], "messages": [
{
"payload": payload,
"qos": 0,
"retain": False,
"time": start_dt,
"topic": topic,
}
],
} in debug_info_data["entities"][0]["subscriptions"] } in debug_info_data["entities"][0]["subscriptions"]

View file

@ -1291,7 +1291,15 @@ async def test_debug_info_wildcard(hass, mqtt_mock):
assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1 assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1
assert { assert {
"topic": "sensor/#", "topic": "sensor/#",
"messages": [{"topic": "sensor/abc", "payload": "123", "time": start_dt}], "messages": [
{
"payload": "123",
"qos": 0,
"retain": False,
"time": start_dt,
"topic": "sensor/abc",
}
],
} in debug_info_data["entities"][0]["subscriptions"] } in debug_info_data["entities"][0]["subscriptions"]
@ -1338,8 +1346,20 @@ async def test_debug_info_filter_same(hass, mqtt_mock):
assert { assert {
"topic": "sensor/#", "topic": "sensor/#",
"messages": [ "messages": [
{"payload": "123", "time": dt1, "topic": "sensor/abc"}, {
{"payload": "123", "time": dt2, "topic": "sensor/abc"}, "payload": "123",
"qos": 0,
"retain": False,
"time": dt1,
"topic": "sensor/abc",
},
{
"payload": "123",
"qos": 0,
"retain": False,
"time": dt2,
"topic": "sensor/abc",
},
], ],
} == debug_info_data["entities"][0]["subscriptions"][0] } == debug_info_data["entities"][0]["subscriptions"][0]
@ -1382,6 +1402,8 @@ async def test_debug_info_same_topic(hass, mqtt_mock):
assert len(debug_info_data["entities"][0]["subscriptions"]) == 1 assert len(debug_info_data["entities"][0]["subscriptions"]) == 1
assert { assert {
"payload": "123", "payload": "123",
"qos": 0,
"retain": False,
"time": start_dt, "time": start_dt,
"topic": "sensor/status", "topic": "sensor/status",
} in debug_info_data["entities"][0]["subscriptions"][0]["messages"] } in debug_info_data["entities"][0]["subscriptions"][0]["messages"]
@ -1395,3 +1417,63 @@ async def test_debug_info_same_topic(hass, mqtt_mock):
with patch("homeassistant.util.dt.utcnow") as dt_utcnow: with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
dt_utcnow.return_value = start_dt dt_utcnow.return_value = start_dt
async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False) async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False)
async def test_debug_info_qos_retain(hass, mqtt_mock):
"""Test debug info."""
config = {
"device": {"identifiers": ["helloworld"]},
"platform": "mqtt",
"name": "test",
"state_topic": "sensor/#",
"unique_id": "veryunique",
}
entry = MockConfigEntry(domain=mqtt.DOMAIN)
entry.add_to_hass(hass)
await async_start(hass, "homeassistant", {}, entry)
registry = await hass.helpers.device_registry.async_get_registry()
data = json.dumps(config)
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)
await hass.async_block_till_done()
device = registry.async_get_device({("mqtt", "helloworld")}, set())
assert device is not None
debug_info_data = await debug_info.info_for_device(hass, device.id)
assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1
assert {"topic": "sensor/#", "messages": []} in debug_info_data["entities"][0][
"subscriptions"
]
start_dt = datetime(2019, 1, 1, 0, 0, 0)
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
dt_utcnow.return_value = start_dt
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=0, retain=False)
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=1, retain=True)
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=2, retain=False)
debug_info_data = await debug_info.info_for_device(hass, device.id)
assert len(debug_info_data["entities"][0]["subscriptions"]) == 1
assert {
"payload": "123",
"qos": 0,
"retain": False,
"time": start_dt,
"topic": "sensor/abc",
} in debug_info_data["entities"][0]["subscriptions"][0]["messages"]
assert {
"payload": "123",
"qos": 1,
"retain": True,
"time": start_dt,
"topic": "sensor/abc",
} in debug_info_data["entities"][0]["subscriptions"][0]["messages"]
assert {
"payload": "123",
"qos": 2,
"retain": False,
"time": start_dt,
"topic": "sensor/abc",
} in debug_info_data["entities"][0]["subscriptions"][0]["messages"]