Simplify modern_forms config flow (part 2) (#130494)

This commit is contained in:
epenet 2024-12-17 19:23:54 +01:00 committed by GitHub
parent 98d5020690
commit b5f6734197
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,7 +22,7 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
host: str | None = None host: str
mac: str | None = None mac: str | None = None
name: str name: str
@ -30,7 +30,13 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Handle setup by user for Modern Forms integration.""" """Handle setup by user for Modern Forms integration."""
return await self._handle_config_flow(user_input) if user_input is None:
return self.async_show_form(
step_id="user",
data_schema=USER_SCHEMA,
)
self.host = user_input[CONF_HOST]
return await self._handle_config_flow()
async def async_step_zeroconf( async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo self, discovery_info: zeroconf.ZeroconfServiceInfo
@ -44,40 +50,26 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN):
self.mac = discovery_info.properties.get(CONF_MAC) self.mac = discovery_info.properties.get(CONF_MAC)
self.name = name self.name = name
# Prepare configuration flow # Loop through self._handle_config_flow to ensure we load the
return await self._handle_config_flow({}, True) # MAC if it is missing, and abort if already configured
return await self._handle_config_flow(True)
async def async_step_zeroconf_confirm( async def async_step_zeroconf_confirm(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Handle a flow initiated by zeroconf.""" """Handle a flow initiated by zeroconf."""
return await self._handle_config_flow(user_input) return await self._handle_config_flow()
async def _handle_config_flow( async def _handle_config_flow(
self, user_input: dict[str, Any] | None = None, prepare: bool = False self, initial_zeroconf: bool = False
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Config flow handler for ModernForms.""" """Config flow handler for ModernForms."""
# Request user input, unless we are preparing discovery flow if self.mac is None or not initial_zeroconf:
if user_input is None: # User flow
user_input = {} # Or zeroconf without MAC
if not prepare: # Or zeroconf with MAC, but need to ensure device is still available
if self.source == SOURCE_ZEROCONF:
return self.async_show_form(
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
user_input[CONF_MAC] = self.mac
if user_input.get(CONF_MAC) is None or not prepare:
session = async_get_clientsession(self.hass) session = async_get_clientsession(self.hass)
device = ModernFormsDevice(user_input[CONF_HOST], session=session) device = ModernFormsDevice(self.host, session=session)
try: try:
device = await device.update() device = await device.update()
except ModernFormsConnectionError: except ModernFormsConnectionError:
@ -88,20 +80,21 @@ class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN):
data_schema=USER_SCHEMA, data_schema=USER_SCHEMA,
errors={"base": "cannot_connect"}, 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 # Check if already configured
await self.async_set_unique_id(user_input[CONF_MAC]) await self.async_set_unique_id(self.mac)
self._abort_if_unique_id_configured(updates={CONF_HOST: user_input[CONF_HOST]}) self._abort_if_unique_id_configured(updates={CONF_HOST: self.host})
title = device.info.device_name if initial_zeroconf:
if self.source == SOURCE_ZEROCONF: return self.async_show_form(
title = self.name step_id="zeroconf_confirm",
description_placeholders={"name": self.name},
if prepare: )
return await self.async_step_zeroconf_confirm()
return self.async_create_entry( return self.async_create_entry(
title=title, title=self.name,
data={CONF_HOST: user_input[CONF_HOST], CONF_MAC: user_input[CONF_MAC]}, data={CONF_HOST: self.host, CONF_MAC: self.mac},
) )