From b02cb569881e4ba4357f7a9d311e4bc86a4a4366 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Mon, 26 Jun 2023 23:22:43 +0200 Subject: [PATCH] Clean up Awair const (#95135) --- homeassistant/components/awair/__init__.py | 17 ++- homeassistant/components/awair/const.py | 121 -------------------- homeassistant/components/awair/sensor.py | 124 +++++++++++++++++++-- tests/components/awair/test_sensor.py | 2 + 4 files changed, 134 insertions(+), 130 deletions(-) diff --git a/homeassistant/components/awair/__init__.py b/homeassistant/components/awair/__init__.py index cef2c7d1fd4..dca885ffe0d 100644 --- a/homeassistant/components/awair/__init__.py +++ b/homeassistant/components/awair/__init__.py @@ -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.""" diff --git a/homeassistant/components/awair/const.py b/homeassistant/components/awair/const.py index d483df64298..19341ab6050 100644 --- a/homeassistant/components/awair/const.py +++ b/homeassistant/components/awair/const.py @@ -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 diff --git a/homeassistant/components/awair/sensor.py b/homeassistant/components/awair/sensor.py index f42a46999fb..e771c29d45b 100644 --- a/homeassistant/components/awair/sensor.py +++ b/homeassistant/components/awair/sensor.py @@ -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, + ), ) diff --git a/tests/components/awair/test_sensor.py b/tests/components/awair/test_sensor.py index 287eb72d21e..2c8aa78f791 100644 --- a/tests/components/awair/test_sensor.py +++ b/tests/components/awair/test_sensor.py @@ -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,