Add state class and last reset in kostal_plenticore (#54084)

* Add state class and implement in kostal_plenticore

* Add support for more entity variants

* Add the state_class to the total values too

* Reformat kostal const.py

* Add `last_reset` to kostal_plenticore entities when `state_class` is set

Also reformat sensor.py

* Fix import

* Remove the constants from the homeassistant constants file

* Use sensor constants for the state_class

* Reformat

* Reformat

* Move last_reset from sensor.py into const.py

* Remove last_reset on PERCENTAGE entities

* Address lint issues

* Update homeassistant/components/kostal_plenticore/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Import datetime

* Apply suggestions from code review

* Fix isort

* Fix more isort

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/54193/head
Gian Klug 2021-08-07 06:25:35 +02:00 committed by GitHub
parent 98bcdc2cf5
commit 8b8f3b55b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 14 deletions

View File

@ -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",
),

View File

@ -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."""