diff --git a/homeassistant/components/solaredge_modbus/__init__.py b/homeassistant/components/solaredge_modbus/__init__.py index ba54fdade7be..fd2fc03f5dea 100644 --- a/homeassistant/components/solaredge_modbus/__init__.py +++ b/homeassistant/components/solaredge_modbus/__init__.py @@ -87,6 +87,11 @@ async def async_setup_entry( translation_placeholders={"error": str(err)}, ) from err + # Set before probing opens the connection: raising the timeout on an open + # link tears down and reopens the socket, and some SolarEdge inverters + # refuse or ignore a new TCP connection right after the previous one closed. + unit.require_timeout(READ_TIMEOUT) + # Blocks that were still silent on the last attachment check are taken for # absent, so setting up does not wait out a timeout for each. The first # check after setup asks them for real again, which is where a block that @@ -107,9 +112,6 @@ async def async_setup_entry( translation_key="no_solaredge_device", ) from err - # Only now: the probe gives up on a silent block after the link's default. - unit.require_timeout(READ_TIMEOUT) - readings = SolarEdgeModbusDataUpdateCoordinator( hass, entry, diff --git a/homeassistant/components/solaredge_modbus/const.py b/homeassistant/components/solaredge_modbus/const.py index 11c87a188955..61040bd8234f 100644 --- a/homeassistant/components/solaredge_modbus/const.py +++ b/homeassistant/components/solaredge_modbus/const.py @@ -51,10 +51,9 @@ SUBSYSTEM_ADVANCED_POWER_CONTROL: Final = "advanced_power_control" SUBSYSTEM_POWER_CONTROL: Final = "power_control" SUBSYSTEM_SITE_CONTROL: Final = "site_control" -# How long a request may take once the inverter's blocks are known. Over Wi-Fi -# a SolarEdge inverter answers some requests well past the link's default 10 s, -# and one late answer fails the whole poll. Probing keeps the default: a block -# the inverter never answers costs a full timeout there. +# Over Wi-Fi a SolarEdge inverter answers some requests well past the link's +# default 10 s, and one late answer fails the whole poll. Set up front so the +# connection is not torn down and reopened between probing and the first read. READ_TIMEOUT: Final = 30 # Local Modbus is cheap to read and PV production moves fast. diff --git a/tests/components/solaredge_modbus/test_init.py b/tests/components/solaredge_modbus/test_init.py index 39f754fd9684..6485124c5e53 100644 --- a/tests/components/solaredge_modbus/test_init.py +++ b/tests/components/solaredge_modbus/test_init.py @@ -1297,16 +1297,16 @@ async def test_only_a_block_a_check_reloads_for_is_kept( assert mock_config_entry.data[CONF_SILENT_BLOCKS] == ["power_control"] -async def test_reads_wait_longer_than_the_probe( +async def test_timeout_is_set_before_probing( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_modbus_unit: MockModbusUnit, ) -> None: - """Polls get a longer timeout, the probe keeps the link's default. + """The read timeout is set before probing opens the connection. - Over Wi-Fi some answers come late, and one late answer fails a whole poll. - The probe waits out a full timeout for every block the inverter never - answers, so raising it there would only make setting up slower. + Raising the timeout after probing tears down and reopens the underlying + socket, and SolarEdge inverters refuse or ignore a TCP reconnect that + follows immediately after closing the previous connection. """ during_probe: list[float | None] = [] probe = SolarEdge.async_probe @@ -1320,5 +1320,5 @@ async def test_reads_wait_longer_than_the_probe( with patch.object(SolarEdge, "async_probe", recording_probe): await _setup(hass, mock_config_entry) - assert during_probe == [None] + assert during_probe == [READ_TIMEOUT] assert mock_modbus_unit.required_timeout == READ_TIMEOUT