Skip to content

Commit

Permalink
Enable strict typing for air_quality component (home-assistant#50722)
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI authored May 17, 2021
1 parent 56774a9 commit 72dfa86
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 38 deletions.
1 change: 1 addition & 0 deletions .strict-typing
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ homeassistant.components
homeassistant.components.acer_projector.*
homeassistant.components.actiontec.*
homeassistant.components.aftership.*
homeassistant.components.air_quality.*
homeassistant.components.airly.*
homeassistant.components.aladdin_connect.*
homeassistant.components.amazon_polly.*
Expand Down
83 changes: 45 additions & 38 deletions homeassistant/components/air_quality/__init__.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
"""Component for handling Air Quality data for your location."""
from __future__ import annotations

from datetime import timedelta
import logging
from typing import final
from typing import Final, final

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.config_validation import ( # noqa: F401
PLATFORM_SCHEMA,
PLATFORM_SCHEMA_BASE,
)
from homeassistant.helpers.entity import Entity
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_CO2 = "carbon_dioxide"
ATTR_CO = "carbon_monoxide"
ATTR_N2O = "nitrogen_oxide"
ATTR_NO = "nitrogen_monoxide"
ATTR_NO2 = "nitrogen_dioxide"
ATTR_OZONE = "ozone"
ATTR_PM_0_1 = "particulate_matter_0_1"
ATTR_PM_10 = "particulate_matter_10"
ATTR_PM_2_5 = "particulate_matter_2_5"
ATTR_SO2 = "sulphur_dioxide"
ATTR_AQI: Final = "air_quality_index"
ATTR_CO2: Final = "carbon_dioxide"
ATTR_CO: Final = "carbon_monoxide"
ATTR_N2O: Final = "nitrogen_oxide"
ATTR_NO: Final = "nitrogen_monoxide"
ATTR_NO2: Final = "nitrogen_dioxide"
ATTR_OZONE: Final = "ozone"
ATTR_PM_0_1: Final = "particulate_matter_0_1"
ATTR_PM_10: Final = "particulate_matter_10"
ATTR_PM_2_5: Final = "particulate_matter_2_5"
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,
"attribution": ATTR_ATTRIBUTION,
"carbon_dioxide": ATTR_CO2,
Expand All @@ -50,7 +55,7 @@
}


async def async_setup(hass, config):
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the air quality component."""
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
Expand All @@ -59,84 +64,86 @@ async def async_setup(hass, config):
return True


async def async_setup_entry(hass, entry):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""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."""
return await hass.data[DOMAIN].async_unload_entry(entry)
component: EntityComponent = hass.data[DOMAIN]
return await component.async_unload_entry(entry)


class AirQualityEntity(Entity):
"""ABC for air quality data."""

@property
def particulate_matter_2_5(self):
def particulate_matter_2_5(self) -> StateType:
"""Return the particulate matter 2.5 level."""
raise NotImplementedError()

@property
def particulate_matter_10(self):
def particulate_matter_10(self) -> StateType:
"""Return the particulate matter 10 level."""
return None

@property
def particulate_matter_0_1(self):
def particulate_matter_0_1(self) -> StateType:
"""Return the particulate matter 0.1 level."""
return None

@property
def air_quality_index(self):
def air_quality_index(self) -> StateType:
"""Return the Air Quality Index (AQI)."""
return None

@property
def ozone(self):
def ozone(self) -> StateType:
"""Return the O3 (ozone) level."""
return None

@property
def carbon_monoxide(self):
def carbon_monoxide(self) -> StateType:
"""Return the CO (carbon monoxide) level."""
return None

@property
def carbon_dioxide(self):
def carbon_dioxide(self) -> StateType:
"""Return the CO2 (carbon dioxide) level."""
return None

@property
def attribution(self):
def attribution(self) -> StateType:
"""Return the attribution."""
return None

@property
def sulphur_dioxide(self):
def sulphur_dioxide(self) -> StateType:
"""Return the SO2 (sulphur dioxide) level."""
return None

@property
def nitrogen_oxide(self):
def nitrogen_oxide(self) -> StateType:
"""Return the N2O (nitrogen oxide) level."""
return None

@property
def nitrogen_monoxide(self):
def nitrogen_monoxide(self) -> StateType:
"""Return the NO (nitrogen monoxide) level."""
return None

@property
def nitrogen_dioxide(self):
def nitrogen_dioxide(self) -> StateType:
"""Return the NO2 (nitrogen dioxide) level."""
return None

@final
@property
def state_attributes(self):
def state_attributes(self) -> dict[str, str | int | float]:
"""Return the state attributes."""
data = {}
data: dict[str, str | int | float] = {}

for prop, attr in PROP_TO_ATTR.items():
value = getattr(self, prop)
Expand All @@ -146,11 +153,11 @@ def state_attributes(self):
return data

@property
def state(self):
def state(self) -> StateType:
"""Return the current state."""
return self.particulate_matter_2_5

@property
def unit_of_measurement(self):
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of this entity."""
return CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
11 changes: 11 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ no_implicit_optional = true
warn_return_any = 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.*]
check_untyped_defs = true
disallow_incomplete_defs = true
Expand Down

0 comments on commit 72dfa86

Please sign in to comment.