Skip to content

Commit

Permalink
Strictly type tradfri config_flow.py (home-assistant#56391)
Browse files Browse the repository at this point in the history
* Strictly type config_flow.py.

* Review comments.
  • Loading branch information
janiversen authored Sep 21, 2021
1 parent 56b66d5 commit 34de74d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions .strict-typing
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ homeassistant.components.systemmonitor.*
homeassistant.components.tag.*
homeassistant.components.tcp.*
homeassistant.components.tile.*
homeassistant.components.tradfri.*
homeassistant.components.tts.*
homeassistant.components.upcloud.*
homeassistant.components.uptime.*
Expand Down
34 changes: 24 additions & 10 deletions homeassistant/components/tradfri/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Config flow for Tradfri."""
from __future__ import annotations

import asyncio
from typing import Any
from uuid import uuid4

import async_timeout
Expand All @@ -8,6 +11,9 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.typing import DiscoveryInfoType

from .const import (
CONF_GATEWAY_ID,
Expand All @@ -23,7 +29,7 @@
class AuthError(Exception):
"""Exception if authentication occurs."""

def __init__(self, code):
def __init__(self, code: str) -> None:
"""Initialize exception."""
super().__init__()
self.code = code
Expand All @@ -34,18 +40,22 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):

VERSION = 1

def __init__(self):
def __init__(self) -> None:
"""Initialize flow."""
self._host = None
self._import_groups = False

async def async_step_user(self, user_input=None):
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initialized by the user."""
return await self.async_step_auth()

async def async_step_auth(self, user_input=None):
async def async_step_auth(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the authentication with a gateway."""
errors = {}
errors: dict[str, str] = {}

if user_input is not None:
host = user_input.get(CONF_HOST, self._host)
Expand Down Expand Up @@ -82,7 +92,7 @@ async def async_step_auth(self, user_input=None):
step_id="auth", data_schema=vol.Schema(fields), errors=errors
)

async def async_step_homekit(self, discovery_info):
async def async_step_homekit(self, discovery_info: DiscoveryInfoType) -> 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"]})
Expand All @@ -104,7 +114,7 @@ async def async_step_homekit(self, discovery_info):
self._host = host
return await self.async_step_auth()

async def async_step_import(self, user_input):
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
"""Import a config entry."""
self._async_abort_entries_match({CONF_HOST: user_input["host"]})

Expand All @@ -131,7 +141,7 @@ async def async_step_import(self, user_input):
self._host = user_input["host"]
return await self.async_step_auth()

async def _entry_from_data(self, data):
async def _entry_from_data(self, data: dict[str, Any]) -> FlowResult:
"""Create an entry from data."""
host = data[CONF_HOST]
gateway_id = data[CONF_GATEWAY_ID]
Expand All @@ -154,7 +164,9 @@ async def _entry_from_data(self, data):
return self.async_create_entry(title=host, data=data)


async def authenticate(hass, host, security_code):
async def authenticate(
hass: HomeAssistant, host: str, security_code: str
) -> dict[str, str | bool]:
"""Authenticate with a Tradfri hub."""

identity = uuid4().hex
Expand All @@ -174,7 +186,9 @@ async def authenticate(hass, host, security_code):
return await get_gateway_info(hass, host, identity, key)


async def get_gateway_info(hass, host, identity, key):
async def get_gateway_info(
hass: HomeAssistant, host: str, identity: str, key: str
) -> dict[str, str | bool]:
"""Return info for the gateway."""

try:
Expand Down
11 changes: 11 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,17 @@ no_implicit_optional = true
warn_return_any = true
warn_unreachable = true

[mypy-homeassistant.components.tradfri.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
no_implicit_optional = true
warn_return_any = true
warn_unreachable = true

[mypy-homeassistant.components.tts.*]
check_untyped_defs = true
disallow_incomplete_defs = true
Expand Down

0 comments on commit 34de74d

Please sign in to comment.