Compare commits

...
Sign in to create a new pull request.

3 commits

Author SHA1 Message Date
epenet
593e1d4a75 Adjust type hints, and add comments 2024-11-13 07:43:06 +00:00
epenet
e7a6c41d60 Move show zeroconf form to async_step_zeroconf_confirm 2024-11-13 06:54:50 +00:00
epenet
513cfe3c41 Move show initial form to async_step_user 2024-11-13 06:33:04 +00:00

View file

@ -22,15 +22,21 @@ 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 | None = None name: str
async def async_step_user( async def async_step_user(
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},
) )