Skip to content

Commit

Permalink
Improve config flow type hints (a-f) (home-assistant#124859)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Aug 29, 2024
1 parent 34680be commit 681fe34
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 32 deletions.
28 changes: 16 additions & 12 deletions homeassistant/components/broadlink/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import partial
import logging
import socket
from typing import TYPE_CHECKING, Any
from typing import Any

import broadlink as blk
from broadlink.exceptions import (
Expand Down Expand Up @@ -37,9 +37,7 @@ class BroadlinkFlowHandler(ConfigFlow, domain=DOMAIN):

VERSION = 1

def __init__(self) -> None:
"""Initialize the Broadlink flow."""
self.device: blk.Device | None = None
device: blk.Device

async def async_set_device(
self, device: blk.Device, raise_on_progress: bool = True
Expand Down Expand Up @@ -131,8 +129,6 @@ async def async_step_user(
)
return await self.async_step_auth()

if TYPE_CHECKING:
assert self.device
if device.mac == self.device.mac:
await self.async_set_device(device, raise_on_progress=False)
return await self.async_step_auth()
Expand All @@ -158,10 +154,10 @@ async def async_step_user(
errors=errors,
)

async def async_step_auth(self):
async def async_step_auth(self) -> ConfigFlowResult:
"""Authenticate to the device."""
device = self.device
errors = {}
errors: dict[str, str] = {}

try:
await self.hass.async_add_executor_job(device.auth)
Expand Down Expand Up @@ -211,7 +207,11 @@ async def async_step_auth(self):
)
return self.async_show_form(step_id="auth", errors=errors)

async def async_step_reset(self, user_input=None, errors=None):
async def async_step_reset(
self,
user_input: dict[str, Any] | None = None,
errors: dict[str, str] | None = None,
) -> ConfigFlowResult:
"""Guide the user to unlock the device manually.
We are unable to authenticate because the device is locked.
Expand All @@ -234,7 +234,9 @@ async def async_step_reset(self, user_input=None, errors=None):
{CONF_HOST: device.host[0], CONF_TIMEOUT: device.timeout}
)

async def async_step_unlock(self, user_input=None):
async def async_step_unlock(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Unlock the device.
The authentication succeeded, but the device is locked.
Expand Down Expand Up @@ -288,10 +290,12 @@ async def async_step_unlock(self, user_input=None):
},
)

async def async_step_finish(self, user_input=None):
async def async_step_finish(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Choose a name for the device and create config entry."""
device = self.device
errors = {}
errors: dict[str, str] = {}

# Abort reauthentication flow.
self._abort_if_unique_id_configured(
Expand Down
14 changes: 10 additions & 4 deletions homeassistant/components/control4/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import logging
from typing import Any
from typing import TYPE_CHECKING, Any

from aiohttp.client_exceptions import ClientError
from pyControl4.account import C4Account
Expand All @@ -23,7 +23,7 @@
CONF_SCAN_INTERVAL,
CONF_USERNAME,
)
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import aiohttp_client, config_validation as cv
from homeassistant.helpers.device_registry import format_mac
Expand All @@ -49,7 +49,9 @@
class Control4Validator:
"""Validates that config details can be used to authenticate and communicate with Control4."""

def __init__(self, host, username, password, hass):
def __init__(
self, host: str, username: str, password: str, hass: HomeAssistant
) -> None:
"""Initialize."""
self.host = host
self.username = username
Expand Down Expand Up @@ -126,6 +128,8 @@ async def async_step_user(

if not errors:
controller_unique_id = hub.controller_unique_id
if TYPE_CHECKING:
assert hub.controller_unique_id
mac = (controller_unique_id.split("_", 3))[2]
formatted_mac = format_mac(mac)
await self.async_set_unique_id(formatted_mac)
Expand Down Expand Up @@ -160,7 +164,9 @@ def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

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."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/dexcom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

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."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
Expand Down
8 changes: 4 additions & 4 deletions homeassistant/components/ecobee/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class EcobeeFlowHandler(ConfigFlow, domain=DOMAIN):

VERSION = 1

def __init__(self) -> None:
"""Initialize the ecobee flow."""
self._ecobee: Ecobee | None = None
_ecobee: Ecobee

async def async_step_user(
self, user_input: dict[str, Any] | None = None
Expand Down Expand Up @@ -59,7 +57,9 @@ async def async_step_user(
errors=errors,
)

async def async_step_authorize(self, user_input=None):
async def async_step_authorize(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Present the user with the PIN so that the app can be authorized on ecobee.com."""
errors = {}

Expand Down
7 changes: 5 additions & 2 deletions homeassistant/components/emonitor/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ class EmonitorConfigFlow(ConfigFlow, domain=DOMAIN):

VERSION = 1

discovered_info: dict[str, str]

def __init__(self) -> None:
"""Initialize Emonitor ConfigFlow."""
self.discovered_ip: str | None = None
self.discovered_info: dict[str, str] | None = None

async def async_step_user(
self, user_input: dict[str, Any] | None = None
Expand Down Expand Up @@ -87,7 +88,9 @@ async def async_step_dhcp(
return await self.async_step_user()
return await self.async_step_confirm()

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 confirm."""
if user_input is not None:
return self.async_create_entry(
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/emulated_roku/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_NAME
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback

from .const import CONF_LISTEN_PORT, DEFAULT_NAME, DEFAULT_PORT, DOMAIN


@callback
def configured_servers(hass):
def configured_servers(hass: HomeAssistant) -> set[str]:
"""Return a set of the configured servers."""
return {
entry.data[CONF_NAME] for entry in hass.config_entries.async_entries(DOMAIN)
Expand Down
10 changes: 7 additions & 3 deletions homeassistant/components/enocean/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ async def async_step_user(

return await self.async_step_detect()

async def async_step_detect(self, user_input=None):
async def async_step_detect(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Propose a list of detected dongles."""
errors = {}
if user_input is not None:
if user_input[CONF_DEVICE] == self.MANUAL_PATH_VALUE:
return await self.async_step_manual(None)
return await self.async_step_manual()
if await self.validate_enocean_conf(user_input):
return self.create_enocean_entry(user_input)
errors = {CONF_DEVICE: ERROR_INVALID_DONGLE_PATH}
Expand All @@ -64,7 +66,9 @@ async def async_step_detect(self, user_input=None):
errors=errors,
)

async def async_step_manual(self, user_input=None):
async def async_step_manual(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Request manual USB dongle path."""
default_value = None
errors = {}
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/forked_daapd/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize."""
self.config_entry = config_entry

async def async_step_init(self, user_input=None):
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="options", data=user_input)
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/freedompro/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
class Hub:
"""Freedompro Hub class."""

def __init__(self, hass, api_key):
def __init__(self, hass: HomeAssistant, api_key: str) -> None:
"""Freedompro Hub class init."""
self._hass = hass
self._api_key = api_key

async def authenticate(self):
async def authenticate(self) -> dict[str, Any]:
"""Freedompro Hub class authenticate."""
return await get_list(
aiohttp_client.async_get_clientsession(self._hass), self._api_key
)


async def validate_input(hass: HomeAssistant, api_key):
async def validate_input(hass: HomeAssistant, api_key: str) -> None:
"""Validate api key."""
hub = Hub(hass, api_key)
result = await hub.authenticate()
Expand Down

0 comments on commit 681fe34

Please sign in to comment.