mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Fix Shelly diagnostics when the device is not initialized (#195967)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d7bf1d2b06
commit
971c3728c4
@@ -19,6 +19,7 @@ async def async_get_config_entry_diagnostics(
|
||||
"""Return diagnostics for a config entry."""
|
||||
shelly_entry_data = entry.runtime_data
|
||||
|
||||
device_info: str | dict = "not initialized"
|
||||
device_settings: str | dict = "not initialized"
|
||||
device_status: str | dict = "not initialized"
|
||||
bluetooth: str | dict = "not initialized"
|
||||
@@ -57,9 +58,7 @@ async def async_get_config_entry_diagnostics(
|
||||
if block_coordinator.device.last_error:
|
||||
last_error = repr(block_coordinator.device.last_error)
|
||||
|
||||
else:
|
||||
rpc_coordinator = shelly_entry_data.rpc
|
||||
assert rpc_coordinator
|
||||
elif rpc_coordinator := shelly_entry_data.rpc:
|
||||
device_info = {
|
||||
"name": rpc_coordinator.name,
|
||||
"model": rpc_coordinator.model,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Tests for Shelly diagnostics platform."""
|
||||
|
||||
from copy import deepcopy
|
||||
from unittest.mock import ANY, Mock, PropertyMock
|
||||
from unittest.mock import ANY, AsyncMock, Mock, PropertyMock
|
||||
|
||||
from aioshelly.ble.const import BLE_SCAN_RESULT_EVENT
|
||||
from aioshelly.const import MODEL_25
|
||||
@@ -17,6 +17,7 @@ from homeassistant.components.shelly.const import (
|
||||
BLEScannerMode,
|
||||
)
|
||||
from homeassistant.components.shelly.diagnostics import TO_REDACT
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import init_integration, inject_rpc_device_event
|
||||
@@ -239,3 +240,39 @@ async def test_rpc_config_entry_diagnostics_no_ws(
|
||||
result = await get_diagnostics_for_config_entry(hass, hass_client, entry)
|
||||
|
||||
assert result["device_settings"]["ws_outbound"] == "not supported"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("gen", [1, 2])
|
||||
async def test_config_entry_diagnostics_setup_retry(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
mock_block_device: Mock,
|
||||
mock_rpc_device: Mock,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
gen: int,
|
||||
) -> None:
|
||||
"""Test config entry diagnostics when the device is not initialized."""
|
||||
monkeypatch.setattr(
|
||||
mock_block_device, "initialize", AsyncMock(side_effect=DeviceConnectionError)
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
mock_rpc_device, "initialize", AsyncMock(side_effect=DeviceConnectionError)
|
||||
)
|
||||
entry = await init_integration(hass, gen)
|
||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
entry_dict = entry.as_dict()
|
||||
entry_dict["data"].update(
|
||||
{key: REDACTED for key in TO_REDACT if key in entry_dict["data"]}
|
||||
)
|
||||
|
||||
result = await get_diagnostics_for_config_entry(hass, hass_client, entry)
|
||||
|
||||
assert result == {
|
||||
"entry": entry_dict | {"discovery_keys": {}},
|
||||
"bluetooth": "not initialized",
|
||||
"device_info": "not initialized",
|
||||
"device_settings": "not initialized",
|
||||
"device_status": "not initialized",
|
||||
"last_error": "not initialized",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user