Skip to content

Commit

Permalink
forcing state_class to allow sensors to be used in energy dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
myTselection committed Aug 16, 2023
1 parent b705a37 commit 47ab0af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion custom_components/pixometer/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/myTselection/pixometer/issues",
"requirements": [],
"version": "2.1.1"
"version": "2.1.2"
}
25 changes: 18 additions & 7 deletions custom_components/pixometer/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ def __init__(self, data, meter_details, meter_reading, hass):
self._meter_reading = meter_reading
self._meter_id = self._meter_details.get("meter_id")
self._hass = hass
self._attr_state_class = SensorStateClass.TOTAL_INCREASING

self._state_class_type = SensorStateClass.TOTAL_INCREASING
if self._meter_details.get("physical_medium") == "gas" or self._meter_details.get("physical_medium") == "water":
# Gas and Water meters can only increase. A lower value means the meter has been replaced.
self._state_class_type = SensorStateClass.TOTAL_INCREASING
if self._meter_details.get("physical_medium") == "electricity":
# Recent electricity meters can only increase. But pixometer is used to read old meters, and
# old meters can decrease e.g. when supplying current to the grid.
self._state_class_type = SensorStateClass.TOTAL

@property
def state(self):
Expand Down Expand Up @@ -191,7 +201,8 @@ def extra_state_attributes(self) -> dict:
"physical_unit": self._meter_details.get("physical_unit").replace("^3","³").replace("^2","²"),
"meter_id": self._meter_details.get("meter_id"),
"description": self._meter_details.get("description"),
"image": self._meter_reading.get("image_meta").get("image")
"image": self._meter_reading.get("image_meta").get("image"),
"state_class": self._state_class_type
}

@property
Expand Down Expand Up @@ -235,12 +246,12 @@ def device_class(self) -> str:

@property
def state_class(self) -> str:
state_class = None
if self.device_class == SensorDeviceClass.GAS or self.device_class == SensorDeviceClass.WATER:
state_class_type = SensorStateClass.TOTAL_INCREASING
if self._meter_details.get("physical_medium") == "gas" or self._meter_details.get("physical_medium") == "water":
# Gas and Water meters can only increase. A lower value means the meter has been replaced.
state_class = SensorStateClass.TOTAL_INCREASING
if self.device_class == SensorDeviceClass.ENERGY:
state_class_type = SensorStateClass.TOTAL_INCREASING
if self._meter_details.get("physical_medium") == "electricity":
# Recent electricity meters can only increase. But pixometer is used to read old meters, and
# old meters can decrease e.g. when supplying current to the grid.
state_class = SensorStateClass.TOTAL
return state_class
state_class_type = SensorStateClass.TOTAL
return state_class_type

0 comments on commit 47ab0af

Please sign in to comment.