Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
add an air quality value sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
vlebourl committed Sep 14, 2023
1 parent 62ea7fd commit ecec852
Showing 1 changed file with 58 additions and 14 deletions.
72 changes: 58 additions & 14 deletions custom_components/vesync/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ def _setup_entities(devices, async_add_entities, coordinator):
if has_feature(dev, "details", "humidity"):
entities.append(VeSyncHumiditySensor(dev, coordinator))
if has_feature(dev, "details", "air_quality"):
entities.append(VeSyncAirQualitySensor(dev, coordinator))
entities.extend(
(
VeSyncAirQualitySensor(dev, coordinator),
VeSyncAirQualityValueSensor(dev, coordinator),
)
)
if has_feature(dev, "details", "filter_life"):
entities.append(VeSyncFilterLifeSensor(dev, coordinator))

Expand Down Expand Up @@ -185,11 +190,18 @@ class VeSyncAirQualitySensor(VeSyncHumidifierSensorEntity):
"""Representation of an air quality sensor."""

_attr_state_class = SensorStateClass.MEASUREMENT
_attr_device_class = SensorDeviceClass.AQI

def __init__(self, device, coordinator):
"""Initialize the VeSync device."""
super().__init__(device, coordinator)
self._numeric_quality = None
if self.native_value is not None:
self._numeric_quality = isinstance(self.native_value, (int, float))

@property
def device_class(self):
"""Return the air quality device class."""
return SensorDeviceClass.AQI if self._numeric_quality else None

@property
def unique_id(self):
Expand All @@ -204,18 +216,50 @@ def name(self):
@property
def native_value(self):
"""Return the air quality index."""
feature_keys = ["air_quality_value", "air_quality"]
for key in feature_keys:
if has_feature(self.smarthumidifier, "details", key):
quality = self.smarthumidifier.details[key]
if isinstance(quality, (int, float)):
return quality
_LOGGER.warn(
"Got non numeric value for AQI sensor from '%s' for %s: %s",
key,
self.name,
quality,
)
if has_feature(self.smarthumidifier, "details", "air_quality"):
quality = self.smarthumidifier.details["air_quality"]
if isinstance(quality, (int, float)):
return quality
_LOGGER.warn(
"Got non numeric value for AQI sensor from 'air_quality' for %s: %s",
self.name,
quality,
)
_LOGGER.warn("No air quality index found in '%s'", self.name)
return None


class VeSyncAirQualityValueSensor(VeSyncHumidifierSensorEntity):
"""Representation of an air quality sensor."""

_attr_state_class = SensorStateClass.MEASUREMENT

def __init__(self, device, coordinator):
"""Initialize the VeSync device."""
super().__init__(device, coordinator)

@property
def unique_id(self):
"""Return unique ID for air quality sensor on device."""
return f"{super().unique_id}-air-quality-value"

@property
def name(self):
"""Return sensor name."""
return f"{super().name} air quality value"

@property
def native_value(self):
"""Return the air quality index."""
if has_feature(self.smarthumidifier, "details", "air_quality_value"):
quality_value = self.smarthumidifier.details["air_quality_value"]
if isinstance(quality_value, (int, float)):
return quality_value
_LOGGER.warn(
"Got non numeric value for AQI sensor from 'air_quality_value' for %s: %s",
self.name,
quality_value,
)
_LOGGER.warn("No air quality value found in '%s'", self.name)
return None

Expand Down

0 comments on commit ecec852

Please sign in to comment.