Enable strict typing for airthings (#106814)

This commit is contained in:
Marc Mueller 2024-01-02 11:38:20 +01:00 committed by GitHub
parent d89683f980
commit 8903aecb77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 11 deletions

View File

@ -51,6 +51,7 @@ homeassistant.components.air_quality.*
homeassistant.components.airly.*
homeassistant.components.airnow.*
homeassistant.components.airq.*
homeassistant.components.airthings.*
homeassistant.components.airthings_ble.*
homeassistant.components.airvisual.*
homeassistant.components.airvisual_pro.*

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from datetime import timedelta
import logging
from airthings import Airthings, AirthingsError
from airthings import Airthings, AirthingsDevice, AirthingsError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ID, Platform
@ -19,6 +19,8 @@ _LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.SENSOR]
SCAN_INTERVAL = timedelta(minutes=6)
AirthingsDataCoordinatorType = DataUpdateCoordinator[dict[str, AirthingsDevice]]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Airthings from a config entry."""
@ -30,10 +32,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async_get_clientsession(hass),
)
async def _update_method():
async def _update_method() -> dict[str, AirthingsDevice]:
"""Get the latest data from Airthings."""
try:
return await airthings.update_devices()
return await airthings.update_devices() # type: ignore[no-any-return]
except AirthingsError as err:
raise UpdateFailed(f"Unable to fetch data: {err}") from err

View File

@ -24,11 +24,9 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import AirthingsDataCoordinatorType
from .const import DOMAIN
SENSORS: dict[str, SensorEntityDescription] = {
@ -108,7 +106,7 @@ async def async_setup_entry(
) -> None:
"""Set up the Airthings sensor."""
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
coordinator: AirthingsDataCoordinatorType = hass.data[DOMAIN][entry.entry_id]
entities = [
AirthingsHeaterEnergySensor(
coordinator,
@ -122,7 +120,9 @@ async def async_setup_entry(
async_add_entities(entities)
class AirthingsHeaterEnergySensor(CoordinatorEntity, SensorEntity):
class AirthingsHeaterEnergySensor(
CoordinatorEntity[AirthingsDataCoordinatorType], SensorEntity
):
"""Representation of a Airthings Sensor device."""
_attr_state_class = SensorStateClass.MEASUREMENT
@ -130,7 +130,7 @@ class AirthingsHeaterEnergySensor(CoordinatorEntity, SensorEntity):
def __init__(
self,
coordinator: DataUpdateCoordinator,
coordinator: AirthingsDataCoordinatorType,
airthings_device: AirthingsDevice,
entity_description: SensorEntityDescription,
) -> None:
@ -155,4 +155,4 @@ class AirthingsHeaterEnergySensor(CoordinatorEntity, SensorEntity):
@property
def native_value(self) -> StateType:
"""Return the value reported by the sensor."""
return self.coordinator.data[self._id].sensors[self.entity_description.key]
return self.coordinator.data[self._id].sensors[self.entity_description.key] # type: ignore[no-any-return]

View File

@ -270,6 +270,16 @@ disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.airthings.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.airthings_ble.*]
check_untyped_defs = true
disallow_incomplete_defs = true