mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Add reconfigure flow to BLUETTI Modbus (#188188)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
026844669f
commit
ad8515e6fa
@@ -81,6 +81,33 @@ class BluettiModbusFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
step_id="user", data_schema=STEP_USER, errors=errors
|
||||
)
|
||||
|
||||
async def async_step_reconfigure(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Point an existing entry at the address the device moved to."""
|
||||
reconfigure_entry = self._get_reconfigure_entry()
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input is not None:
|
||||
errors, serial = await self._async_validate(user_input)
|
||||
if not errors:
|
||||
if TYPE_CHECKING:
|
||||
assert serial is not None
|
||||
await self.async_set_unique_id(serial)
|
||||
# Another serial is a different power station, not a moved one.
|
||||
self._abort_if_unique_id_mismatch()
|
||||
return self.async_update_reload_and_abort(
|
||||
reconfigure_entry, data_updates=user_input
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reconfigure",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
STEP_USER, user_input or reconfigure_entry.data
|
||||
),
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def _async_validate(
|
||||
self, data: dict[str, Any]
|
||||
) -> tuple[dict[str, str], str | None]:
|
||||
|
||||
@@ -83,9 +83,7 @@ rules:
|
||||
entity-translations: done
|
||||
exception-translations: done
|
||||
icon-translations: done
|
||||
reconfiguration-flow:
|
||||
status: todo
|
||||
comment: Split off to a follow-up PR to keep this one reviewable.
|
||||
reconfiguration-flow: done
|
||||
repair-issues:
|
||||
status: exempt
|
||||
comment: No repairable issues are raised.
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
{
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||
"unique_id_mismatch": "The power station at this address has a different serial number than expected. Make sure you enter the address for the correct power station."
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
@@ -11,6 +12,19 @@
|
||||
"unsupported_device": "The device at this address does not report itself as a BLUETTI Balco 260, the only supported model."
|
||||
},
|
||||
"step": {
|
||||
"reconfigure": {
|
||||
"data": {
|
||||
"host": "[%key:common::config_flow::data::host%]",
|
||||
"port": "[%key:common::config_flow::data::port%]",
|
||||
"unit_id": "[%key:component::bluetti_modbus::config::step::user::data::unit_id%]"
|
||||
},
|
||||
"data_description": {
|
||||
"host": "[%key:component::bluetti_modbus::config::step::user::data_description::host%]",
|
||||
"port": "[%key:component::bluetti_modbus::config::step::user::data_description::port%]",
|
||||
"unit_id": "[%key:component::bluetti_modbus::config::step::user::data_description::unit_id%]"
|
||||
},
|
||||
"description": "Update the address Home Assistant reaches this power station at."
|
||||
},
|
||||
"user": {
|
||||
"data": {
|
||||
"host": "[%key:common::config_flow::data::host%]",
|
||||
|
||||
@@ -15,12 +15,13 @@ import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
|
||||
from homeassistant.components.bluetti_modbus.const import CONF_UNIT_ID, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
from . import setup_integration
|
||||
from .conftest import (
|
||||
HOST,
|
||||
PORT,
|
||||
@@ -32,6 +33,7 @@ from .conftest import (
|
||||
)
|
||||
|
||||
TITLE = "Balco 260"
|
||||
NEW_HOST = "5.6.7.8"
|
||||
|
||||
|
||||
def _user_input(host: str = HOST, unit_id: int = UNIT_ID) -> dict[str, Any]:
|
||||
@@ -301,3 +303,76 @@ async def test_user_flow_rejects_the_same_serial_at_a_different_endpoint(
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_reconfigure_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_modbus_unit: MockModbusUnit,
|
||||
) -> None:
|
||||
"""The entry is pointed at the address the same device moved to."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], _user_input(host=NEW_HOST)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert mock_config_entry.data == {**bluetti_data(), CONF_HOST: NEW_HOST}
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
|
||||
async def test_reconfigure_flow_cannot_connect(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_modbus_unit: MockModbusUnit,
|
||||
) -> None:
|
||||
"""A device that can't be read surfaces cannot_connect, then the flow recovers."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
mock_modbus_unit.fail_requests(ModbusTimeoutError("timed out"))
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], _user_input(host=NEW_HOST)
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
mock_modbus_unit.fail_requests(None)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], _user_input(host=NEW_HOST)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert mock_config_entry.data[CONF_HOST] == NEW_HOST
|
||||
|
||||
|
||||
async def test_reconfigure_flow_rejects_another_device(
|
||||
hass: HomeAssistant,
|
||||
mock_modbus_connection: MockModbusConnection,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_modbus_unit: MockModbusUnit,
|
||||
) -> None:
|
||||
"""A different serial at the new address aborts and leaves the entry as it was."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
other = mock_modbus_connection.for_unit(2)
|
||||
seed_unit(other)
|
||||
other.holding[SERIAL_ADDRESS] = 5678
|
||||
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], _user_input(unit_id=2)
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "unique_id_mismatch"
|
||||
assert mock_config_entry.data == bluetti_data()
|
||||
|
||||
Reference in New Issue
Block a user