diff --git a/homeassistant/components/shelly/diagnostics.py b/homeassistant/components/shelly/diagnostics.py index db69abc8f55..e70b76a7c00 100644 --- a/homeassistant/components/shelly/diagnostics.py +++ b/homeassistant/components/shelly/diagnostics.py @@ -24,6 +24,8 @@ async def async_get_config_entry_diagnostics( device_settings: str | dict = "not initialized" device_status: str | dict = "not initialized" bluetooth: str | dict = "not initialized" + last_error: str = "not initialized" + if shelly_entry_data.block: block_coordinator = shelly_entry_data.block assert block_coordinator @@ -55,6 +57,10 @@ async def async_get_config_entry_diagnostics( "uptime", ] } + + if block_coordinator.device.last_error: + last_error = repr(block_coordinator.device.last_error) + else: rpc_coordinator = shelly_entry_data.rpc assert rpc_coordinator @@ -79,6 +85,9 @@ async def async_get_config_entry_diagnostics( "scanner": await scanner.async_diagnostics(), } + if rpc_coordinator.device.last_error: + last_error = repr(rpc_coordinator.device.last_error) + if isinstance(device_status, dict): device_status = async_redact_data(device_status, ["ssid"]) @@ -87,5 +96,6 @@ async def async_get_config_entry_diagnostics( "device_info": device_info, "device_settings": device_settings, "device_status": device_status, + "last_error": last_error, "bluetooth": bluetooth, } diff --git a/tests/components/shelly/test_diagnostics.py b/tests/components/shelly/test_diagnostics.py index f7f238f3327..4fc8ea6ca8f 100644 --- a/tests/components/shelly/test_diagnostics.py +++ b/tests/components/shelly/test_diagnostics.py @@ -1,9 +1,10 @@ """Tests for Shelly diagnostics platform.""" -from unittest.mock import ANY, Mock +from unittest.mock import ANY, Mock, PropertyMock from aioshelly.ble.const import BLE_SCAN_RESULT_EVENT from aioshelly.const import MODEL_25 +from aioshelly.exceptions import DeviceConnectionError import pytest from homeassistant.components.diagnostics import REDACTED @@ -36,6 +37,10 @@ async def test_block_config_entry_diagnostics( {key: REDACTED for key in TO_REDACT if key in entry_dict["data"]} ) + type(mock_block_device).last_error = PropertyMock( + return_value=DeviceConnectionError() + ) + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) assert result == { @@ -48,6 +53,7 @@ async def test_block_config_entry_diagnostics( }, "device_settings": {"coiot": {"update_period": 15}}, "device_status": MOCK_STATUS_COAP, + "last_error": "DeviceConnectionError()", } @@ -91,6 +97,10 @@ async def test_rpc_config_entry_diagnostics( {key: REDACTED for key in TO_REDACT if key in entry_dict["data"]} ) + type(mock_rpc_device).last_error = PropertyMock( + return_value=DeviceConnectionError() + ) + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) assert result == { @@ -152,4 +162,5 @@ async def test_rpc_config_entry_diagnostics( }, "wifi": {"rssi": -63}, }, + "last_error": "DeviceConnectionError()", }