From 070cb616317f4661e8558706ae025d3d9b2b4868 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 24 May 2022 08:23:09 +0200 Subject: [PATCH] Adjust config-flow type hints in cloudflare (#72388) * Adjust config-flow type hints in cloudflare * Improve type hints --- .../components/cloudflare/config_flow.py | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/cloudflare/config_flow.py b/homeassistant/components/cloudflare/config_flow.py index 27a22dbc5bd..121e0fc9974 100644 --- a/homeassistant/components/cloudflare/config_flow.py +++ b/homeassistant/components/cloudflare/config_flow.py @@ -32,7 +32,7 @@ DATA_SCHEMA = vol.Schema( ) -def _zone_schema(zones: list | None = None): +def _zone_schema(zones: list[str] | None = None) -> vol.Schema: """Zone selection schema.""" zones_list = [] @@ -42,7 +42,7 @@ def _zone_schema(zones: list | None = None): return vol.Schema({vol.Required(CONF_ZONE): vol.In(zones_list)}) -def _records_schema(records: list | None = None): +def _records_schema(records: list[str] | None = None) -> vol.Schema: """Zone records selection schema.""" records_dict = {} @@ -52,13 +52,15 @@ def _records_schema(records: list | None = None): return vol.Schema({vol.Required(CONF_RECORDS): cv.multi_select(records_dict)}) -async def validate_input(hass: HomeAssistant, data: dict): +async def _validate_input( + hass: HomeAssistant, data: dict[str, Any] +) -> dict[str, list[str] | None]: """Validate the user input allows us to connect. Data has the keys from DATA_SCHEMA with values provided by the user. """ zone = data.get(CONF_ZONE) - records = None + records: list[str] | None = None cfupdate = CloudflareUpdater( async_get_clientsession(hass), @@ -68,7 +70,7 @@ async def validate_input(hass: HomeAssistant, data: dict): ) try: - zones = await cfupdate.get_zones() + zones: list[str] | None = await cfupdate.get_zones() if zone: zone_id = await cfupdate.get_zone_id() records = await cfupdate.get_zone_records(zone_id, "A") @@ -89,11 +91,11 @@ class CloudflareConfigFlow(ConfigFlow, domain=DOMAIN): entry: ConfigEntry | None = None - def __init__(self): + def __init__(self) -> None: """Initialize the Cloudflare config flow.""" - self.cloudflare_config = {} - self.zones = None - self.records = None + self.cloudflare_config: dict[str, Any] = {} + self.zones: list[str] | None = None + self.records: list[str] | None = None async def async_step_reauth(self, data: dict[str, Any]) -> FlowResult: """Handle initiation of re-authentication with Cloudflare.""" @@ -104,7 +106,7 @@ class CloudflareConfigFlow(ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle re-authentication with Cloudflare.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None and self.entry: _, errors = await self._async_validate_or_error(user_input) @@ -130,14 +132,16 @@ class CloudflareConfigFlow(ConfigFlow, domain=DOMAIN): errors=errors, ) - async def async_step_user(self, user_input: dict | None = None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Handle a flow initiated by the user.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") persistent_notification.async_dismiss(self.hass, "cloudflare_setup") - errors = {} + errors: dict[str, str] = {} if user_input is not None: info, errors = await self._async_validate_or_error(user_input) @@ -151,9 +155,11 @@ class CloudflareConfigFlow(ConfigFlow, domain=DOMAIN): step_id="user", data_schema=DATA_SCHEMA, errors=errors ) - async def async_step_zone(self, user_input: dict | None = None): + async def async_step_zone( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Handle the picking the zone.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: self.cloudflare_config.update(user_input) @@ -171,7 +177,9 @@ class CloudflareConfigFlow(ConfigFlow, domain=DOMAIN): errors=errors, ) - async def async_step_records(self, user_input: dict | None = None): + async def async_step_records( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: """Handle the picking the zone records.""" if user_input is not None: @@ -184,12 +192,14 @@ class CloudflareConfigFlow(ConfigFlow, domain=DOMAIN): data_schema=_records_schema(self.records), ) - async def _async_validate_or_error(self, config): - errors = {} + async def _async_validate_or_error( + self, config: dict[str, Any] + ) -> tuple[dict[str, list[str] | None], dict[str, str]]: + errors: dict[str, str] = {} info = {} try: - info = await validate_input(self.hass, config) + info = await _validate_input(self.hass, config) except CannotConnect: errors["base"] = "cannot_connect" except InvalidAuth: