mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Tell a silent Sofar inverter apart from an unreachable adapter (#186422)
This commit is contained in:
@@ -4,7 +4,7 @@ from collections.abc import Mapping
|
||||
import logging
|
||||
from typing import Any, override
|
||||
|
||||
from modbus_connection import ModbusError
|
||||
from modbus_connection import ModbusError, ModbusTimeoutError
|
||||
import probatio
|
||||
from sofar_modbus.modern.device import SofarInverter
|
||||
|
||||
@@ -69,11 +69,20 @@ STEP_SERIAL_DATA_SCHEMA = probatio.Schema(
|
||||
)
|
||||
|
||||
|
||||
class _InverterSilentError(Exception):
|
||||
"""The adapter is reachable, but the inverter behind it is not answering."""
|
||||
|
||||
|
||||
async def _async_probe(hass: HomeAssistant, data: Mapping[str, Any]) -> SofarInverter:
|
||||
"""Connect to the inverter and identify it, or raise."""
|
||||
params = create_modbus_params(data)
|
||||
async with async_get_temporary_unit(hass, params, data[CONF_UNIT_ID]) as unit:
|
||||
return await SofarInverter.async_detect(unit)
|
||||
try:
|
||||
return await SofarInverter.async_detect(unit)
|
||||
except ModbusTimeoutError as err:
|
||||
if unit.connected:
|
||||
raise _InverterSilentError from err
|
||||
raise
|
||||
|
||||
|
||||
class SofarConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
@@ -209,6 +218,8 @@ class SofarConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Probe the inverter, returning it or the errors to show instead."""
|
||||
try:
|
||||
device = await _async_probe(self.hass, data)
|
||||
except _InverterSilentError:
|
||||
return None, {"base": "inverter_silent"}, {}
|
||||
except (ModbusError, HomeAssistantError) as err:
|
||||
return None, {"base": "cannot_connect"}, {"error": str(err)}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "Failed to connect: {error}",
|
||||
"inverter_silent": "The adapter is reachable, but the inverter behind it did not answer. A PV inverter switches off without sunlight, so try again in daylight. Otherwise, check the unit ID and the adapter's serial settings.",
|
||||
"unrecognized_inverter": "The device answered, but neither its serial number nor its register masks identify a known Sofar model."
|
||||
},
|
||||
"step": {
|
||||
|
||||
@@ -187,11 +187,17 @@ async def test_user_step_success_without_model(
|
||||
assert result["result"].unique_id == serial
|
||||
|
||||
|
||||
def _seed_unreachable(unit: MockModbusUnit) -> None:
|
||||
unit.fail_requests(ModbusTimeoutError("stuck"))
|
||||
def _seed_unreachable(connection: MockModbusConnection) -> None:
|
||||
"""No adapter answers, so the link never opens."""
|
||||
connection._connect_client = AsyncMock(side_effect=ModbusTimeoutError("stuck"))
|
||||
|
||||
|
||||
def _seed_unrecognized(unit: MockModbusUnit) -> None:
|
||||
def _seed_silent(connection: MockModbusConnection) -> None:
|
||||
"""The adapter answers, but the inverter behind it does not."""
|
||||
connection.for_unit(1).fail_requests(ModbusTimeoutError("asleep"))
|
||||
|
||||
|
||||
def _seed_unrecognized(connection: MockModbusConnection) -> None:
|
||||
"""No-op: unseeded registers give no known serial and no usable mask."""
|
||||
|
||||
|
||||
@@ -204,6 +210,7 @@ def _seed_unrecognized(unit: MockModbusUnit) -> None:
|
||||
{"error": "stuck"},
|
||||
id="cannot_connect",
|
||||
),
|
||||
pytest.param(_seed_silent, "inverter_silent", {}, id="inverter_silent"),
|
||||
pytest.param(
|
||||
_seed_unrecognized,
|
||||
"unrecognized_inverter",
|
||||
@@ -215,13 +222,13 @@ def _seed_unrecognized(unit: MockModbusUnit) -> None:
|
||||
async def test_user_step_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
seed: Callable[[MockModbusUnit], None],
|
||||
seed: Callable[[MockModbusConnection], None],
|
||||
expected_error: str,
|
||||
expected_placeholders: dict[str, str],
|
||||
) -> None:
|
||||
"""Test the user step reports the right error and recovers, per failure."""
|
||||
mock_conn = MockModbusConnection()
|
||||
seed(mock_conn.for_unit(1))
|
||||
seed(mock_conn)
|
||||
flow_id = await _start_flow(hass, TYPE_TCP)
|
||||
|
||||
with _patch_temporary_unit(mock_conn):
|
||||
@@ -404,6 +411,7 @@ async def test_reconfigure_rejects_a_different_serial(hass: HomeAssistant) -> No
|
||||
{"error": "stuck"},
|
||||
id="cannot_connect",
|
||||
),
|
||||
pytest.param(_seed_silent, "inverter_silent", {}, id="inverter_silent"),
|
||||
pytest.param(
|
||||
_seed_unrecognized,
|
||||
"unrecognized_inverter",
|
||||
@@ -415,7 +423,7 @@ async def test_reconfigure_rejects_a_different_serial(hass: HomeAssistant) -> No
|
||||
async def test_reconfigure_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
seed: Callable[[MockModbusUnit], None],
|
||||
seed: Callable[[MockModbusConnection], None],
|
||||
expected_error: str,
|
||||
expected_placeholders: dict[str, str],
|
||||
) -> None:
|
||||
@@ -425,7 +433,7 @@ async def test_reconfigure_errors(
|
||||
result = await _start_reconfigure(hass, entry, STEP_RECONFIGURE_TCP)
|
||||
|
||||
mock_conn = MockModbusConnection()
|
||||
seed(mock_conn.for_unit(1))
|
||||
seed(mock_conn)
|
||||
|
||||
with _patch_temporary_unit(mock_conn):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
@@ -525,7 +533,7 @@ async def test_reconfigure_new_line_settings(
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_get_unit")
|
||||
async def test_reconfigure_new_line_settings_cannot_connect(
|
||||
async def test_reconfigure_new_line_settings_silent_inverter(
|
||||
hass: HomeAssistant, mock_connection: MockModbusConnection
|
||||
) -> None:
|
||||
"""Test a failed probe puts the entry back on the bus it was taken off."""
|
||||
@@ -545,7 +553,7 @@ async def test_reconfigure_new_line_settings_cannot_connect(
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
assert result["errors"] == {"base": "inverter_silent"}
|
||||
assert entry.data == MOCK_SERIAL_ENTRY_DATA
|
||||
assert entry.unique_id == MOCK_SERIAL
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
Reference in New Issue
Block a user