Skip to content

Commit

Permalink
Improve type hints in konnected config flow (#124904)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Aug 30, 2024
1 parent 9e23607 commit ffabd5d
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions homeassistant/components/konnected/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
import random
import string
from typing import Any
from typing import TYPE_CHECKING, Any
from urllib.parse import urlparse

import voluptuous as vol
Expand Down Expand Up @@ -227,8 +227,12 @@ async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResu
self._abort_if_unique_id_configured()
return await self.async_step_import_confirm()

async def async_step_import_confirm(self, user_input=None):
async def async_step_import_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Confirm the user wants to import the config entry."""
if TYPE_CHECKING:
assert self.unique_id is not None
if user_input is None:
return self.async_show_form(
step_id="import_confirm",
Expand Down Expand Up @@ -349,7 +353,9 @@ async def async_step_user(
errors=errors,
)

async def async_step_confirm(self, user_input=None):
async def async_step_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Attempt to link with the Konnected panel.
Given a configured host, will ask the user to confirm and finalize
Expand Down Expand Up @@ -401,8 +407,8 @@ def __init__(self, config_entry: ConfigEntry) -> None:
self.current_opt = self.entry.options or self.entry.data[CONF_DEFAULT_OPTIONS]

# as config proceeds we'll build up new options and then replace what's in the config entry
self.new_opt: dict[str, dict[str, Any]] = {CONF_IO: {}}
self.active_cfg = None
self.new_opt: dict[str, Any] = {CONF_IO: {}}
self.active_cfg: str | None = None
self.io_cfg: dict[str, Any] = {}
self.current_states: list[dict[str, Any]] = []
self.current_state = 1
Expand All @@ -419,13 +425,17 @@ def get_current_cfg(self, io_type, zone):
{},
)

async def async_step_init(self, user_input=None):
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle options flow."""
return await self.async_step_options_io()

async def async_step_options_io(self, user_input=None):
async def async_step_options_io(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Configure legacy panel IO or first half of pro IO."""
errors = {}
errors: dict[str, str] = {}
current_io = self.current_opt.get(CONF_IO, {})

if user_input is not None:
Expand Down Expand Up @@ -508,9 +518,11 @@ async def async_step_options_io(self, user_input=None):

return self.async_abort(reason="not_konn_panel")

async def async_step_options_io_ext(self, user_input=None):
async def async_step_options_io_ext(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Allow the user to configure the extended IO for pro."""
errors = {}
errors: dict[str, str] = {}
current_io = self.current_opt.get(CONF_IO, {})

if user_input is not None:
Expand Down Expand Up @@ -566,10 +578,12 @@ async def async_step_options_io_ext(self, user_input=None):

return self.async_abort(reason="not_konn_panel")

async def async_step_options_binary(self, user_input=None):
async def async_step_options_binary(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Allow the user to configure the IO options for binary sensors."""
errors = {}
if user_input is not None:
errors: dict[str, str] = {}
if user_input is not None and self.active_cfg is not None:
zone = {"zone": self.active_cfg}
zone.update(user_input)
self.new_opt[CONF_BINARY_SENSORS] = [
Expand Down Expand Up @@ -602,7 +616,7 @@ async def async_step_options_binary(self, user_input=None):
description_placeholders={
"zone": f"Zone {self.active_cfg}"
if len(self.active_cfg) < 3
else self.active_cfg.upper
else self.active_cfg.upper()
},
errors=errors,
)
Expand Down Expand Up @@ -635,17 +649,19 @@ async def async_step_options_binary(self, user_input=None):
description_placeholders={
"zone": f"Zone {self.active_cfg}"
if len(self.active_cfg) < 3
else self.active_cfg.upper
else self.active_cfg.upper()
},
errors=errors,
)

return await self.async_step_options_digital()

async def async_step_options_digital(self, user_input=None):
async def async_step_options_digital(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Allow the user to configure the IO options for digital sensors."""
errors = {}
if user_input is not None:
errors: dict[str, str] = {}
if user_input is not None and self.active_cfg is not None:
zone = {"zone": self.active_cfg}
zone.update(user_input)
self.new_opt[CONF_SENSORS] = [*self.new_opt.get(CONF_SENSORS, []), zone]
Expand Down Expand Up @@ -710,10 +726,12 @@ async def async_step_options_digital(self, user_input=None):

return await self.async_step_options_switch()

async def async_step_options_switch(self, user_input=None):
async def async_step_options_switch(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Allow the user to configure the IO options for switches."""
errors = {}
if user_input is not None:
errors: dict[str, str] = {}
if user_input is not None and self.active_cfg is not None:
zone = {"zone": self.active_cfg}
zone.update(user_input)
del zone[CONF_MORE_STATES]
Expand Down Expand Up @@ -825,7 +843,9 @@ async def async_step_options_switch(self, user_input=None):

return await self.async_step_options_misc()

async def async_step_options_misc(self, user_input=None):
async def async_step_options_misc(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Allow the user to configure the LED behavior."""
errors = {}
if user_input is not None:
Expand Down

0 comments on commit ffabd5d

Please sign in to comment.