Skip to content

Commit 1289a03

Browse files
authored
Add consumed energy sensor for Shelly pm1 and switch components (home-assistant#153053)
1 parent 289546e commit 1289a03

File tree

4 files changed

+426
-129
lines changed

4 files changed

+426
-129
lines changed

homeassistant/components/shelly/sensor.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ def native_value(self) -> StateType:
122122
return self.option_map[attribute_value]
123123

124124

125+
class RpcConsumedEnergySensor(RpcSensor):
126+
"""Represent a RPC sensor."""
127+
128+
@property
129+
def native_value(self) -> StateType:
130+
"""Return value of sensor."""
131+
total_energy = self.status["aenergy"]["total"]
132+
133+
if not isinstance(total_energy, float):
134+
return None
135+
136+
if not isinstance(self.attribute_value, float):
137+
return None
138+
139+
return total_energy - self.attribute_value
140+
141+
125142
class RpcPresenceSensor(RpcSensor):
126143
"""Represent a RPC presence sensor."""
127144

@@ -885,7 +902,7 @@ def __init__(
885902
"energy": RpcSensorDescription(
886903
key="switch",
887904
sub_key="aenergy",
888-
name="Energy",
905+
name="Total energy",
889906
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
890907
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
891908
value=lambda status, _: status["total"],
@@ -903,7 +920,22 @@ def __init__(
903920
suggested_display_precision=2,
904921
device_class=SensorDeviceClass.ENERGY,
905922
state_class=SensorStateClass.TOTAL_INCREASING,
923+
removal_condition=lambda _config, status, key: (
924+
status[key].get("ret_aenergy") is None
925+
),
926+
),
927+
"consumed_energy_switch": RpcSensorDescription(
928+
key="switch",
929+
sub_key="ret_aenergy",
930+
name="Consumed energy",
931+
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
932+
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
933+
value=lambda status, _: status["total"],
934+
suggested_display_precision=2,
935+
device_class=SensorDeviceClass.ENERGY,
936+
state_class=SensorStateClass.TOTAL_INCREASING,
906937
entity_registry_enabled_default=False,
938+
entity_class=RpcConsumedEnergySensor,
907939
removal_condition=lambda _config, status, key: (
908940
status[key].get("ret_aenergy") is None
909941
),
@@ -922,7 +954,7 @@ def __init__(
922954
"energy_pm1": RpcSensorDescription(
923955
key="pm1",
924956
sub_key="aenergy",
925-
name="Energy",
957+
name="Total energy",
926958
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
927959
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
928960
value=lambda status, _: status["total"],
@@ -933,14 +965,26 @@ def __init__(
933965
"ret_energy_pm1": RpcSensorDescription(
934966
key="pm1",
935967
sub_key="ret_aenergy",
936-
name="Total active returned energy",
968+
name="Returned energy",
969+
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
970+
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
971+
value=lambda status, _: status["total"],
972+
suggested_display_precision=2,
973+
device_class=SensorDeviceClass.ENERGY,
974+
state_class=SensorStateClass.TOTAL_INCREASING,
975+
),
976+
"consumed_energy_pm1": RpcSensorDescription(
977+
key="pm1",
978+
sub_key="ret_aenergy",
979+
name="Consumed energy",
937980
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
938981
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
939982
value=lambda status, _: status["total"],
940983
suggested_display_precision=2,
941984
device_class=SensorDeviceClass.ENERGY,
942985
state_class=SensorStateClass.TOTAL_INCREASING,
943986
entity_registry_enabled_default=False,
987+
entity_class=RpcConsumedEnergySensor,
944988
),
945989
"energy_cct": RpcSensorDescription(
946990
key="cct",

0 commit comments

Comments
 (0)