2022-04-01 15:02:13 +02:00
|
|
|
"""Support for power & energy sensors for VeSync outlets."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import ENERGY_KILO_WATT_HOUR, PERCENTAGE, POWER_WATT
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2022-07-30 09:59:00 +02:00
|
|
|
from .common import VeSyncBaseEntity, has_feature
|
2022-04-01 15:02:13 +02:00
|
|
|
from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_SENSORS
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up switches."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def discover(devices):
|
|
|
|
"""Add new devices to platform."""
|
|
|
|
_setup_entities(devices, async_add_entities)
|
|
|
|
|
|
|
|
config_entry.async_on_unload(
|
|
|
|
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_SENSORS), discover)
|
|
|
|
)
|
|
|
|
|
|
|
|
_setup_entities(
|
|
|
|
hass.data[DOMAIN][config_entry.entry_id][VS_SENSORS], async_add_entities
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _setup_entities(devices, async_add_entities):
|
|
|
|
"""Check if device is online and add entity."""
|
|
|
|
entities = []
|
|
|
|
for dev in devices:
|
|
|
|
if DEV_TYPE_TO_HA.get(dev.device_type) == "outlet":
|
|
|
|
entities.extend((VeSyncPowerSensor(dev), VeSyncEnergySensor(dev)))
|
2022-07-30 09:59:00 +02:00
|
|
|
if has_feature(dev, "details", "humidity"):
|
2022-04-01 15:02:13 +02:00
|
|
|
entities.append(VeSyncHumiditySensor(dev))
|
2022-07-30 09:59:00 +02:00
|
|
|
if has_feature(dev, "details", "air_quality"):
|
2022-07-11 16:56:09 +02:00
|
|
|
entities.append(VeSyncAirQualitySensor(dev))
|
2022-07-30 09:59:00 +02:00
|
|
|
if has_feature(dev, "details", "filter_life"):
|
|
|
|
entities.append(VeSyncFilterLifeSensor(dev))
|
2022-04-01 15:02:13 +02:00
|
|
|
|
|
|
|
async_add_entities(entities, update_before_add=True)
|
|
|
|
|
|
|
|
|
|
|
|
class VeSyncOutletSensorEntity(VeSyncBaseEntity, SensorEntity):
|
|
|
|
"""Representation of a sensor describing diagnostics of a VeSync outlet."""
|
|
|
|
|
|
|
|
def __init__(self, plug):
|
|
|
|
"""Initialize the VeSync outlet device."""
|
|
|
|
super().__init__(plug)
|
|
|
|
self.smartplug = plug
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_category(self):
|
|
|
|
"""Return the diagnostic entity category."""
|
|
|
|
return EntityCategory.DIAGNOSTIC
|
|
|
|
|
|
|
|
|
|
|
|
class VeSyncPowerSensor(VeSyncOutletSensorEntity):
|
|
|
|
"""Representation of current power use for a VeSync outlet."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID for power sensor on device."""
|
|
|
|
return f"{super().unique_id}-power"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return sensor name."""
|
|
|
|
return f"{super().name} current power"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the power device class."""
|
|
|
|
return SensorDeviceClass.POWER
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self):
|
|
|
|
"""Return the current power usage in W."""
|
|
|
|
return self.smartplug.power
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_unit_of_measurement(self):
|
|
|
|
"""Return the Watt unit of measurement."""
|
|
|
|
return POWER_WATT
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_class(self):
|
|
|
|
"""Return the measurement state class."""
|
|
|
|
return SensorStateClass.MEASUREMENT
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update outlet details and energy usage."""
|
|
|
|
self.smartplug.update()
|
|
|
|
self.smartplug.update_energy()
|
|
|
|
|
|
|
|
|
|
|
|
class VeSyncEnergySensor(VeSyncOutletSensorEntity):
|
|
|
|
"""Representation of current day's energy use for a VeSync outlet."""
|
|
|
|
|
|
|
|
def __init__(self, plug):
|
|
|
|
"""Initialize the VeSync outlet device."""
|
|
|
|
super().__init__(plug)
|
|
|
|
self.smartplug = plug
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID for power sensor on device."""
|
|
|
|
return f"{super().unique_id}-energy"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return sensor name."""
|
|
|
|
return f"{super().name} energy use today"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the energy device class."""
|
|
|
|
return SensorDeviceClass.ENERGY
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self):
|
|
|
|
"""Return the today total energy usage in kWh."""
|
|
|
|
return self.smartplug.energy_today
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_unit_of_measurement(self):
|
|
|
|
"""Return the kWh unit of measurement."""
|
|
|
|
return ENERGY_KILO_WATT_HOUR
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_class(self):
|
|
|
|
"""Return the total_increasing state class."""
|
|
|
|
return SensorStateClass.TOTAL_INCREASING
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update outlet details and energy usage."""
|
|
|
|
self.smartplug.update()
|
|
|
|
self.smartplug.update_energy()
|
|
|
|
|
|
|
|
|
|
|
|
class VeSyncHumidifierSensorEntity(VeSyncBaseEntity, SensorEntity):
|
|
|
|
"""Representation of a sensor describing diagnostics of a VeSync humidifier."""
|
|
|
|
|
|
|
|
def __init__(self, humidifier):
|
|
|
|
"""Initialize the VeSync humidifier device."""
|
|
|
|
super().__init__(humidifier)
|
|
|
|
self.smarthumidifier = humidifier
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_category(self):
|
|
|
|
"""Return the diagnostic entity category."""
|
|
|
|
return EntityCategory.DIAGNOSTIC
|
|
|
|
|
|
|
|
|
2022-07-11 16:56:09 +02:00
|
|
|
class VeSyncAirQualitySensor(VeSyncHumidifierSensorEntity):
|
|
|
|
"""Representation of an air quality sensor."""
|
|
|
|
|
2022-08-17 11:36:40 +02:00
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
|
|
|
_attr_device_class = SensorDeviceClass.AQI
|
|
|
|
|
2022-07-11 16:56:09 +02:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID for air quality sensor on device."""
|
|
|
|
return f"{super().unique_id}-air-quality"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return sensor name."""
|
|
|
|
return f"{super().name} air quality"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self):
|
|
|
|
"""Return the air quality index."""
|
2022-08-17 11:36:40 +02:00
|
|
|
quality = None
|
2022-08-18 16:52:41 +02:00
|
|
|
if has_feature(self.smarthumidifier, "details", "air_quality_value"):
|
2022-08-17 11:36:40 +02:00
|
|
|
quality = self.smarthumidifier.details["air_quality_value"]
|
2022-08-18 16:52:41 +02:00
|
|
|
elif has_feature(self.smarthumidifier, "details", "air_quality"):
|
|
|
|
quality = self.smarthumidifier.details["air_quality"]
|
2022-08-17 11:36:40 +02:00
|
|
|
return quality.capitalize() if isinstance(quality, str) else quality
|
2022-07-30 09:59:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class VeSyncFilterLifeSensor(VeSyncHumidifierSensorEntity):
|
|
|
|
"""Representation of a filter life sensor."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID for filter life sensor on device."""
|
|
|
|
return f"{super().unique_id}-filter-life"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return sensor name."""
|
|
|
|
return f"{super().name} filter life"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the filter life device class."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self):
|
|
|
|
"""Return the filter life index."""
|
2022-08-17 11:36:40 +02:00
|
|
|
return (
|
|
|
|
self.smarthumidifier.filter_life
|
|
|
|
if hasattr(self.smarthumidifier, "filter_life")
|
|
|
|
else self.smarthumidifier.details["filter_life"]
|
|
|
|
)
|
2022-07-30 09:59:00 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def native_unit_of_measurement(self):
|
|
|
|
"""Return the % unit of measurement."""
|
|
|
|
return PERCENTAGE
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_class(self):
|
|
|
|
"""Return the measurement state class."""
|
2022-07-11 16:56:09 +02:00
|
|
|
return SensorStateClass.MEASUREMENT
|
|
|
|
|
2022-08-17 11:36:40 +02:00
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return (
|
|
|
|
self.smarthumidifier.details["filter_life"]
|
|
|
|
if isinstance(self.smarthumidifier.details["filter_life"], dict)
|
|
|
|
else {}
|
|
|
|
)
|
|
|
|
|
2022-07-11 16:56:09 +02:00
|
|
|
|
2022-04-01 15:02:13 +02:00
|
|
|
class VeSyncHumiditySensor(VeSyncHumidifierSensorEntity):
|
|
|
|
"""Representation of current humidity for a VeSync humidifier."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID for humidity sensor on device."""
|
|
|
|
return f"{super().unique_id}-humidity"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return sensor name."""
|
|
|
|
return f"{super().name} current humidity"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the humidity device class."""
|
|
|
|
return SensorDeviceClass.HUMIDITY
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self):
|
|
|
|
"""Return the current humidity in percent."""
|
|
|
|
return self.smarthumidifier.details["humidity"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_unit_of_measurement(self):
|
|
|
|
"""Return the % unit of measurement."""
|
|
|
|
return PERCENTAGE
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_class(self):
|
|
|
|
"""Return the measurement state class."""
|
|
|
|
return SensorStateClass.MEASUREMENT
|