Remove last_reset attribute and set state class to total_increasing for tibber energy sensors (#54799)

* Remove last_reset attribute from tibber energy sensors

* Remove reset_type, fix merge

* Update homeassistant/components/tibber/sensor.py

Co-authored-by: Franck Nijhof <git@frenck.dev>

Co-authored-by: Daniel Hjelseth Høyer <mail@dahoiv.net>
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Erik Montnemery 2021-08-18 14:18:51 +02:00 committed by GitHub
parent 60f8e24bde
commit 0329d0f246
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from enum import Enum
import logging import logging
from random import randrange from random import randrange
@ -19,6 +17,7 @@ from homeassistant.components.sensor import (
DEVICE_CLASS_SIGNAL_STRENGTH, DEVICE_CLASS_SIGNAL_STRENGTH,
DEVICE_CLASS_VOLTAGE, DEVICE_CLASS_VOLTAGE,
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
@ -49,166 +48,143 @@ PARALLEL_UPDATES = 0
SIGNAL_UPDATE_ENTITY = "tibber_rt_update_{}" SIGNAL_UPDATE_ENTITY = "tibber_rt_update_{}"
class ResetType(Enum): RT_SENSORS: tuple[SensorEntityDescription, ...] = (
"""Data reset type.""" SensorEntityDescription(
HOURLY = "hourly"
DAILY = "daily"
NEVER = "never"
@dataclass
class TibberSensorEntityDescription(SensorEntityDescription):
"""Describes Tibber sensor entity."""
reset_type: ResetType | None = None
RT_SENSORS: tuple[TibberSensorEntityDescription, ...] = (
TibberSensorEntityDescription(
key="averagePower", key="averagePower",
name="average power", name="average power",
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="power", key="power",
name="power", name="power",
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="powerProduction", key="powerProduction",
name="power production", name="power production",
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="minPower", key="minPower",
name="min power", name="min power",
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="maxPower", key="maxPower",
name="max power", name="max power",
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="accumulatedConsumption", key="accumulatedConsumption",
name="accumulated consumption", name="accumulated consumption",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_TOTAL_INCREASING,
reset_type=ResetType.DAILY,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="accumulatedConsumptionLastHour", key="accumulatedConsumptionLastHour",
name="accumulated consumption current hour", name="accumulated consumption current hour",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_TOTAL_INCREASING,
reset_type=ResetType.HOURLY,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="accumulatedProduction", key="accumulatedProduction",
name="accumulated production", name="accumulated production",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_TOTAL_INCREASING,
reset_type=ResetType.DAILY,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="accumulatedProductionLastHour", key="accumulatedProductionLastHour",
name="accumulated production current hour", name="accumulated production current hour",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_TOTAL_INCREASING,
reset_type=ResetType.HOURLY,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="lastMeterConsumption", key="lastMeterConsumption",
name="last meter consumption", name="last meter consumption",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_TOTAL_INCREASING,
reset_type=ResetType.NEVER,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="lastMeterProduction", key="lastMeterProduction",
name="last meter production", name="last meter production",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_TOTAL_INCREASING,
reset_type=ResetType.NEVER,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="voltagePhase1", key="voltagePhase1",
name="voltage phase1", name="voltage phase1",
device_class=DEVICE_CLASS_VOLTAGE, device_class=DEVICE_CLASS_VOLTAGE,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="voltagePhase2", key="voltagePhase2",
name="voltage phase2", name="voltage phase2",
device_class=DEVICE_CLASS_VOLTAGE, device_class=DEVICE_CLASS_VOLTAGE,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="voltagePhase3", key="voltagePhase3",
name="voltage phase3", name="voltage phase3",
device_class=DEVICE_CLASS_VOLTAGE, device_class=DEVICE_CLASS_VOLTAGE,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="currentL1", key="currentL1",
name="current L1", name="current L1",
device_class=DEVICE_CLASS_CURRENT, device_class=DEVICE_CLASS_CURRENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="currentL2", key="currentL2",
name="current L2", name="current L2",
device_class=DEVICE_CLASS_CURRENT, device_class=DEVICE_CLASS_CURRENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="currentL3", key="currentL3",
name="current L3", name="current L3",
device_class=DEVICE_CLASS_CURRENT, device_class=DEVICE_CLASS_CURRENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="signalStrength", key="signalStrength",
name="signal strength", name="signal strength",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH, device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS, native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="accumulatedReward", key="accumulatedReward",
name="accumulated reward", name="accumulated reward",
device_class=DEVICE_CLASS_MONETARY, device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.DAILY,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="accumulatedCost", key="accumulatedCost",
name="accumulated cost", name="accumulated cost",
device_class=DEVICE_CLASS_MONETARY, device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.DAILY,
), ),
TibberSensorEntityDescription( SensorEntityDescription(
key="powerFactor", key="powerFactor",
name="power factor", name="power factor",
device_class=DEVICE_CLASS_POWER_FACTOR, device_class=DEVICE_CLASS_POWER_FACTOR,
@ -376,12 +352,10 @@ class TibberSensorElPrice(TibberSensor):
class TibberSensorRT(TibberSensor, update_coordinator.CoordinatorEntity): class TibberSensorRT(TibberSensor, update_coordinator.CoordinatorEntity):
"""Representation of a Tibber sensor for real time consumption.""" """Representation of a Tibber sensor for real time consumption."""
entity_description: TibberSensorEntityDescription
def __init__( def __init__(
self, self,
tibber_home, tibber_home,
description: TibberSensorEntityDescription, description: SensorEntityDescription,
initial_state, initial_state,
coordinator: TibberRtDataCoordinator, coordinator: TibberRtDataCoordinator,
): ):
@ -397,18 +371,6 @@ class TibberSensorRT(TibberSensor, update_coordinator.CoordinatorEntity):
if description.key in ("accumulatedCost", "accumulatedReward"): if description.key in ("accumulatedCost", "accumulatedReward"):
self._attr_native_unit_of_measurement = tibber_home.currency self._attr_native_unit_of_measurement = tibber_home.currency
if description.reset_type == ResetType.NEVER:
self._attr_last_reset = dt_util.utc_from_timestamp(0)
elif description.reset_type == ResetType.DAILY:
self._attr_last_reset = dt_util.as_utc(
dt_util.now().replace(hour=0, minute=0, second=0, microsecond=0)
)
elif description.reset_type == ResetType.HOURLY:
self._attr_last_reset = dt_util.as_utc(
dt_util.now().replace(minute=0, second=0, microsecond=0)
)
else:
self._attr_last_reset = None
@property @property
def available(self): def available(self):
@ -422,16 +384,6 @@ class TibberSensorRT(TibberSensor, update_coordinator.CoordinatorEntity):
state = live_measurement.get(self.entity_description.key) state = live_measurement.get(self.entity_description.key)
if state is None: if state is None:
return return
timestamp = dt_util.parse_datetime(live_measurement["timestamp"])
if timestamp is not None and state < self.state:
if self.entity_description.reset_type == ResetType.DAILY:
self._attr_last_reset = dt_util.as_utc(
timestamp.replace(hour=0, minute=0, second=0, microsecond=0)
)
elif self.entity_description.reset_type == ResetType.HOURLY:
self._attr_last_reset = dt_util.as_utc(
timestamp.replace(minute=0, second=0, microsecond=0)
)
if self.entity_description.key == "powerFactor": if self.entity_description.key == "powerFactor":
state *= 100.0 state *= 100.0
self._attr_native_value = state self._attr_native_value = state