diff --git a/homeassistant/components/mqtt/debug_info.py b/homeassistant/components/mqtt/debug_info.py index e462d76fa31..3e32d301b70 100644 --- a/homeassistant/components/mqtt/debug_info.py +++ b/homeassistant/components/mqtt/debug_info.py @@ -139,7 +139,7 @@ async def info_for_device(hass, device_id): "topic": topic, "messages": [ { - "payload": msg.payload, + "payload": str(msg.payload), "qos": msg.qos, "retain": msg.retain, "time": msg.timestamp, diff --git a/tests/components/mqtt/test_common.py b/tests/components/mqtt/test_common.py index 593d08f4c87..78c37b1105a 100644 --- a/tests/components/mqtt/test_common.py +++ b/tests/components/mqtt/test_common.py @@ -1222,7 +1222,7 @@ async def help_test_entity_debug_info_message( "topic": topic, "messages": [ { - "payload": payload, + "payload": str(payload), "qos": 0, "retain": False, "time": start_dt, diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index a71c332b70b..9101b895218 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -3,7 +3,7 @@ import asyncio from datetime import datetime, timedelta import json import ssl -from unittest.mock import AsyncMock, MagicMock, call, mock_open, patch +from unittest.mock import ANY, AsyncMock, MagicMock, call, mock_open, patch import pytest import voluptuous as vol @@ -1540,6 +1540,68 @@ async def test_mqtt_ws_get_device_debug_info( assert response["result"] == expected_result +async def test_mqtt_ws_get_device_debug_info_binary( + hass, device_reg, hass_ws_client, mqtt_mock +): + """Test MQTT websocket device debug info.""" + config = { + "device": {"identifiers": ["0AFFD2"]}, + "platform": "mqtt", + "topic": "foobar/image", + "unique_id": "unique", + } + data = json.dumps(config) + + async_fire_mqtt_message(hass, "homeassistant/camera/bla/config", data) + await hass.async_block_till_done() + + # Verify device entry is created + device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) + assert device_entry is not None + + small_png = ( + b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x04\x00\x00\x00\x04\x08\x06" + b"\x00\x00\x00\xa9\xf1\x9e~\x00\x00\x00\x13IDATx\xdac\xfc\xcf\xc0P\xcf\x80\x04" + b"\x18I\x17\x00\x00\xf2\xae\x05\xfdR\x01\xc2\xde\x00\x00\x00\x00IEND\xaeB`\x82" + ) + async_fire_mqtt_message(hass, "foobar/image", small_png) + await hass.async_block_till_done() + + client = await hass_ws_client(hass) + await client.send_json( + {"id": 5, "type": "mqtt/device/debug_info", "device_id": device_entry.id} + ) + response = await client.receive_json() + assert response["success"] + expected_result = { + "entities": [ + { + "entity_id": "camera.mqtt_camera", + "subscriptions": [ + { + "topic": "foobar/image", + "messages": [ + { + "payload": str(small_png), + "qos": 0, + "retain": False, + "time": ANY, + "topic": "foobar/image", + } + ], + } + ], + "discovery_data": { + "payload": config, + "topic": "homeassistant/camera/bla/config", + }, + } + ], + "triggers": [], + } + assert response["result"] == expected_result + + async def test_debug_info_multiple_devices(hass, mqtt_mock): """Test we get correct debug_info when multiple devices are present.""" devices = [