From 152f799d0eb6c2f1e27059c127657728f7513192 Mon Sep 17 00:00:00 2001
From: bsmappee <58250533+bsmappee@users.noreply.github.com>
Date: Fri, 20 Aug 2021 23:20:45 +0200
Subject: [PATCH] Extract smappee switch energy attributes into sensors
 (#54329)

---
 homeassistant/components/smappee/sensor.py | 104 +++++++++++++++------
 homeassistant/components/smappee/switch.py |  11 ---
 2 files changed, 77 insertions(+), 38 deletions(-)

diff --git a/homeassistant/components/smappee/sensor.py b/homeassistant/components/smappee/sensor.py
index fb879e3cef5..6474c74c185 100644
--- a/homeassistant/components/smappee/sensor.py
+++ b/homeassistant/components/smappee/sensor.py
@@ -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)
diff --git a/homeassistant/components/smappee/switch.py b/homeassistant/components/smappee/switch.py
index 3ba5e6b2a97..ded898f9f10 100644
--- a/homeassistant/components/smappee/switch.py
+++ b/homeassistant/components/smappee/switch.py
@@ -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,