diff --git a/homeassistant/components/shelly/diagnostics.py b/homeassistant/components/shelly/diagnostics.py index fcdc5ab3d036..956bec2f52e3 100644 --- a/homeassistant/components/shelly/diagnostics.py +++ b/homeassistant/components/shelly/diagnostics.py @@ -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, diff --git a/tests/components/shelly/test_diagnostics.py b/tests/components/shelly/test_diagnostics.py index 7b40dcff33b0..59f39a539a0b 100644 --- a/tests/components/shelly/test_diagnostics.py +++ b/tests/components/shelly/test_diagnostics.py @@ -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", + }