diff --git a/homeassistant/components/kostal_plenticore/const.py b/homeassistant/components/kostal_plenticore/const.py index 5c223f4f5d6..ede8e10cb25 100644 --- a/homeassistant/components/kostal_plenticore/const.py +++ b/homeassistant/components/kostal_plenticore/const.py @@ -1,5 +1,10 @@ """Constants for the Kostal Plenticore Solar Inverter integration.""" +from homeassistant.components.sensor import ( + ATTR_LAST_RESET, + ATTR_STATE_CLASS, + STATE_CLASS_MEASUREMENT, +) from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_ICON, @@ -11,11 +16,14 @@ from homeassistant.const import ( PERCENTAGE, POWER_WATT, ) +from homeassistant.util.dt import utc_from_timestamp DOMAIN = "kostal_plenticore" ATTR_ENABLED_DEFAULT = "entity_registry_enabled_default" +LAST_RESET_NEVER = utc_from_timestamp(0) + # Defines all entities for process data. # # Each entry is defined with a tuple of these values: @@ -40,6 +48,7 @@ SENSOR_PROCESS_DATA = [ ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, ATTR_ENABLED_DEFAULT: True, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, }, "format_round", ), @@ -51,6 +60,7 @@ SENSOR_PROCESS_DATA = [ ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, ATTR_ENABLED_DEFAULT: True, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, }, "format_round", ), @@ -65,28 +75,44 @@ SENSOR_PROCESS_DATA = [ "devices:local", "HomeGrid_P", "Home Power from Grid", - {ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER}, + { + ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( "devices:local", "HomeOwn_P", "Home Power from Own", - {ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER}, + { + ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( "devices:local", "HomePv_P", "Home Power from PV", - {ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER}, + { + ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( "devices:local", "Home_P", "Home Power", - {ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER}, + { + ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( @@ -97,6 +123,7 @@ SENSOR_PROCESS_DATA = [ ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, ATTR_ENABLED_DEFAULT: True, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, }, "format_round", ), @@ -104,28 +131,44 @@ SENSOR_PROCESS_DATA = [ "devices:local:pv1", "P", "DC1 Power", - {ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER}, + { + ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( "devices:local:pv2", "P", "DC2 Power", - {ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER}, + { + ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( "devices:local:pv3", "P", "DC3 Power", - {ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER}, + { + ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( "devices:local", "PV2Bat_P", "PV to Battery Power", - {ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER}, + { + ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( @@ -139,14 +182,18 @@ SENSOR_PROCESS_DATA = [ "devices:local:battery", "Cycles", "Battery Cycles", - {ATTR_ICON: "mdi:recycle"}, + {ATTR_ICON: "mdi:recycle", ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT}, "format_round", ), ( "devices:local:battery", "P", "Battery Power", - {ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER}, + { + ATTR_UNIT_OF_MEASUREMENT: POWER_WATT, + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( @@ -174,7 +221,11 @@ SENSOR_PROCESS_DATA = [ "scb:statistic:EnergyFlow", "Statistic:Autarky:Total", "Autarky Total", - {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE, ATTR_ICON: "mdi:chart-donut"}, + { + ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE, + ATTR_ICON: "mdi:chart-donut", + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( @@ -202,7 +253,11 @@ SENSOR_PROCESS_DATA = [ "scb:statistic:EnergyFlow", "Statistic:OwnConsumptionRate:Total", "Own Consumption Rate Total", - {ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE, ATTR_ICON: "mdi:chart-donut"}, + { + ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE, + ATTR_ICON: "mdi:chart-donut", + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + }, "format_round", ), ( @@ -249,6 +304,8 @@ SENSOR_PROCESS_DATA = [ { ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, ATTR_DEVICE_CLASS: DEVICE_CLASS_ENERGY, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + ATTR_LAST_RESET: LAST_RESET_NEVER, }, "format_energy", ), @@ -289,6 +346,8 @@ SENSOR_PROCESS_DATA = [ { ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, ATTR_DEVICE_CLASS: DEVICE_CLASS_ENERGY, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + ATTR_LAST_RESET: LAST_RESET_NEVER, }, "format_energy", ), @@ -329,6 +388,8 @@ SENSOR_PROCESS_DATA = [ { ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, ATTR_DEVICE_CLASS: DEVICE_CLASS_ENERGY, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + ATTR_LAST_RESET: LAST_RESET_NEVER, }, "format_energy", ), @@ -369,6 +430,8 @@ SENSOR_PROCESS_DATA = [ { ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, ATTR_DEVICE_CLASS: DEVICE_CLASS_ENERGY, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + ATTR_LAST_RESET: LAST_RESET_NEVER, }, "format_energy", ), @@ -409,6 +472,8 @@ SENSOR_PROCESS_DATA = [ { ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, ATTR_DEVICE_CLASS: DEVICE_CLASS_ENERGY, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + ATTR_LAST_RESET: LAST_RESET_NEVER, }, "format_energy", ), @@ -449,6 +514,8 @@ SENSOR_PROCESS_DATA = [ { ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, ATTR_DEVICE_CLASS: DEVICE_CLASS_ENERGY, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + ATTR_LAST_RESET: LAST_RESET_NEVER, }, "format_energy", ), @@ -489,6 +556,8 @@ SENSOR_PROCESS_DATA = [ { ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, ATTR_DEVICE_CLASS: DEVICE_CLASS_ENERGY, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + ATTR_LAST_RESET: LAST_RESET_NEVER, }, "format_energy", ), @@ -530,6 +599,8 @@ SENSOR_PROCESS_DATA = [ { ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR, ATTR_DEVICE_CLASS: DEVICE_CLASS_ENERGY, + ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT, + ATTR_LAST_RESET: LAST_RESET_NEVER, }, "format_energy", ), diff --git a/homeassistant/components/kostal_plenticore/sensor.py b/homeassistant/components/kostal_plenticore/sensor.py index 717dfacbfdf..099d359e619 100644 --- a/homeassistant/components/kostal_plenticore/sensor.py +++ b/homeassistant/components/kostal_plenticore/sensor.py @@ -1,11 +1,15 @@ """Platform for Kostal Plenticore sensors.""" from __future__ import annotations -from datetime import timedelta +from datetime import datetime, timedelta import logging from typing import Any, Callable -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import ( + ATTR_LAST_RESET, + ATTR_STATE_CLASS, + SensorEntity, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT from homeassistant.core import HomeAssistant @@ -179,11 +183,21 @@ class PlenticoreDataSensor(CoordinatorEntity, SensorEntity): """Return the class of this device, from component DEVICE_CLASSES.""" return self._sensor_data.get(ATTR_DEVICE_CLASS) + @property + def state_class(self) -> str | None: + """Return the class of the state of this device, from component STATE_CLASSES.""" + return self._sensor_data.get(ATTR_STATE_CLASS) + @property def entity_registry_enabled_default(self) -> bool: """Return if the entity should be enabled when first added to the entity registry.""" return self._sensor_data.get(ATTR_ENABLED_DEFAULT, False) + @property + def last_reset(self) -> datetime | None: + """Return the last_reset time.""" + return self._sensor_data.get(ATTR_LAST_RESET) + @property def state(self) -> Any | None: """Return the state of the sensor."""