Extract smappee switch energy attributes into sensors (#54329)

This commit is contained in:
bsmappee 2021-08-20 23:20:45 +02:00 committed by GitHub
parent 8f6281473e
commit 152f799d0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 38 deletions

View file

@ -1,8 +1,15 @@
"""Support for monitoring a Smappee energy sensor."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.const import (
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_VOLTAGE,
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
ENERGY_WATT_HOUR,
POWER_WATT,
)
@ -28,34 +35,34 @@ TREND_SENSORS = {
],
"power_today": [
"Total consumption - Today",
"mdi:power-plug",
None,
ENERGY_WATT_HOUR,
"power_today",
None,
DEVICE_CLASS_ENERGY,
False, # cloud only
],
"power_current_hour": [
"Total consumption - Current hour",
"mdi:power-plug",
None,
ENERGY_WATT_HOUR,
"power_current_hour",
None,
DEVICE_CLASS_ENERGY,
False, # cloud only
],
"power_last_5_minutes": [
"Total consumption - Last 5 minutes",
"mdi:power-plug",
None,
ENERGY_WATT_HOUR,
"power_last_5_minutes",
None,
DEVICE_CLASS_ENERGY,
False, # cloud only
],
"alwayson_today": [
"Always on - Today",
"mdi:sleep",
None,
ENERGY_WATT_HOUR,
"alwayson_today",
None,
DEVICE_CLASS_ENERGY,
False, # cloud only
],
}
@ -79,68 +86,68 @@ SOLAR_SENSORS = {
],
"solar_today": [
"Total production - Today",
"mdi:white-balance-sunny",
None,
ENERGY_WATT_HOUR,
"solar_today",
None,
DEVICE_CLASS_POWER,
False, # cloud only
],
"solar_current_hour": [
"Total production - Current hour",
"mdi:white-balance-sunny",
None,
ENERGY_WATT_HOUR,
"solar_current_hour",
None,
DEVICE_CLASS_POWER,
False, # cloud only
],
}
VOLTAGE_SENSORS = {
"phase_voltages_a": [
"Phase voltages - A",
"mdi:flash",
None,
ELECTRIC_POTENTIAL_VOLT,
"phase_voltage_a",
None,
DEVICE_CLASS_VOLTAGE,
["ONE", "TWO", "THREE_STAR", "THREE_DELTA"],
],
"phase_voltages_b": [
"Phase voltages - B",
"mdi:flash",
None,
ELECTRIC_POTENTIAL_VOLT,
"phase_voltage_b",
None,
DEVICE_CLASS_VOLTAGE,
["TWO", "THREE_STAR", "THREE_DELTA"],
],
"phase_voltages_c": [
"Phase voltages - C",
"mdi:flash",
None,
ELECTRIC_POTENTIAL_VOLT,
"phase_voltage_c",
None,
DEVICE_CLASS_VOLTAGE,
["THREE_STAR"],
],
"line_voltages_a": [
"Line voltages - A",
"mdi:flash",
None,
ELECTRIC_POTENTIAL_VOLT,
"line_voltage_a",
None,
DEVICE_CLASS_VOLTAGE,
["ONE", "TWO", "THREE_STAR", "THREE_DELTA"],
],
"line_voltages_b": [
"Line voltages - B",
"mdi:flash",
None,
ELECTRIC_POTENTIAL_VOLT,
"line_voltage_b",
None,
DEVICE_CLASS_VOLTAGE,
["TWO", "THREE_STAR", "THREE_DELTA"],
],
"line_voltages_c": [
"Line voltages - C",
"mdi:flash",
None,
ELECTRIC_POTENTIAL_VOLT,
"line_voltage_c",
None,
DEVICE_CLASS_VOLTAGE,
["THREE_STAR", "THREE_DELTA"],
],
}
@ -246,6 +253,25 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
)
# Add today_energy_kwh sensors for switches
for actuator_id, actuator in service_location.actuators.items():
if actuator.type == "SWITCH":
entities.append(
SmappeeSensor(
smappee_base=smappee_base,
service_location=service_location,
sensor="switch",
attributes=[
f"{actuator.name} - energy today",
None,
ENERGY_KILO_WATT_HOUR,
actuator_id,
DEVICE_CLASS_ENERGY,
False, # cloud only
],
)
)
async_add_entities(entities, True)
@ -268,7 +294,7 @@ class SmappeeSensor(SensorEntity):
@property
def name(self):
"""Return the name for this sensor."""
if self._sensor in ("sensor", "load"):
if self._sensor in ("sensor", "load", "switch"):
return (
f"{self._service_location.service_location_name} - "
f"{self._sensor.title()} - {self._name}"
@ -291,6 +317,24 @@ class SmappeeSensor(SensorEntity):
"""Return the class of this device, from component DEVICE_CLASSES."""
return self._device_class
@property
def state_class(self):
"""Return the state class of this device."""
scm = STATE_CLASS_MEASUREMENT
if self._sensor in (
"power_today",
"power_current_hour",
"power_last_5_minutes",
"solar_today",
"solar_current_hour",
"alwayson_today",
"switch",
):
scm = STATE_CLASS_TOTAL_INCREASING
return scm
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
@ -301,7 +345,7 @@ class SmappeeSensor(SensorEntity):
self,
):
"""Return the unique ID for this sensor."""
if self._sensor in ("load", "sensor"):
if self._sensor in ("load", "sensor", "switch"):
return (
f"{self._service_location.device_serial_number}-"
f"{self._service_location.service_location_id}-"
@ -379,3 +423,9 @@ class SmappeeSensor(SensorEntity):
for channel in sensor.channels:
if channel.get("channel") == int(channel_id):
self._state = channel.get("value_today")
elif self._sensor == "switch":
cons = self._service_location.actuators.get(
self._sensor_id
).consumption_today
if cons is not None:
self._state = round(cons / 1000.0, 2)

View file

@ -128,17 +128,6 @@ class SmappeeActuator(SwitchEntity):
or self._actuator_type == "COMFORT_PLUG"
)
@property
def today_energy_kwh(self):
"""Return the today total energy usage in kWh."""
if self._actuator_type == "SWITCH":
cons = self._service_location.actuators.get(
self._actuator_id
).consumption_today
if cons is not None:
return round(cons / 1000.0, 2)
return None
@property
def unique_id(
self,