From c6c7e7eae10ca501d99c91593f6dc49f58173120 Mon Sep 17 00:00:00 2001 From: Markus Adrario Date: Wed, 4 Jun 2025 07:27:07 -0600 Subject: [PATCH] Add homee reconfiguration flow (#146065) * Add a reconfigure flow to homee * Add tests for reconfiguration flow * string refinement * fix review comments * more review fixes --- homeassistant/components/homee/config_flow.py | 51 +++++++ homeassistant/components/homee/strings.json | 14 +- tests/components/homee/conftest.py | 1 + tests/components/homee/test_config_flow.py | 125 +++++++++++++++++- 4 files changed, 189 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homee/config_flow.py b/homeassistant/components/homee/config_flow.py index 1a3c5011f82..773ca0dff1d 100644 --- a/homeassistant/components/homee/config_flow.py +++ b/homeassistant/components/homee/config_flow.py @@ -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, + ) diff --git a/homeassistant/components/homee/strings.json b/homeassistant/components/homee/strings.json index 5e124aa427e..19c21e662e0 100644 --- a/homeassistant/components/homee/strings.json +++ b/homeassistant/components/homee/strings.json @@ -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." + } } } }, diff --git a/tests/components/homee/conftest.py b/tests/components/homee/conftest.py index 5a3234e896b..75fc3dc830b 100644 --- a/tests/components/homee/conftest.py +++ b/tests/components/homee/conftest.py @@ -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" diff --git a/tests/components/homee/test_config_flow.py b/tests/components/homee/test_config_flow.py index 4dfe8226d16..70d34ced91c 100644 --- a/tests/components/homee/test_config_flow.py +++ b/tests/components/homee/test_config_flow.py @@ -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