1
mirror of https://github.com/home-assistant/core synced 2024-09-15 17:29:45 +02:00

Add Vesync voltage sensor, and yearly, weekly, montly energy sensors (#72570)

This commit is contained in:
b3nj1 2022-06-09 02:14:18 -07:00 committed by GitHub
parent a7398b8a73
commit 69050d5942
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 14 deletions

View File

@ -30,7 +30,7 @@ async def async_process_devices(hass, manager):
if manager.outlets:
devices[VS_SWITCHES].extend(manager.outlets)
# Expose outlets' power & energy usage as separate sensors
# Expose outlets' voltage, power & energy usage as separate sensors
devices[VS_SENSORS].extend(manager.outlets)
_LOGGER.info("%d VeSync outlets found", len(manager.outlets))

View File

@ -1,4 +1,4 @@
"""Support for power & energy sensors for VeSync outlets."""
"""Support for voltage, power & energy sensors for VeSync outlets."""
from __future__ import annotations
from collections.abc import Callable
@ -18,6 +18,7 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
PERCENTAGE,
POWER_WATT,
@ -121,6 +122,46 @@ SENSORS: tuple[VeSyncSensorEntityDescription, ...] = (
update_fn=update_energy,
exists_fn=lambda device: ha_dev_type(device) == "outlet",
),
VeSyncSensorEntityDescription(
key="energy-weekly",
name="energy use weekly",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda device: device.weekly_energy_total,
update_fn=update_energy,
exists_fn=lambda device: ha_dev_type(device) == "outlet",
),
VeSyncSensorEntityDescription(
key="energy-monthly",
name="energy use monthly",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda device: device.monthly_energy_total,
update_fn=update_energy,
exists_fn=lambda device: ha_dev_type(device) == "outlet",
),
VeSyncSensorEntityDescription(
key="energy-yearly",
name="energy use yearly",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda device: device.yearly_energy_total,
update_fn=update_energy,
exists_fn=lambda device: ha_dev_type(device) == "outlet",
),
VeSyncSensorEntityDescription(
key="voltage",
name="current voltage",
device_class=SensorDeviceClass.VOLTAGE,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda device: device.details["voltage"],
update_fn=update_energy,
exists_fn=lambda device: ha_dev_type(device) == "outlet",
),
)

View File

@ -66,18 +66,6 @@ class VeSyncSwitchHA(VeSyncBaseSwitch, SwitchEntity):
super().__init__(plug)
self.smartplug = plug
@property
def extra_state_attributes(self):
"""Return the state attributes of the device."""
if not hasattr(self.smartplug, "weekly_energy_total"):
return {}
return {
"voltage": self.smartplug.voltage,
"weekly_energy_total": self.smartplug.weekly_energy_total,
"monthly_energy_total": self.smartplug.monthly_energy_total,
"yearly_energy_total": self.smartplug.yearly_energy_total,
}
def update(self):
"""Update outlet details and energy usage."""
self.smartplug.update()