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 GitHub
parent 31e9de3b95
commit d8ab2debfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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_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,
}

View file

@ -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()",
}