Skip to content

Commit

Permalink
Fix long-term statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 2, 2024
1 parent a306c7f commit 725c3c0
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions custom_components/xiaomi_gateway3/hass/entity_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"mode": "_attr_mode", # NumberMode
"name": "_attr_name",
"poll": "_attr_should_poll",
"stats": "_attr_state_class",
"statistics": "_attr_state_class",
"units": "_attr_native_unit_of_measurement",
"visible": "_attr_entity_registry_visible_default",
}
Expand Down Expand Up @@ -153,7 +153,7 @@
},
"sensor.energy": {
"class": SENSOR.ENERGY,
"stats": SensorStateClass.TOTAL,
"statistics": SensorStateClass.TOTAL,
"units": UnitOfEnergy.KILO_WATT_HOUR,
},
##
Expand Down Expand Up @@ -229,4 +229,13 @@ def setup_entity_description(entity: Entity, conv: BaseConv) -> bool:
v = EntityCategory(v)
setattr(entity, ENTITY_KEYS.get(k) or k, v)

# sensor with unit_of_measurement and without state_class will be MEASUREMENT
# https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics
if (
conv.domain == "sensor"
and hasattr(entity, "_attr_native_unit_of_measurement")
and not hasattr(entity, "_attr_state_class")
):
setattr(entity, "_attr_state_class", SensorStateClass.MEASUREMENT)

return True

0 comments on commit 725c3c0

Please sign in to comment.