1
mirror of https://github.com/home-assistant/core synced 2024-09-25 00:41:32 +02:00

Add strict type annotations to ampio (#50699)

This commit is contained in:
Michael 2021-05-17 11:14:47 +02:00 committed by GitHub
parent 2d29959a52
commit f9c7474a78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 19 deletions

View File

@ -8,6 +8,7 @@ homeassistant.components.actiontec.*
homeassistant.components.aftership.*
homeassistant.components.airly.*
homeassistant.components.aladdin_connect.*
homeassistant.components.ampio.*
homeassistant.components.automation.*
homeassistant.components.binary_sensor.*
homeassistant.components.bond.*

View File

@ -1,28 +1,39 @@
"""Support for Ampio Air Quality data."""
from datetime import timedelta
from __future__ import annotations
import logging
from typing import Final
from asmog import AmpioSmog
import voluptuous as vol
from homeassistant.components.air_quality import PLATFORM_SCHEMA, AirQualityEntity
from homeassistant.components.air_quality import (
PLATFORM_SCHEMA as BASE_PLATFORM_SCHEMA,
AirQualityEntity,
)
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__)
from .const import ATTRIBUTION, CONF_STATION_ID, SCAN_INTERVAL
ATTRIBUTION = "Data provided by Ampio"
CONF_STATION_ID = "station_id"
SCAN_INTERVAL = timedelta(minutes=10)
_LOGGER: Final = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
PLATFORM_SCHEMA: Final = BASE_PLATFORM_SCHEMA.extend(
{vol.Required(CONF_STATION_ID): cv.string, vol.Optional(CONF_NAME): cv.string}
)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Ampio Smog air quality platform."""
name = config.get(CONF_NAME)
@ -43,38 +54,40 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class AmpioSmogQuality(AirQualityEntity):
"""Implementation of an Ampio Smog air quality entity."""
def __init__(self, api, station_id, name):
def __init__(
self, api: AmpioSmogMapData, station_id: str, name: str | None
) -> None:
"""Initialize the air quality entity."""
self._ampio = api
self._station_id = station_id
self._name = name or api.api.name
@property
def name(self):
def name(self) -> str:
"""Return the name of the air quality entity."""
return self._name
@property
def unique_id(self):
def unique_id(self) -> str:
"""Return unique_name."""
return f"ampio_smog_{self._station_id}"
@property
def particulate_matter_2_5(self):
def particulate_matter_2_5(self) -> str | None:
"""Return the particulate matter 2.5 level."""
return self._ampio.api.pm2_5
return self._ampio.api.pm2_5 # type: ignore[no-any-return]
@property
def particulate_matter_10(self):
def particulate_matter_10(self) -> str | None:
"""Return the particulate matter 10 level."""
return self._ampio.api.pm10
return self._ampio.api.pm10 # type: ignore[no-any-return]
@property
def attribution(self):
def attribution(self) -> str:
"""Return the attribution."""
return ATTRIBUTION
async def async_update(self):
async def async_update(self) -> None:
"""Get the latest data from the AmpioMap API."""
await self._ampio.async_update()
@ -82,11 +95,11 @@ class AmpioSmogQuality(AirQualityEntity):
class AmpioSmogMapData:
"""Get the latest data and update the states."""
def __init__(self, api):
def __init__(self, api: AmpioSmog) -> None:
"""Initialize the data object."""
self.api = api
@Throttle(SCAN_INTERVAL)
async def async_update(self):
async def async_update(self) -> None:
"""Get the latest data from AmpioMap."""
await self.api.get_data()

View File

@ -0,0 +1,7 @@
"""Constants for Ampio Air Quality platform."""
from datetime import timedelta
from typing import Final
ATTRIBUTION: Final = "Data provided by Ampio"
CONF_STATION_ID: Final = "station_id"
SCAN_INTERVAL: Final = timedelta(minutes=10)

View File

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