diff --git a/homeassistant/components/mqtt/debug_info.py b/homeassistant/components/mqtt/debug_info.py index 86850c61638..75e4b53a191 100644 --- a/homeassistant/components/mqtt/debug_info.py +++ b/homeassistant/components/mqtt/debug_info.py @@ -137,7 +137,13 @@ async def info_for_device(hass, device_id): { "topic": topic, "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"]) ], } diff --git a/tests/components/mqtt/test_common.py b/tests/components/mqtt/test_common.py index 1199aaa40c7..949d77c244d 100644 --- a/tests/components/mqtt/test_common.py +++ b/tests/components/mqtt/test_common.py @@ -601,7 +601,13 @@ async def help_test_entity_debug_info_max_messages(hass, mqtt_mock, domain, conf == debug_info.STORED_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) ] 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 { "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"] diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 672ff127b4d..28cca6a856a 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -1291,7 +1291,15 @@ async def test_debug_info_wildcard(hass, mqtt_mock): assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1 assert { "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"] @@ -1338,8 +1346,20 @@ async def test_debug_info_filter_same(hass, mqtt_mock): assert { "topic": "sensor/#", "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] @@ -1382,6 +1402,8 @@ async def test_debug_info_same_topic(hass, mqtt_mock): assert len(debug_info_data["entities"][0]["subscriptions"]) == 1 assert { "payload": "123", + "qos": 0, + "retain": False, "time": start_dt, "topic": "sensor/status", } 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: dt_utcnow.return_value = start_dt 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"]