From 249adbcf4bd2639dd97dc7280bc1d6422dd0c4ec Mon Sep 17 00:00:00 2001 From: tronikos Date: Wed, 25 Nov 2026 16:19:06 -0800 Subject: [PATCH] Settle SolarEdge Modbus battery probes when storage control is silent (#191778) --- .../components/solaredge_modbus/__init__.py | 23 +++++++++--- .../components/solaredge_modbus/test_init.py | 37 +++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/solaredge_modbus/__init__.py b/homeassistant/components/solaredge_modbus/__init__.py index 3020ada78eb2..f8d8b6be602d 100644 --- a/homeassistant/components/solaredge_modbus/__init__.py +++ b/homeassistant/components/solaredge_modbus/__init__.py @@ -208,13 +208,26 @@ async def async_setup_entry( ) +def _settleable_blocks(blocks: AbstractSet[str]) -> frozenset[str]: + """Return the silent blocks from `blocks` that can be settled as absent. + + Blocks that finding hardware depends on are never settled on their own, + except batteries when storage control is also silent (meaning the inverter + does not serve the 57xxx StorEdge register space at all). + """ + settled = frozenset(blocks) - DISCOVERY_SUBSYSTEMS + if SUBSYSTEM_STORAGE_CONTROL in settled and SUBSYSTEM_BATTERIES in blocks: + return settled | {SUBSYSTEM_BATTERIES} + return settled + + def _known_silent_blocks(entry: SolarEdgeModbusConfigEntry) -> frozenset[str]: """Return the blocks the last attachment check settled as silent. - Blocks that finding hardware depends on are never settled, so one stored - by anything else is not taken for absent either. + Blocks that finding hardware depends on are never settled on their own, so + one stored by anything else is not taken for absent either. """ - return frozenset(entry.data.get(CONF_SILENT_BLOCKS, ())) - DISCOVERY_SUBSYSTEMS + return _settleable_blocks(frozenset(entry.data.get(CONF_SILENT_BLOCKS, ()))) def _attachment_identities(solaredge: SolarEdge) -> frozenset[str]: @@ -285,9 +298,9 @@ async def _async_reload_when_attachments_change( # later removal until the entry loads again. Blocks that finding hardware # depends on are never settled: picking up what was wired in later is what # asking is for. - settled = ( + settled = _settleable_blocks( probed.unresponsive_blocks & solaredge.unresponsive_blocks - ) - DISCOVERY_SUBSYSTEMS + ) entry.runtime_data.settled_silent_blocks = settled # Kept with the entry for the next setup, before a reload below can start diff --git a/tests/components/solaredge_modbus/test_init.py b/tests/components/solaredge_modbus/test_init.py index a5b0014dabca..fe78c7cdc9fd 100644 --- a/tests/components/solaredge_modbus/test_init.py +++ b/tests/components/solaredge_modbus/test_init.py @@ -1354,3 +1354,40 @@ async def test_attachment_check_skipped_when_polling_disabled( await _tick_attachment_check(hass, freezer) mock_probe.assert_not_called() + + +async def test_batteries_settled_when_storage_control_is_silent( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + mock_config_entry: MockConfigEntry, + mock_modbus_unit: MockModbusUnit, +) -> None: + """Batteries are settled alongside storage control when both stay silent.""" + mock_modbus_unit.fail_read(SITE_CONTROL_REGISTER, ModbusTimeoutError("timed out")) + mock_modbus_unit.fail_read(BATTERY_RATED_ENERGY, ModbusTimeoutError("timed out")) + await _setup(hass, mock_config_entry) + + asked: list[frozenset[str]] = [] + probe = SolarEdge.async_probe + + async def recording_probe( + unit: ModbusUnit, *, assume_absent: frozenset[str] = frozenset() + ) -> SolarEdge: + asked.append(assume_absent) + return await probe(unit, assume_absent=assume_absent) + + with patch.object(SolarEdge, "async_probe", recording_probe): + await _tick_attachment_check(hass, freezer) + await _tick_attachment_check(hass, freezer) + await hass.config_entries.async_reload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert asked == [ + frozenset(), + frozenset({"batteries", "storage_control"}), + frozenset({"batteries", "storage_control"}), + ] + assert mock_config_entry.data[CONF_SILENT_BLOCKS] == [ + "batteries", + "storage_control", + ]