Use constants with TypedDict in Nettigo Air Monitor integration (#50883)

* Use constants with TypedDict

* Sensor names as consts
This commit is contained in:
Maciej Bieniek 2021-05-20 12:38:46 +02:00 committed by GitHub
parent be6a1bf096
commit aaae4cfc8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 133 additions and 100 deletions

View file

@ -10,7 +10,14 @@ from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import NAMDataUpdateCoordinator from . import NAMDataUpdateCoordinator
from .const import AIR_QUALITY_SENSORS, DEFAULT_NAME, DOMAIN, SUFFIX_P1, SUFFIX_P2 from .const import (
AIR_QUALITY_SENSORS,
ATTR_MHZ14A_CARBON_DIOXIDE,
DEFAULT_NAME,
DOMAIN,
SUFFIX_P1,
SUFFIX_P2,
)
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
@ -61,7 +68,9 @@ class NAMAirQuality(CoordinatorEntity, AirQualityEntity):
@property @property
def carbon_dioxide(self) -> StateType: def carbon_dioxide(self) -> StateType:
"""Return the particulate matter 10 level.""" """Return the particulate matter 10 level."""
return round_state(getattr(self.coordinator.data, "conc_co2_ppm", None)) return round_state(
getattr(self.coordinator.data, ATTR_MHZ14A_CARBON_DIOXIDE, None)
)
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
@ -82,7 +91,7 @@ class NAMAirQuality(CoordinatorEntity, AirQualityEntity):
# sensors. For this reason, we mark entities for which data is missing as # sensors. For this reason, we mark entities for which data is missing as
# unavailable. # unavailable.
return available and bool( return available and bool(
getattr(self.coordinator.data, f"{self.sensor_type}_p2", None) getattr(self.coordinator.data, f"{self.sensor_type}{SUFFIX_P2}", None)
) )

View file

@ -5,6 +5,8 @@ from datetime import timedelta
from typing import Final from typing import Final
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ICON,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_PRESSURE, DEVICE_CLASS_PRESSURE,
@ -19,6 +21,27 @@ from homeassistant.const import (
from .model import SensorDescription from .model import SensorDescription
ATTR_BME280_HUMIDITY: Final = "bme280_humidity"
ATTR_BME280_PRESSURE: Final = "bme280_pressure"
ATTR_BME280_TEMPERATURE: Final = "bme280_temperature"
ATTR_BMP280_PRESSURE: Final = "bmp280_pressure"
ATTR_BMP280_TEMPERATURE: Final = "bmp280_temperature"
ATTR_DHT22_HUMIDITY: Final = "humidity"
ATTR_DHT22_TEMPERATURE: Final = "temperature"
ATTR_HECA_HUMIDITY: Final = "heca_humidity"
ATTR_HECA_TEMPERATURE: Final = "heca_temperature"
ATTR_MHZ14A_CARBON_DIOXIDE: Final = "conc_co2_ppm"
ATTR_SHT3X_HUMIDITY: Final = "sht3x_humidity"
ATTR_SHT3X_TEMPERATURE: Final = "sht3x_temperature"
ATTR_SIGNAL_STRENGTH: Final = "signal"
ATTR_SPS30_P0: Final = "sps30_p0"
ATTR_SPS30_P4: Final = "sps30_p4"
ATTR_UPTIME: Final = "uptime"
ATTR_ENABLED: Final = "enabled"
ATTR_LABEL: Final = "label"
ATTR_UNIT: Final = "unit"
DEFAULT_NAME: Final = "Nettigo Air Monitor" DEFAULT_NAME: Final = "Nettigo Air Monitor"
DEFAULT_UPDATE_INTERVAL: Final = timedelta(minutes=6) DEFAULT_UPDATE_INTERVAL: Final = timedelta(minutes=6)
DOMAIN: Final = "nam" DOMAIN: Final = "nam"
@ -30,109 +53,109 @@ SUFFIX_P2: Final = "_p2"
AIR_QUALITY_SENSORS: Final[dict[str, str]] = {"sds": "SDS011", "sps30": "SPS30"} AIR_QUALITY_SENSORS: Final[dict[str, str]] = {"sds": "SDS011", "sps30": "SPS30"}
SENSORS: Final[dict[str, SensorDescription]] = { SENSORS: Final[dict[str, SensorDescription]] = {
"bme280_humidity": { ATTR_BME280_HUMIDITY: {
"label": f"{DEFAULT_NAME} BME280 Humidity", ATTR_LABEL: f"{DEFAULT_NAME} BME280 Humidity",
"unit": PERCENTAGE, ATTR_UNIT: PERCENTAGE,
"device_class": DEVICE_CLASS_HUMIDITY, ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"bme280_pressure": { ATTR_BME280_PRESSURE: {
"label": f"{DEFAULT_NAME} BME280 Pressure", ATTR_LABEL: f"{DEFAULT_NAME} BME280 Pressure",
"unit": PRESSURE_HPA, ATTR_UNIT: PRESSURE_HPA,
"device_class": DEVICE_CLASS_PRESSURE, ATTR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"bme280_temperature": { ATTR_BME280_TEMPERATURE: {
"label": f"{DEFAULT_NAME} BME280 Temperature", ATTR_LABEL: f"{DEFAULT_NAME} BME280 Temperature",
"unit": TEMP_CELSIUS, ATTR_UNIT: TEMP_CELSIUS,
"device_class": DEVICE_CLASS_TEMPERATURE, ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"bmp280_pressure": { ATTR_BMP280_PRESSURE: {
"label": f"{DEFAULT_NAME} BMP280 Pressure", ATTR_LABEL: f"{DEFAULT_NAME} BMP280 Pressure",
"unit": PRESSURE_HPA, ATTR_UNIT: PRESSURE_HPA,
"device_class": DEVICE_CLASS_PRESSURE, ATTR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"bmp280_temperature": { ATTR_BMP280_TEMPERATURE: {
"label": f"{DEFAULT_NAME} BMP280 Temperature", ATTR_LABEL: f"{DEFAULT_NAME} BMP280 Temperature",
"unit": TEMP_CELSIUS, ATTR_UNIT: TEMP_CELSIUS,
"device_class": DEVICE_CLASS_TEMPERATURE, ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"heca_humidity": { ATTR_HECA_HUMIDITY: {
"label": f"{DEFAULT_NAME} HECA Humidity", ATTR_LABEL: f"{DEFAULT_NAME} HECA Humidity",
"unit": PERCENTAGE, ATTR_UNIT: PERCENTAGE,
"device_class": DEVICE_CLASS_HUMIDITY, ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"heca_temperature": { ATTR_HECA_TEMPERATURE: {
"label": f"{DEFAULT_NAME} HECA Temperature", ATTR_LABEL: f"{DEFAULT_NAME} HECA Temperature",
"unit": TEMP_CELSIUS, ATTR_UNIT: TEMP_CELSIUS,
"device_class": DEVICE_CLASS_TEMPERATURE, ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"sht3x_humidity": { ATTR_SHT3X_HUMIDITY: {
"label": f"{DEFAULT_NAME} SHT3X Humidity", ATTR_LABEL: f"{DEFAULT_NAME} SHT3X Humidity",
"unit": PERCENTAGE, ATTR_UNIT: PERCENTAGE,
"device_class": DEVICE_CLASS_HUMIDITY, ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"sht3x_temperature": { ATTR_SHT3X_TEMPERATURE: {
"label": f"{DEFAULT_NAME} SHT3X Temperature", ATTR_LABEL: f"{DEFAULT_NAME} SHT3X Temperature",
"unit": TEMP_CELSIUS, ATTR_UNIT: TEMP_CELSIUS,
"device_class": DEVICE_CLASS_TEMPERATURE, ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"sps30_p0": { ATTR_SPS30_P0: {
"label": f"{DEFAULT_NAME} SPS30 Particulate Matter 1.0", ATTR_LABEL: f"{DEFAULT_NAME} SPS30 Particulate Matter 1.0",
"unit": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, ATTR_UNIT: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
"device_class": None, ATTR_DEVICE_CLASS: None,
"icon": "mdi:blur", ATTR_ICON: "mdi:blur",
"enabled": True, ATTR_ENABLED: True,
}, },
"sps30_p4": { ATTR_SPS30_P4: {
"label": f"{DEFAULT_NAME} SPS30 Particulate Matter 4.0", ATTR_LABEL: f"{DEFAULT_NAME} SPS30 Particulate Matter 4.0",
"unit": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, ATTR_UNIT: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
"device_class": None, ATTR_DEVICE_CLASS: None,
"icon": "mdi:blur", ATTR_ICON: "mdi:blur",
"enabled": True, ATTR_ENABLED: True,
}, },
"humidity": { ATTR_DHT22_HUMIDITY: {
"label": f"{DEFAULT_NAME} DHT22 Humidity", ATTR_LABEL: f"{DEFAULT_NAME} DHT22 Humidity",
"unit": PERCENTAGE, ATTR_UNIT: PERCENTAGE,
"device_class": DEVICE_CLASS_HUMIDITY, ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: True,
}, },
"signal": { ATTR_DHT22_TEMPERATURE: {
"label": f"{DEFAULT_NAME} Signal Strength", ATTR_LABEL: f"{DEFAULT_NAME} DHT22 Temperature",
"unit": SIGNAL_STRENGTH_DECIBELS_MILLIWATT, ATTR_UNIT: TEMP_CELSIUS,
"device_class": DEVICE_CLASS_SIGNAL_STRENGTH, ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
"icon": None, ATTR_ICON: None,
"enabled": False, ATTR_ENABLED: True,
}, },
"temperature": { ATTR_SIGNAL_STRENGTH: {
"label": f"{DEFAULT_NAME} DHT22 Temperature", ATTR_LABEL: f"{DEFAULT_NAME} Signal Strength",
"unit": TEMP_CELSIUS, ATTR_UNIT: SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
"device_class": DEVICE_CLASS_TEMPERATURE, ATTR_DEVICE_CLASS: DEVICE_CLASS_SIGNAL_STRENGTH,
"icon": None, ATTR_ICON: None,
"enabled": True, ATTR_ENABLED: False,
}, },
"uptime": { ATTR_UPTIME: {
"label": f"{DEFAULT_NAME} Uptime", ATTR_LABEL: f"{DEFAULT_NAME} Uptime",
"unit": None, ATTR_UNIT: None,
"device_class": DEVICE_CLASS_TIMESTAMP, ATTR_DEVICE_CLASS: DEVICE_CLASS_TIMESTAMP,
"icon": None, ATTR_ICON: None,
"enabled": False, ATTR_ENABLED: False,
}, },
} }

View file

@ -6,6 +6,7 @@ from typing import Any
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ICON
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -13,7 +14,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from . import NAMDataUpdateCoordinator from . import NAMDataUpdateCoordinator
from .const import DOMAIN, SENSORS from .const import ATTR_ENABLED, ATTR_LABEL, ATTR_UNIT, ATTR_UPTIME, DOMAIN, SENSORS
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
@ -27,7 +28,7 @@ async def async_setup_entry(
sensors: list[NAMSensor | NAMSensorUptime] = [] sensors: list[NAMSensor | NAMSensorUptime] = []
for sensor in SENSORS: for sensor in SENSORS:
if sensor in coordinator.data: if sensor in coordinator.data:
if sensor == "uptime": if sensor == ATTR_UPTIME:
sensors.append(NAMSensorUptime(coordinator, sensor)) sensors.append(NAMSensorUptime(coordinator, sensor))
else: else:
sensors.append(NAMSensor(coordinator, sensor)) sensors.append(NAMSensor(coordinator, sensor))
@ -49,7 +50,7 @@ class NAMSensor(CoordinatorEntity, SensorEntity):
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name.""" """Return the name."""
return self._description["label"] return self._description[ATTR_LABEL]
@property @property
def state(self) -> Any: def state(self) -> Any:
@ -59,22 +60,22 @@ class NAMSensor(CoordinatorEntity, SensorEntity):
@property @property
def unit_of_measurement(self) -> str | None: def unit_of_measurement(self) -> str | None:
"""Return the unit the value is expressed in.""" """Return the unit the value is expressed in."""
return self._description["unit"] return self._description[ATTR_UNIT]
@property @property
def device_class(self) -> str | None: def device_class(self) -> str | None:
"""Return the class of this sensor.""" """Return the class of this sensor."""
return self._description["device_class"] return self._description[ATTR_DEVICE_CLASS]
@property @property
def icon(self) -> str | None: def icon(self) -> str | None:
"""Return the icon.""" """Return the icon."""
return self._description["icon"] return self._description[ATTR_ICON]
@property @property
def entity_registry_enabled_default(self) -> bool: def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry.""" """Return if the entity should be enabled when first added to the entity registry."""
return self._description["enabled"] return self._description[ATTR_ENABLED]
@property @property
def unique_id(self) -> str: def unique_id(self) -> str: