Skip to content

Commit 00c3d21

Browse files
committed
Include qos and retain in MQTT debug info
1 parent 8402363 commit 00c3d21

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

homeassistant/components/mqtt/debug_info.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,13 @@ async def info_for_device(hass, device_id):
126126
{
127127
"topic": topic,
128128
"messages": [
129-
{"payload": msg.payload, "time": msg.timestamp, "topic": msg.topic}
129+
{
130+
"payload": msg.payload,
131+
"qos": msg.qos,
132+
"retain": msg.retain,
133+
"time": msg.timestamp,
134+
"topic": msg.topic,
135+
}
130136
for msg in list(messages)
131137
],
132138
}

tests/components/mqtt/test_common.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,13 @@ async def help_test_entity_debug_info_max_messages(hass, mqtt_mock, domain, conf
601601
== debug_info.STORED_MESSAGES
602602
)
603603
messages = [
604-
{"topic": "test-topic", "payload": f"{i}", "time": start_dt}
604+
{
605+
"payload": f"{i}",
606+
"qos": 0,
607+
"retain": False,
608+
"time": start_dt,
609+
"topic": "test-topic",
610+
}
605611
for i in range(1, debug_info.STORED_MESSAGES + 1)
606612
]
607613
assert {"topic": "test-topic", "messages": messages} in debug_info_data["entities"][
@@ -656,7 +662,15 @@ async def help_test_entity_debug_info_message(
656662
assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1
657663
assert {
658664
"topic": topic,
659-
"messages": [{"topic": topic, "payload": payload, "time": start_dt}],
665+
"messages": [
666+
{
667+
"payload": payload,
668+
"qos": 0,
669+
"retain": False,
670+
"time": start_dt,
671+
"topic": topic,
672+
}
673+
],
660674
} in debug_info_data["entities"][0]["subscriptions"]
661675

662676

tests/components/mqtt/test_init.py

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,15 @@ async def test_debug_info_wildcard(hass, mqtt_mock):
12571257
assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1
12581258
assert {
12591259
"topic": "sensor/#",
1260-
"messages": [{"topic": "sensor/abc", "payload": "123", "time": start_dt}],
1260+
"messages": [
1261+
{
1262+
"payload": "123",
1263+
"qos": 0,
1264+
"retain": False,
1265+
"time": start_dt,
1266+
"topic": "sensor/abc",
1267+
}
1268+
],
12611269
} in debug_info_data["entities"][0]["subscriptions"]
12621270

12631271

@@ -1304,7 +1312,79 @@ async def test_debug_info_filter_same(hass, mqtt_mock):
13041312
assert {
13051313
"topic": "sensor/#",
13061314
"messages": [
1307-
{"topic": "sensor/abc", "payload": "123", "time": dt1},
1308-
{"topic": "sensor/abc", "payload": "123", "time": dt2},
1315+
{
1316+
"payload": "123",
1317+
"qos": 0,
1318+
"retain": False,
1319+
"time": dt1,
1320+
"topic": "sensor/abc",
1321+
},
1322+
{
1323+
"payload": "123",
1324+
"qos": 0,
1325+
"retain": False,
1326+
"time": dt2,
1327+
"topic": "sensor/abc",
1328+
},
13091329
],
13101330
} == debug_info_data["entities"][0]["subscriptions"][0]
1331+
1332+
1333+
async def test_debug_info_qos_retain(hass, mqtt_mock):
1334+
"""Test debug info."""
1335+
config = {
1336+
"device": {"identifiers": ["helloworld"]},
1337+
"platform": "mqtt",
1338+
"name": "test",
1339+
"state_topic": "sensor/#",
1340+
"unique_id": "veryunique",
1341+
}
1342+
1343+
entry = MockConfigEntry(domain=mqtt.DOMAIN)
1344+
entry.add_to_hass(hass)
1345+
await async_start(hass, "homeassistant", {}, entry)
1346+
registry = await hass.helpers.device_registry.async_get_registry()
1347+
1348+
data = json.dumps(config)
1349+
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)
1350+
await hass.async_block_till_done()
1351+
1352+
device = registry.async_get_device({("mqtt", "helloworld")}, set())
1353+
assert device is not None
1354+
1355+
debug_info_data = await debug_info.info_for_device(hass, device.id)
1356+
assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1
1357+
assert {"topic": "sensor/#", "messages": []} in debug_info_data["entities"][0][
1358+
"subscriptions"
1359+
]
1360+
1361+
start_dt = datetime(2019, 1, 1, 0, 0, 0)
1362+
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
1363+
dt_utcnow.return_value = start_dt
1364+
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=0, retain=False)
1365+
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=1, retain=True)
1366+
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=2, retain=False)
1367+
1368+
debug_info_data = await debug_info.info_for_device(hass, device.id)
1369+
assert len(debug_info_data["entities"][0]["subscriptions"]) == 1
1370+
assert {
1371+
"payload": "123",
1372+
"qos": 0,
1373+
"retain": False,
1374+
"time": start_dt,
1375+
"topic": "sensor/abc",
1376+
} in debug_info_data["entities"][0]["subscriptions"][0]["messages"]
1377+
assert {
1378+
"payload": "123",
1379+
"qos": 1,
1380+
"retain": True,
1381+
"time": start_dt,
1382+
"topic": "sensor/abc",
1383+
} in debug_info_data["entities"][0]["subscriptions"][0]["messages"]
1384+
assert {
1385+
"payload": "123",
1386+
"qos": 2,
1387+
"retain": False,
1388+
"time": start_dt,
1389+
"topic": "sensor/abc",
1390+
} in debug_info_data["entities"][0]["subscriptions"][0]["messages"]

0 commit comments

Comments
 (0)