Add strict typing to Luftdaten (#62588)

This commit is contained in:
Franck Nijhof 2021-12-22 12:20:35 +01:00 committed by GitHub
parent 3323263c94
commit 4805b67300
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 15 deletions

View file

@ -78,6 +78,7 @@ homeassistant.components.light.*
homeassistant.components.local_ip.* homeassistant.components.local_ip.*
homeassistant.components.lock.* homeassistant.components.lock.*
homeassistant.components.lookin.* homeassistant.components.lookin.*
homeassistant.components.luftdaten.*
homeassistant.components.mailbox.* homeassistant.components.mailbox.*
homeassistant.components.media_player.* homeassistant.components.media_player.*
homeassistant.components.modbus.* homeassistant.components.modbus.*

View file

@ -33,7 +33,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
luftdaten = Luftdaten(entry.data[CONF_SENSOR_ID]) luftdaten = Luftdaten(entry.data[CONF_SENSOR_ID])
async def async_update() -> dict[Any, Any]: async def async_update() -> dict[str, float | int]:
"""Update sensor/binary sensor data.""" """Update sensor/binary sensor data."""
try: try:
await luftdaten.get_data() await luftdaten.get_data()
@ -43,7 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if not luftdaten.values: if not luftdaten.values:
raise UpdateFailed("Did not receive sensor data from luftdaten.info") raise UpdateFailed("Did not receive sensor data from luftdaten.info")
data = luftdaten.values data: dict[str, float | int] = luftdaten.values
data.update(luftdaten.meta) data.update(luftdaten.meta)
return data return data

View file

@ -1,5 +1,7 @@
"""Config flow to configure the Luftdaten component.""" """Config flow to configure the Luftdaten component."""
from collections import OrderedDict from __future__ import annotations
from typing import Any
from luftdaten import Luftdaten from luftdaten import Luftdaten
from luftdaten.exceptions import LuftdatenConnectionError from luftdaten.exceptions import LuftdatenConnectionError
@ -13,6 +15,7 @@ from homeassistant.const import (
CONF_SHOW_ON_MAP, CONF_SHOW_ON_MAP,
) )
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from .const import CONF_SENSOR_ID, DEFAULT_SCAN_INTERVAL, DOMAIN from .const import CONF_SENSOR_ID, DEFAULT_SCAN_INTERVAL, DOMAIN
@ -24,17 +27,22 @@ class LuftDatenFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
@callback @callback
def _show_form(self, errors=None): def _show_form(self, errors: dict[str, str] | None = None) -> FlowResult:
"""Show the form to the user.""" """Show the form to the user."""
data_schema = OrderedDict()
data_schema[vol.Required(CONF_SENSOR_ID)] = cv.positive_int
data_schema[vol.Optional(CONF_SHOW_ON_MAP, default=False)] = bool
return self.async_show_form( return self.async_show_form(
step_id="user", data_schema=vol.Schema(data_schema), errors=errors or {} step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_SENSOR_ID): cv.positive_int,
vol.Optional(CONF_SHOW_ON_MAP, default=False): bool,
}
),
errors=errors or {},
) )
async def async_step_user(self, user_input=None): async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the start of the config flow.""" """Handle the start of the config flow."""
if not user_input: if not user_input:

View file

@ -1,6 +1,8 @@
"""Support for Luftdaten sensors.""" """Support for Luftdaten sensors."""
from __future__ import annotations from __future__ import annotations
from typing import cast
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
@ -131,4 +133,4 @@ class LuftdatenSensor(CoordinatorEntity, SensorEntity):
or (value := self.coordinator.data.get(self.entity_description.key)) is None or (value := self.coordinator.data.get(self.entity_description.key)) is None
): ):
return None return None
return value return cast(float, value)

View file

@ -869,6 +869,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.luftdaten.*]
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.mailbox.*] [mypy-homeassistant.components.mailbox.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true
@ -1919,9 +1930,6 @@ ignore_errors = true
[mypy-homeassistant.components.lovelace.*] [mypy-homeassistant.components.lovelace.*]
ignore_errors = true ignore_errors = true
[mypy-homeassistant.components.luftdaten.*]
ignore_errors = true
[mypy-homeassistant.components.lutron_caseta.*] [mypy-homeassistant.components.lutron_caseta.*]
ignore_errors = true ignore_errors = true

View file

@ -67,7 +67,6 @@ IGNORED_MODULES: Final[list[str]] = [
"homeassistant.components.litejet.*", "homeassistant.components.litejet.*",
"homeassistant.components.litterrobot.*", "homeassistant.components.litterrobot.*",
"homeassistant.components.lovelace.*", "homeassistant.components.lovelace.*",
"homeassistant.components.luftdaten.*",
"homeassistant.components.lutron_caseta.*", "homeassistant.components.lutron_caseta.*",
"homeassistant.components.lyric.*", "homeassistant.components.lyric.*",
"homeassistant.components.melcloud.*", "homeassistant.components.melcloud.*",