Clean up Awair const (#95135)

pull/95309/head
Joost Lekkerkerker 2023-06-26 23:22:43 +02:00 committed by GitHub
parent 3635508a08
commit b02cb56988
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 134 additions and 130 deletions

View File

@ -2,16 +2,22 @@
from __future__ import annotations
from asyncio import gather
from dataclasses import dataclass
from datetime import timedelta
from aiohttp import ClientSession
from async_timeout import timeout
from python_awair import Awair, AwairLocal
from python_awair.air_data import AirData
from python_awair.devices import AwairBaseDevice, AwairLocalDevice
from python_awair.exceptions import AuthError, AwairError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, Platform
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_HOST,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -23,7 +29,6 @@ from .const import (
LOGGER,
UPDATE_INTERVAL_CLOUD,
UPDATE_INTERVAL_LOCAL,
AwairResult,
)
PLATFORMS = [Platform.SENSOR]
@ -72,6 +77,14 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
return unload_ok
@dataclass
class AwairResult:
"""Wrapper class to hold an awair device and set of air data."""
device: AwairBaseDevice
air_data: AirData
class AwairDataUpdateCoordinator(DataUpdateCoordinator[dict[str, AwairResult]]):
"""Define a wrapper class to update Awair data."""

View File

@ -1,28 +1,9 @@
"""Constants for the Awair component."""
from __future__ import annotations
from dataclasses import dataclass
from datetime import timedelta
import logging
from python_awair.air_data import AirData
from python_awair.devices import AwairBaseDevice
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
LIGHT_LUX,
PERCENTAGE,
UnitOfSoundPressure,
UnitOfTemperature,
)
API_CO2 = "carbon_dioxide"
API_DUST = "dust"
API_HUMID = "humidity"
@ -39,109 +20,7 @@ ATTRIBUTION = "Awair air quality sensor"
DOMAIN = "awair"
DUST_ALIASES = [API_PM25, API_PM10]
LOGGER = logging.getLogger(__package__)
UPDATE_INTERVAL_CLOUD = timedelta(minutes=5)
UPDATE_INTERVAL_LOCAL = timedelta(seconds=30)
@dataclass
class AwairRequiredKeysMixin:
"""Mixin for required keys."""
unique_id_tag: str
@dataclass
class AwairSensorEntityDescription(SensorEntityDescription, AwairRequiredKeysMixin):
"""Describes Awair sensor entity."""
SENSOR_TYPE_SCORE = AwairSensorEntityDescription(
key=API_SCORE,
icon="mdi:blur",
native_unit_of_measurement=PERCENTAGE,
name="Score",
unique_id_tag="score", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
)
SENSOR_TYPES: tuple[AwairSensorEntityDescription, ...] = (
AwairSensorEntityDescription(
key=API_HUMID,
device_class=SensorDeviceClass.HUMIDITY,
native_unit_of_measurement=PERCENTAGE,
name="Humidity",
unique_id_tag="HUMID", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_LUX,
device_class=SensorDeviceClass.ILLUMINANCE,
native_unit_of_measurement=LIGHT_LUX,
name="Illuminance",
unique_id_tag="illuminance",
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_SPL_A,
device_class=SensorDeviceClass.SOUND_PRESSURE,
native_unit_of_measurement=UnitOfSoundPressure.WEIGHTED_DECIBEL_A,
name="Sound level",
unique_id_tag="sound_level",
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_VOC,
icon="mdi:molecule",
native_unit_of_measurement=CONCENTRATION_PARTS_PER_BILLION,
name="Volatile organic compounds",
unique_id_tag="VOC", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_TEMP,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
name="Temperature",
unique_id_tag="TEMP", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_CO2,
device_class=SensorDeviceClass.CO2,
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
name="Carbon dioxide",
unique_id_tag="CO2", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
)
SENSOR_TYPES_DUST: tuple[AwairSensorEntityDescription, ...] = (
AwairSensorEntityDescription(
key=API_PM25,
device_class=SensorDeviceClass.PM25,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
name="PM2.5",
unique_id_tag="PM25", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_PM10,
device_class=SensorDeviceClass.PM10,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
name="PM10",
unique_id_tag="PM10", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
)
@dataclass
class AwairResult:
"""Wrapper class to hold an awair device and set of air data."""
device: AwairBaseDevice
air_data: AirData

View File

@ -1,14 +1,30 @@
"""Support for Awair sensors."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, cast
from python_awair.air_data import AirData
from python_awair.devices import AwairBaseDevice, AwairLocalDevice
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_CONNECTIONS, ATTR_SW_VERSION
from homeassistant.const import (
ATTR_CONNECTIONS,
ATTR_SW_VERSION,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
LIGHT_LUX,
PERCENTAGE,
UnitOfSoundPressure,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity import DeviceInfo
@ -17,18 +33,112 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import AwairDataUpdateCoordinator, AwairResult
from .const import (
API_CO2,
API_DUST,
API_HUMID,
API_LUX,
API_PM10,
API_PM25,
API_SCORE,
API_SPL_A,
API_TEMP,
API_VOC,
ATTRIBUTION,
DOMAIN,
DUST_ALIASES,
SENSOR_TYPE_SCORE,
SENSOR_TYPES,
SENSOR_TYPES_DUST,
AwairSensorEntityDescription,
)
DUST_ALIASES = [API_PM25, API_PM10]
@dataclass
class AwairRequiredKeysMixin:
"""Mixin for required keys."""
unique_id_tag: str
@dataclass
class AwairSensorEntityDescription(SensorEntityDescription, AwairRequiredKeysMixin):
"""Describes Awair sensor entity."""
SENSOR_TYPE_SCORE = AwairSensorEntityDescription(
key=API_SCORE,
icon="mdi:blur",
native_unit_of_measurement=PERCENTAGE,
name="Score",
unique_id_tag="score", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
)
SENSOR_TYPES: tuple[AwairSensorEntityDescription, ...] = (
AwairSensorEntityDescription(
key=API_HUMID,
device_class=SensorDeviceClass.HUMIDITY,
native_unit_of_measurement=PERCENTAGE,
name="Humidity",
unique_id_tag="HUMID", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_LUX,
device_class=SensorDeviceClass.ILLUMINANCE,
native_unit_of_measurement=LIGHT_LUX,
name="Illuminance",
unique_id_tag="illuminance",
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_SPL_A,
device_class=SensorDeviceClass.SOUND_PRESSURE,
native_unit_of_measurement=UnitOfSoundPressure.WEIGHTED_DECIBEL_A,
name="Sound level",
unique_id_tag="sound_level",
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_VOC,
icon="mdi:molecule",
native_unit_of_measurement=CONCENTRATION_PARTS_PER_BILLION,
name="Volatile organic compounds",
unique_id_tag="VOC", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_TEMP,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
name="Temperature",
unique_id_tag="TEMP", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_CO2,
device_class=SensorDeviceClass.CO2,
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
name="Carbon dioxide",
unique_id_tag="CO2", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
)
SENSOR_TYPES_DUST: tuple[AwairSensorEntityDescription, ...] = (
AwairSensorEntityDescription(
key=API_PM25,
device_class=SensorDeviceClass.PM25,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
name="PM2.5",
unique_id_tag="PM25", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
AwairSensorEntityDescription(
key=API_PM10,
device_class=SensorDeviceClass.PM10,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
name="PM10",
unique_id_tag="PM10", # matches legacy format
state_class=SensorStateClass.MEASUREMENT,
),
)

View File

@ -11,6 +11,8 @@ from homeassistant.components.awair.const import (
API_SPL_A,
API_TEMP,
API_VOC,
)
from homeassistant.components.awair.sensor import (
SENSOR_TYPE_SCORE,
SENSOR_TYPES,
SENSOR_TYPES_DUST,