Add last_error reporting to Shelly diagnostics (#120595)

This commit is contained in:
Shay Levy 2024-06-26 21:35:23 +03:00 committed by Franck Nijhof
parent 80e70993c8
commit b5c34808e6
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 22 additions and 1 deletions

View File

@ -24,6 +24,8 @@ async def async_get_config_entry_diagnostics(
device_settings: str | dict = "not initialized" device_settings: str | dict = "not initialized"
device_status: str | dict = "not initialized" device_status: str | dict = "not initialized"
bluetooth: str | dict = "not initialized" bluetooth: str | dict = "not initialized"
last_error: str = "not initialized"
if shelly_entry_data.block: if shelly_entry_data.block:
block_coordinator = shelly_entry_data.block block_coordinator = shelly_entry_data.block
assert block_coordinator assert block_coordinator
@ -55,6 +57,10 @@ async def async_get_config_entry_diagnostics(
"uptime", "uptime",
] ]
} }
if block_coordinator.device.last_error:
last_error = repr(block_coordinator.device.last_error)
else: else:
rpc_coordinator = shelly_entry_data.rpc rpc_coordinator = shelly_entry_data.rpc
assert rpc_coordinator assert rpc_coordinator
@ -79,6 +85,9 @@ async def async_get_config_entry_diagnostics(
"scanner": await scanner.async_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): if isinstance(device_status, dict):
device_status = async_redact_data(device_status, ["ssid"]) device_status = async_redact_data(device_status, ["ssid"])
@ -87,5 +96,6 @@ async def async_get_config_entry_diagnostics(
"device_info": device_info, "device_info": device_info,
"device_settings": device_settings, "device_settings": device_settings,
"device_status": device_status, "device_status": device_status,
"last_error": last_error,
"bluetooth": bluetooth, "bluetooth": bluetooth,
} }

View File

@ -1,9 +1,10 @@
"""Tests for Shelly diagnostics platform.""" """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.ble.const import BLE_SCAN_RESULT_EVENT
from aioshelly.const import MODEL_25 from aioshelly.const import MODEL_25
from aioshelly.exceptions import DeviceConnectionError
import pytest import pytest
from homeassistant.components.diagnostics import REDACTED 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"]} {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) result = await get_diagnostics_for_config_entry(hass, hass_client, entry)
assert result == { assert result == {
@ -48,6 +53,7 @@ async def test_block_config_entry_diagnostics(
}, },
"device_settings": {"coiot": {"update_period": 15}}, "device_settings": {"coiot": {"update_period": 15}},
"device_status": MOCK_STATUS_COAP, "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"]} {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) result = await get_diagnostics_for_config_entry(hass, hass_client, entry)
assert result == { assert result == {
@ -152,4 +162,5 @@ async def test_rpc_config_entry_diagnostics(
}, },
"wifi": {"rssi": -63}, "wifi": {"rssi": -63},
}, },
"last_error": "DeviceConnectionError()",
} }