From 4d96ca3ddb3df29d131394c0a96a54b4f9c7d0bb Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 16 Nov 2021 00:27:04 +0100 Subject: [PATCH] Adjust async_step_homekit signature for strict typing (#59745) * Use ZeroconfServiceInfo in async_step_homekit * Update DiscoveryFlowHandler * Update components --- homeassistant/components/nanoleaf/config_flow.py | 10 +++++----- homeassistant/components/rainmachine/config_flow.py | 11 ++++++----- homeassistant/components/tradfri/config_flow.py | 8 +++++--- homeassistant/config_entries.py | 4 ++-- homeassistant/helpers/config_entry_flow.py | 12 +++++++++++- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/nanoleaf/config_flow.py b/homeassistant/components/nanoleaf/config_flow.py index 17269f2e07a..1d0b31549df 100644 --- a/homeassistant/components/nanoleaf/config_flow.py +++ b/homeassistant/components/nanoleaf/config_flow.py @@ -93,17 +93,17 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): ) -> FlowResult: """Handle Nanoleaf Zeroconf discovery.""" _LOGGER.debug("Zeroconf discovered: %s", discovery_info) - return await self._async_homekit_zeroconf_discovery_handler( - cast(dict, discovery_info) - ) + return await self._async_homekit_zeroconf_discovery_handler(discovery_info) - async def async_step_homekit(self, discovery_info: DiscoveryInfoType) -> FlowResult: + async def async_step_homekit( + self, discovery_info: zeroconf.ZeroconfServiceInfo + ) -> FlowResult: """Handle Nanoleaf Homekit discovery.""" _LOGGER.debug("Homekit discovered: %s", discovery_info) return await self._async_homekit_zeroconf_discovery_handler(discovery_info) async def _async_homekit_zeroconf_discovery_handler( - self, discovery_info: DiscoveryInfoType + self, discovery_info: zeroconf.ZeroconfServiceInfo ) -> FlowResult: """Handle Nanoleaf Homekit and Zeroconf discovery.""" return await self._async_discovery_handler( diff --git a/homeassistant/components/rainmachine/config_flow.py b/homeassistant/components/rainmachine/config_flow.py index 47896cc6080..4dcbbe0423f 100644 --- a/homeassistant/components/rainmachine/config_flow.py +++ b/homeassistant/components/rainmachine/config_flow.py @@ -1,7 +1,7 @@ """Config flow to configure the RainMachine component.""" from __future__ import annotations -from typing import Any, cast +from typing import Any from regenmaschine import Client from regenmaschine.controller import Controller @@ -15,7 +15,6 @@ from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT, CONF_ from homeassistant.core import HomeAssistant, callback from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers import aiohttp_client, config_validation as cv -from homeassistant.helpers.typing import DiscoveryInfoType from .const import CONF_ZONE_RUN_TIME, DEFAULT_PORT, DEFAULT_ZONE_RUN, DOMAIN @@ -55,7 +54,9 @@ class RainMachineFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Define the config flow to handle options.""" return RainMachineOptionsFlowHandler(config_entry) - async def async_step_homekit(self, discovery_info: DiscoveryInfoType) -> FlowResult: + async def async_step_homekit( + self, discovery_info: zeroconf.ZeroconfServiceInfo + ) -> FlowResult: """Handle a flow initialized by homekit discovery.""" return await self.async_step_homekit_zeroconf(discovery_info) @@ -63,10 +64,10 @@ class RainMachineFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): self, discovery_info: zeroconf.ZeroconfServiceInfo ) -> FlowResult: """Handle discovery via zeroconf.""" - return await self.async_step_homekit_zeroconf(cast(dict, discovery_info)) + return await self.async_step_homekit_zeroconf(discovery_info) async def async_step_homekit_zeroconf( - self, discovery_info: DiscoveryInfoType + self, discovery_info: zeroconf.ZeroconfServiceInfo ) -> FlowResult: """Handle discovery via zeroconf.""" ip_address = discovery_info["host"] diff --git a/homeassistant/components/tradfri/config_flow.py b/homeassistant/components/tradfri/config_flow.py index 4de2aa302f0..f0f4016ba9b 100644 --- a/homeassistant/components/tradfri/config_flow.py +++ b/homeassistant/components/tradfri/config_flow.py @@ -11,9 +11,9 @@ from pytradfri.api.aiocoap_api import APIFactory import voluptuous as vol from homeassistant import config_entries +from homeassistant.components import zeroconf from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResult -from homeassistant.helpers.typing import DiscoveryInfoType from .const import ( CONF_GATEWAY_ID, @@ -42,7 +42,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Initialize flow.""" - self._host = None + self._host: str | None = None self._import_groups = False async def async_step_user( @@ -92,7 +92,9 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN): step_id="auth", data_schema=vol.Schema(fields), errors=errors ) - async def async_step_homekit(self, discovery_info: DiscoveryInfoType) -> FlowResult: + async def async_step_homekit( + self, discovery_info: zeroconf.ZeroconfServiceInfo + ) -> FlowResult: """Handle homekit discovery.""" await self.async_set_unique_id(discovery_info["properties"]["id"]) self._abort_if_unique_id_configured({CONF_HOST: discovery_info["host"]}) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 521fb5d444c..d9c0faf2f71 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -1354,10 +1354,10 @@ class ConfigFlow(data_entry_flow.FlowHandler): return await self.async_step_discovery(discovery_info) async def async_step_homekit( - self, discovery_info: DiscoveryInfoType + self, discovery_info: ZeroconfServiceInfo ) -> data_entry_flow.FlowResult: """Handle a flow initialized by Homekit discovery.""" - return await self.async_step_discovery(discovery_info) + return await self.async_step_discovery(cast(dict, discovery_info)) async def async_step_mqtt( self, discovery_info: DiscoveryInfoType diff --git a/homeassistant/helpers/config_entry_flow.py b/homeassistant/helpers/config_entry_flow.py index 1e87d0042d7..4a312b5e01f 100644 --- a/homeassistant/helpers/config_entry_flow.py +++ b/homeassistant/helpers/config_entry_flow.py @@ -82,6 +82,17 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow): return await self.async_step_confirm() + async def async_step_homekit( + self, discovery_info: zeroconf.ZeroconfServiceInfo + ) -> FlowResult: + """Handle a flow initialized by Homekit discovery.""" + if self._async_in_progress() or self._async_current_entries(): + return self.async_abort(reason="single_instance_allowed") + + await self.async_set_unique_id(self._domain) + + return await self.async_step_confirm() + async def async_step_zeroconf( self, discovery_info: zeroconf.ZeroconfServiceInfo ) -> FlowResult: @@ -95,7 +106,6 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow): async_step_ssdp = async_step_discovery async_step_mqtt = async_step_discovery - async_step_homekit = async_step_discovery async_step_dhcp = async_step_discovery async def async_step_import(self, _: dict[str, Any] | None) -> FlowResult: