Add homee reconfiguration flow (#146065)

* Add a reconfigure flow to homee

* Add tests for reconfiguration flow

* string refinement

* fix review comments

* more review fixes
This commit is contained in:
Markus Adrario 2025-06-04 07:27:07 -06:00 committed by GitHub
parent 07557e27b0
commit c6c7e7eae1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 189 additions and 2 deletions

View File

@ -83,3 +83,54 @@ class HomeeConfigFlow(ConfigFlow, domain=DOMAIN):
data_schema=AUTH_SCHEMA,
errors=errors,
)
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the reconfigure flow."""
errors: dict[str, str] = {}
reconfigure_entry = self._get_reconfigure_entry()
if user_input:
self.homee = Homee(
user_input[CONF_HOST],
reconfigure_entry.data[CONF_USERNAME],
reconfigure_entry.data[CONF_PASSWORD],
)
try:
await self.homee.get_access_token()
except HomeeConnectionFailedException:
errors["base"] = "cannot_connect"
except HomeeAuthenticationFailedException:
errors["base"] = "invalid_auth"
except Exception:
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
self.hass.loop.create_task(self.homee.run())
await self.homee.wait_until_connected()
self.homee.disconnect()
await self.homee.wait_until_disconnected()
await self.async_set_unique_id(self.homee.settings.uid)
self._abort_if_unique_id_mismatch(reason="wrong_hub")
_LOGGER.debug("Updated homee entry with ID %s", self.homee.settings.uid)
return self.async_update_reload_and_abort(
self._get_reconfigure_entry(), data_updates=user_input
)
return self.async_show_form(
data_schema=vol.Schema(
{
vol.Required(
CONF_HOST, default=reconfigure_entry.data[CONF_HOST]
): str
}
),
description_placeholders={
"name": reconfigure_entry.runtime_data.settings.uid
},
errors=errors,
)

View File

@ -2,7 +2,9 @@
"config": {
"flow_title": "homee {name} ({host})",
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
"wrong_hub": "Address belongs to a different homee."
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
@ -22,6 +24,16 @@
"username": "The username for your homee.",
"password": "The password for your homee."
}
},
"reconfigure": {
"title": "Reconfigure homee {name}",
"description": "Reconfigure the IP address of your homee.",
"data": {
"host": "[%key:common::config_flow::data::host%]"
},
"data_description": {
"host": "The IP address of your homee."
}
}
}
},

View File

@ -12,6 +12,7 @@ from tests.common import MockConfigEntry
HOMEE_ID = "00055511EECC"
HOMEE_IP = "192.168.1.11"
NEW_HOMEE_IP = "192.168.1.12"
HOMEE_NAME = "TestHomee"
TESTUSER = "testuser"
TESTPASS = "testpass"

View File

@ -11,7 +11,7 @@ from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from .conftest import HOMEE_ID, HOMEE_IP, HOMEE_NAME, TESTPASS, TESTUSER
from .conftest import HOMEE_ID, HOMEE_IP, HOMEE_NAME, NEW_HOMEE_IP, TESTPASS, TESTUSER
from tests.common import MockConfigEntry
@ -130,3 +130,126 @@ async def test_flow_already_configured(
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reconfigure_success(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_homee: AsyncMock,
) -> None:
"""Test the reconfigure flow."""
mock_config_entry.add_to_hass(hass)
mock_config_entry.runtime_data = mock_homee
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result["step_id"] == "reconfigure"
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
assert result["handler"] == DOMAIN
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: NEW_HOMEE_IP,
},
)
assert result2["type"] is FlowResultType.ABORT
assert result2["reason"] == "reconfigure_successful"
# Confirm that the config entry has been updated
assert mock_config_entry.data[CONF_HOST] == NEW_HOMEE_IP
assert mock_config_entry.data[CONF_USERNAME] == TESTUSER
assert mock_config_entry.data[CONF_PASSWORD] == TESTPASS
@pytest.mark.parametrize(
("side_eff", "error"),
[
(
HomeeConnectionFailedException("connection timed out"),
{"base": "cannot_connect"},
),
(
HomeeAuthFailedException("wrong username or password"),
{"base": "invalid_auth"},
),
(
Exception,
{"base": "unknown"},
),
],
)
async def test_reconfigure_errors(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_homee: AsyncMock,
side_eff: Exception,
error: dict[str, str],
) -> None:
"""Test reconfigure flow errors."""
mock_config_entry.add_to_hass(hass)
mock_config_entry.runtime_data = mock_homee
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
mock_homee.get_access_token.side_effect = side_eff
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: NEW_HOMEE_IP,
},
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == error
# Confirm that the config entry is unchanged
assert mock_config_entry.data[CONF_HOST] == HOMEE_IP
mock_homee.get_access_token.side_effect = None
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: NEW_HOMEE_IP,
},
)
assert result2["type"] is FlowResultType.ABORT
assert result2["reason"] == "reconfigure_successful"
# Confirm that the config entry has been updated
assert mock_config_entry.data[CONF_HOST] == NEW_HOMEE_IP
assert mock_config_entry.data[CONF_USERNAME] == TESTUSER
assert mock_config_entry.data[CONF_PASSWORD] == TESTPASS
async def test_reconfigure_wrong_uid(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_homee: AsyncMock,
) -> None:
"""Test reconfigure flow with wrong UID."""
mock_config_entry.add_to_hass(hass)
mock_homee.settings.uid = "wrong_uid"
mock_config_entry.runtime_data = mock_homee
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: NEW_HOMEE_IP,
},
)
assert result2["type"] is FlowResultType.ABORT
assert result2["reason"] == "wrong_hub"
# Confirm that the config entry is unchanged
assert mock_config_entry.data[CONF_HOST] == HOMEE_IP