From 513cfe3c419a1ccda384d6176b79cbfb9f67dbe1 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 13 Nov 2024 06:33:04 +0000 Subject: [PATCH 1/3] Move show initial form to `async_step_user` --- homeassistant/components/modern_forms/config_flow.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/modern_forms/config_flow.py b/homeassistant/components/modern_forms/config_flow.py index 33e814efb51..f09fd583822 100644 --- a/homeassistant/components/modern_forms/config_flow.py +++ b/homeassistant/components/modern_forms/config_flow.py @@ -30,6 +30,11 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Handle setup by user for Modern Forms integration.""" + if user_input is None: + return self.async_show_form( + step_id="user", + data_schema=USER_SCHEMA, + ) return await self._handle_config_flow(user_input) async def async_step_zeroconf( @@ -66,10 +71,6 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN): step_id="zeroconf_confirm", description_placeholders={"name": self.name}, ) - return self.async_show_form( - step_id="user", - data_schema=USER_SCHEMA, - ) if self.source == SOURCE_ZEROCONF: user_input[CONF_HOST] = self.host From e7a6c41d602456e68db04fa1142a5f2cb29d6b7c Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 13 Nov 2024 06:54:50 +0000 Subject: [PATCH 2/3] Move show zeroconf form to `async_step_zeroconf_confirm` --- .../components/modern_forms/config_flow.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/modern_forms/config_flow.py b/homeassistant/components/modern_forms/config_flow.py index f09fd583822..b05515b548a 100644 --- a/homeassistant/components/modern_forms/config_flow.py +++ b/homeassistant/components/modern_forms/config_flow.py @@ -56,22 +56,18 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Handle a flow initiated by zeroconf.""" + if user_input is None: + return self.async_show_form( + step_id="zeroconf_confirm", + description_placeholders={"name": self.name}, + ) return await self._handle_config_flow(user_input) async def _handle_config_flow( - self, user_input: dict[str, Any] | None = None, prepare: bool = False + self, user_input: dict[str, Any], prepare: bool = False ) -> ConfigFlowResult: """Config flow handler for ModernForms.""" # Request user input, unless we are preparing discovery flow - if user_input is None: - user_input = {} - if not prepare: - if self.source == SOURCE_ZEROCONF: - return self.async_show_form( - step_id="zeroconf_confirm", - description_placeholders={"name": self.name}, - ) - if self.source == SOURCE_ZEROCONF: user_input[CONF_HOST] = self.host user_input[CONF_MAC] = self.mac From 593e1d4a758c5613b398616c40cf7b98a3aa5224 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 13 Nov 2024 07:43:06 +0000 Subject: [PATCH 3/3] Adjust type hints, and add comments --- .../components/modern_forms/config_flow.py | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/modern_forms/config_flow.py b/homeassistant/components/modern_forms/config_flow.py index b05515b548a..3c217b5747f 100644 --- a/homeassistant/components/modern_forms/config_flow.py +++ b/homeassistant/components/modern_forms/config_flow.py @@ -22,9 +22,9 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - host: str | None = None + host: str mac: str | None = None - name: str | None = None + name: str async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -35,7 +35,8 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN): step_id="user", data_schema=USER_SCHEMA, ) - return await self._handle_config_flow(user_input) + self.host = user_input[CONF_HOST] + return await self._handle_config_flow() async def async_step_zeroconf( self, discovery_info: zeroconf.ZeroconfServiceInfo @@ -49,32 +50,26 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN): self.mac = discovery_info.properties.get(CONF_MAC) self.name = name - # Prepare configuration flow - return await self._handle_config_flow({}, True) + # Loop through self._handle_config_flow to ensure we load the + # MAC if it is missing, and abort if already configured + return await self._handle_config_flow(True) async def async_step_zeroconf_confirm( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Handle a flow initiated by zeroconf.""" - if user_input is None: - return self.async_show_form( - step_id="zeroconf_confirm", - description_placeholders={"name": self.name}, - ) - return await self._handle_config_flow(user_input) + return await self._handle_config_flow() async def _handle_config_flow( - self, user_input: dict[str, Any], prepare: bool = False + self, initial_zeroconf: bool = False ) -> ConfigFlowResult: """Config flow handler for ModernForms.""" - # Request user input, unless we are preparing discovery flow - if self.source == SOURCE_ZEROCONF: - user_input[CONF_HOST] = self.host - user_input[CONF_MAC] = self.mac - - if user_input.get(CONF_MAC) is None or not prepare: + if self.mac is None or not initial_zeroconf: + # User flow + # Or zeroconf without MAC + # Or zeroconf with MAC, but need to ensure device is still available session = async_get_clientsession(self.hass) - device = ModernFormsDevice(user_input[CONF_HOST], session=session) + device = ModernFormsDevice(self.host, session=session) try: device = await device.update() except ModernFormsConnectionError: @@ -85,20 +80,21 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN): data_schema=USER_SCHEMA, errors={"base": "cannot_connect"}, ) - user_input[CONF_MAC] = device.info.mac_address + self.mac = device.info.mac_address + if self.source != SOURCE_ZEROCONF: + self.name = device.info.device_name # Check if already configured - await self.async_set_unique_id(user_input[CONF_MAC]) - self._abort_if_unique_id_configured(updates={CONF_HOST: user_input[CONF_HOST]}) + await self.async_set_unique_id(self.mac) + self._abort_if_unique_id_configured(updates={CONF_HOST: self.host}) - title = device.info.device_name - if self.source == SOURCE_ZEROCONF: - title = self.name - - if prepare: - return await self.async_step_zeroconf_confirm() + if initial_zeroconf: + return self.async_show_form( + step_id="zeroconf_confirm", + description_placeholders={"name": self.name}, + ) return self.async_create_entry( - title=title, - data={CONF_HOST: user_input[CONF_HOST], CONF_MAC: user_input[CONF_MAC]}, + title=self.name, + data={CONF_HOST: self.host, CONF_MAC: self.mac}, )