Enable strict typing for air_quality component (#50722)

This commit is contained in:
Ruslan Sayfutdinov 2021-05-17 16:20:05 +01:00 committed by GitHub
parent 56774a9f63
commit 72dfa8606e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 38 deletions

View File

@ -6,6 +6,7 @@ homeassistant.components
homeassistant.components.acer_projector.* homeassistant.components.acer_projector.*
homeassistant.components.actiontec.* homeassistant.components.actiontec.*
homeassistant.components.aftership.* homeassistant.components.aftership.*
homeassistant.components.air_quality.*
homeassistant.components.airly.* homeassistant.components.airly.*
homeassistant.components.aladdin_connect.* homeassistant.components.aladdin_connect.*
homeassistant.components.amazon_polly.* homeassistant.components.amazon_polly.*

View File

@ -1,40 +1,45 @@
"""Component for handling Air Quality data for your location.""" """Component for handling Air Quality data for your location."""
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import final from typing import Final, final
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
ATTR_ATTRIBUTION, ATTR_ATTRIBUTION,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
) )
from homeassistant.core import HomeAssistant
from homeassistant.helpers.config_validation import ( # noqa: F401 from homeassistant.helpers.config_validation import ( # noqa: F401
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
PLATFORM_SCHEMA_BASE, PLATFORM_SCHEMA_BASE,
) )
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.typing import ConfigType, StateType
_LOGGER = logging.getLogger(__name__) _LOGGER: Final = logging.getLogger(__name__)
ATTR_AQI = "air_quality_index" ATTR_AQI: Final = "air_quality_index"
ATTR_CO2 = "carbon_dioxide" ATTR_CO2: Final = "carbon_dioxide"
ATTR_CO = "carbon_monoxide" ATTR_CO: Final = "carbon_monoxide"
ATTR_N2O = "nitrogen_oxide" ATTR_N2O: Final = "nitrogen_oxide"
ATTR_NO = "nitrogen_monoxide" ATTR_NO: Final = "nitrogen_monoxide"
ATTR_NO2 = "nitrogen_dioxide" ATTR_NO2: Final = "nitrogen_dioxide"
ATTR_OZONE = "ozone" ATTR_OZONE: Final = "ozone"
ATTR_PM_0_1 = "particulate_matter_0_1" ATTR_PM_0_1: Final = "particulate_matter_0_1"
ATTR_PM_10 = "particulate_matter_10" ATTR_PM_10: Final = "particulate_matter_10"
ATTR_PM_2_5 = "particulate_matter_2_5" ATTR_PM_2_5: Final = "particulate_matter_2_5"
ATTR_SO2 = "sulphur_dioxide" ATTR_SO2: Final = "sulphur_dioxide"
DOMAIN = "air_quality" DOMAIN: Final = "air_quality"
ENTITY_ID_FORMAT = DOMAIN + ".{}" ENTITY_ID_FORMAT: Final = DOMAIN + ".{}"
SCAN_INTERVAL = timedelta(seconds=30) SCAN_INTERVAL: Final = timedelta(seconds=30)
PROP_TO_ATTR = { PROP_TO_ATTR: Final[dict[str, str]] = {
"air_quality_index": ATTR_AQI, "air_quality_index": ATTR_AQI,
"attribution": ATTR_ATTRIBUTION, "attribution": ATTR_ATTRIBUTION,
"carbon_dioxide": ATTR_CO2, "carbon_dioxide": ATTR_CO2,
@ -50,7 +55,7 @@ PROP_TO_ATTR = {
} }
async def async_setup(hass, config): async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the air quality component.""" """Set up the air quality component."""
component = hass.data[DOMAIN] = EntityComponent( component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL _LOGGER, DOMAIN, hass, SCAN_INTERVAL
@ -59,84 +64,86 @@ async def async_setup(hass, config):
return True return True
async def async_setup_entry(hass, entry): async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a config entry.""" """Set up a config entry."""
return await hass.data[DOMAIN].async_setup_entry(entry) component: EntityComponent = hass.data[DOMAIN]
return await component.async_setup_entry(entry)
async def async_unload_entry(hass, entry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
return await hass.data[DOMAIN].async_unload_entry(entry) component: EntityComponent = hass.data[DOMAIN]
return await component.async_unload_entry(entry)
class AirQualityEntity(Entity): class AirQualityEntity(Entity):
"""ABC for air quality data.""" """ABC for air quality data."""
@property @property
def particulate_matter_2_5(self): def particulate_matter_2_5(self) -> StateType:
"""Return the particulate matter 2.5 level.""" """Return the particulate matter 2.5 level."""
raise NotImplementedError() raise NotImplementedError()
@property @property
def particulate_matter_10(self): def particulate_matter_10(self) -> StateType:
"""Return the particulate matter 10 level.""" """Return the particulate matter 10 level."""
return None return None
@property @property
def particulate_matter_0_1(self): def particulate_matter_0_1(self) -> StateType:
"""Return the particulate matter 0.1 level.""" """Return the particulate matter 0.1 level."""
return None return None
@property @property
def air_quality_index(self): def air_quality_index(self) -> StateType:
"""Return the Air Quality Index (AQI).""" """Return the Air Quality Index (AQI)."""
return None return None
@property @property
def ozone(self): def ozone(self) -> StateType:
"""Return the O3 (ozone) level.""" """Return the O3 (ozone) level."""
return None return None
@property @property
def carbon_monoxide(self): def carbon_monoxide(self) -> StateType:
"""Return the CO (carbon monoxide) level.""" """Return the CO (carbon monoxide) level."""
return None return None
@property @property
def carbon_dioxide(self): def carbon_dioxide(self) -> StateType:
"""Return the CO2 (carbon dioxide) level.""" """Return the CO2 (carbon dioxide) level."""
return None return None
@property @property
def attribution(self): def attribution(self) -> StateType:
"""Return the attribution.""" """Return the attribution."""
return None return None
@property @property
def sulphur_dioxide(self): def sulphur_dioxide(self) -> StateType:
"""Return the SO2 (sulphur dioxide) level.""" """Return the SO2 (sulphur dioxide) level."""
return None return None
@property @property
def nitrogen_oxide(self): def nitrogen_oxide(self) -> StateType:
"""Return the N2O (nitrogen oxide) level.""" """Return the N2O (nitrogen oxide) level."""
return None return None
@property @property
def nitrogen_monoxide(self): def nitrogen_monoxide(self) -> StateType:
"""Return the NO (nitrogen monoxide) level.""" """Return the NO (nitrogen monoxide) level."""
return None return None
@property @property
def nitrogen_dioxide(self): def nitrogen_dioxide(self) -> StateType:
"""Return the NO2 (nitrogen dioxide) level.""" """Return the NO2 (nitrogen dioxide) level."""
return None return None
@final @final
@property @property
def state_attributes(self): def state_attributes(self) -> dict[str, str | int | float]:
"""Return the state attributes.""" """Return the state attributes."""
data = {} data: dict[str, str | int | float] = {}
for prop, attr in PROP_TO_ATTR.items(): for prop, attr in PROP_TO_ATTR.items():
value = getattr(self, prop) value = getattr(self, prop)
@ -146,11 +153,11 @@ class AirQualityEntity(Entity):
return data return data
@property @property
def state(self): def state(self) -> StateType:
"""Return the current state.""" """Return the current state."""
return self.particulate_matter_2_5 return self.particulate_matter_2_5
@property @property
def unit_of_measurement(self): def unit_of_measurement(self) -> str:
"""Return the unit of measurement of this entity.""" """Return the unit of measurement of this entity."""
return CONCENTRATION_MICROGRAMS_PER_CUBIC_METER return CONCENTRATION_MICROGRAMS_PER_CUBIC_METER

View File

@ -77,6 +77,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.air_quality.*]
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.airly.*] [mypy-homeassistant.components.airly.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true