From 69050d59426a18758858015644507dac077b7233 Mon Sep 17 00:00:00 2001 From: b3nj1 Date: Thu, 9 Jun 2022 02:14:18 -0700 Subject: [PATCH] Add Vesync voltage sensor, and yearly, weekly, montly energy sensors (#72570) --- homeassistant/components/vesync/common.py | 2 +- homeassistant/components/vesync/sensor.py | 43 ++++++++++++++++++++++- homeassistant/components/vesync/switch.py | 12 ------- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/vesync/common.py b/homeassistant/components/vesync/common.py index acee8e20961..e11897ea9ae 100644 --- a/homeassistant/components/vesync/common.py +++ b/homeassistant/components/vesync/common.py @@ -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)) diff --git a/homeassistant/components/vesync/sensor.py b/homeassistant/components/vesync/sensor.py index 6e0c09b2f60..2da6d8ea6b7 100644 --- a/homeassistant/components/vesync/sensor.py +++ b/homeassistant/components/vesync/sensor.py @@ -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", + ), ) diff --git a/homeassistant/components/vesync/switch.py b/homeassistant/components/vesync/switch.py index e5fd4c829fe..68b10e40bcb 100644 --- a/homeassistant/components/vesync/switch.py +++ b/homeassistant/components/vesync/switch.py @@ -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()