mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Tibber use dataclass (#53233)
* Tibber, use dataclass Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * Tibber, use dataclass Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
This commit is contained in:
parent
2b9b346a28
commit
0cc4231ac2
@ -1,6 +1,10 @@
|
|||||||
"""Support for Tibber sensors."""
|
"""Support for Tibber sensors."""
|
||||||
|
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
|
||||||
|
|
||||||
@ -45,108 +49,142 @@ MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
|
|||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
SIGNAL_UPDATE_ENTITY = "tibber_rt_update_{}"
|
SIGNAL_UPDATE_ENTITY = "tibber_rt_update_{}"
|
||||||
|
|
||||||
RT_SENSOR_MAP = {
|
|
||||||
"averagePower": ["average power", DEVICE_CLASS_POWER, POWER_WATT, None],
|
class ResetType(Enum):
|
||||||
"power": ["power", DEVICE_CLASS_POWER, POWER_WATT, None],
|
"""Data reset type."""
|
||||||
"powerProduction": ["power production", DEVICE_CLASS_POWER, POWER_WATT, None],
|
|
||||||
"minPower": ["min power", DEVICE_CLASS_POWER, POWER_WATT, None],
|
HOURLY = "hourly"
|
||||||
"maxPower": ["max power", DEVICE_CLASS_POWER, POWER_WATT, None],
|
DAILY = "daily"
|
||||||
"accumulatedConsumption": [
|
NEVER = "never"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TibberSensorMetadata:
|
||||||
|
"""Metadata for an individual Tibber sensor."""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
device_class: str
|
||||||
|
unit: str | None = None
|
||||||
|
state_class: str | None = None
|
||||||
|
reset_type: ResetType | None = None
|
||||||
|
|
||||||
|
|
||||||
|
RT_SENSOR_MAP: dict[str, TibberSensorMetadata] = {
|
||||||
|
"averagePower": TibberSensorMetadata(
|
||||||
|
"average power", DEVICE_CLASS_POWER, POWER_WATT
|
||||||
|
),
|
||||||
|
"power": TibberSensorMetadata(
|
||||||
|
"power",
|
||||||
|
DEVICE_CLASS_POWER,
|
||||||
|
POWER_WATT,
|
||||||
|
),
|
||||||
|
"powerProduction": TibberSensorMetadata(
|
||||||
|
"power production", DEVICE_CLASS_POWER, POWER_WATT
|
||||||
|
),
|
||||||
|
"minPower": TibberSensorMetadata("min power", DEVICE_CLASS_POWER, POWER_WATT),
|
||||||
|
"maxPower": TibberSensorMetadata("max power", DEVICE_CLASS_POWER, POWER_WATT),
|
||||||
|
"accumulatedConsumption": TibberSensorMetadata(
|
||||||
"accumulated consumption",
|
"accumulated consumption",
|
||||||
DEVICE_CLASS_ENERGY,
|
DEVICE_CLASS_ENERGY,
|
||||||
ENERGY_KILO_WATT_HOUR,
|
ENERGY_KILO_WATT_HOUR,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
ResetType.DAILY,
|
||||||
"accumulatedConsumptionLastHour": [
|
),
|
||||||
|
"accumulatedConsumptionLastHour": TibberSensorMetadata(
|
||||||
"accumulated consumption current hour",
|
"accumulated consumption current hour",
|
||||||
DEVICE_CLASS_ENERGY,
|
DEVICE_CLASS_ENERGY,
|
||||||
ENERGY_KILO_WATT_HOUR,
|
ENERGY_KILO_WATT_HOUR,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
ResetType.HOURLY,
|
||||||
"accumulatedProduction": [
|
),
|
||||||
|
"accumulatedProduction": TibberSensorMetadata(
|
||||||
"accumulated production",
|
"accumulated production",
|
||||||
DEVICE_CLASS_ENERGY,
|
DEVICE_CLASS_ENERGY,
|
||||||
ENERGY_KILO_WATT_HOUR,
|
ENERGY_KILO_WATT_HOUR,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
ResetType.DAILY,
|
||||||
"accumulatedProductionLastHour": [
|
),
|
||||||
|
"accumulatedProductionLastHour": TibberSensorMetadata(
|
||||||
"accumulated production current hour",
|
"accumulated production current hour",
|
||||||
DEVICE_CLASS_ENERGY,
|
DEVICE_CLASS_ENERGY,
|
||||||
ENERGY_KILO_WATT_HOUR,
|
ENERGY_KILO_WATT_HOUR,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
ResetType.HOURLY,
|
||||||
"lastMeterConsumption": [
|
),
|
||||||
|
"lastMeterConsumption": TibberSensorMetadata(
|
||||||
"last meter consumption",
|
"last meter consumption",
|
||||||
DEVICE_CLASS_ENERGY,
|
DEVICE_CLASS_ENERGY,
|
||||||
ENERGY_KILO_WATT_HOUR,
|
ENERGY_KILO_WATT_HOUR,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
"lastMeterProduction": [
|
"lastMeterProduction": TibberSensorMetadata(
|
||||||
"last meter production",
|
"last meter production",
|
||||||
DEVICE_CLASS_ENERGY,
|
DEVICE_CLASS_ENERGY,
|
||||||
ENERGY_KILO_WATT_HOUR,
|
ENERGY_KILO_WATT_HOUR,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
"voltagePhase1": [
|
"voltagePhase1": TibberSensorMetadata(
|
||||||
"voltage phase1",
|
"voltage phase1",
|
||||||
DEVICE_CLASS_VOLTAGE,
|
DEVICE_CLASS_VOLTAGE,
|
||||||
VOLT,
|
VOLT,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
"voltagePhase2": [
|
"voltagePhase2": TibberSensorMetadata(
|
||||||
"voltage phase2",
|
"voltage phase2",
|
||||||
DEVICE_CLASS_VOLTAGE,
|
DEVICE_CLASS_VOLTAGE,
|
||||||
VOLT,
|
VOLT,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
"voltagePhase3": [
|
"voltagePhase3": TibberSensorMetadata(
|
||||||
"voltage phase3",
|
"voltage phase3",
|
||||||
DEVICE_CLASS_VOLTAGE,
|
DEVICE_CLASS_VOLTAGE,
|
||||||
VOLT,
|
VOLT,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
"currentL1": [
|
"currentL1": TibberSensorMetadata(
|
||||||
"current L1",
|
"current L1",
|
||||||
DEVICE_CLASS_CURRENT,
|
DEVICE_CLASS_CURRENT,
|
||||||
ELECTRICAL_CURRENT_AMPERE,
|
ELECTRICAL_CURRENT_AMPERE,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
"currentL2": [
|
"currentL2": TibberSensorMetadata(
|
||||||
"current L2",
|
"current L2",
|
||||||
DEVICE_CLASS_CURRENT,
|
DEVICE_CLASS_CURRENT,
|
||||||
ELECTRICAL_CURRENT_AMPERE,
|
ELECTRICAL_CURRENT_AMPERE,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
"currentL3": [
|
"currentL3": TibberSensorMetadata(
|
||||||
"current L3",
|
"current L3",
|
||||||
DEVICE_CLASS_CURRENT,
|
DEVICE_CLASS_CURRENT,
|
||||||
ELECTRICAL_CURRENT_AMPERE,
|
ELECTRICAL_CURRENT_AMPERE,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
"signalStrength": [
|
"signalStrength": TibberSensorMetadata(
|
||||||
"signal strength",
|
"signal strength",
|
||||||
DEVICE_CLASS_SIGNAL_STRENGTH,
|
DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
SIGNAL_STRENGTH_DECIBELS,
|
SIGNAL_STRENGTH_DECIBELS,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
"accumulatedReward": [
|
"accumulatedReward": TibberSensorMetadata(
|
||||||
"accumulated reward",
|
"accumulated reward",
|
||||||
DEVICE_CLASS_MONETARY,
|
DEVICE_CLASS_MONETARY,
|
||||||
None,
|
None,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
ResetType.DAILY,
|
||||||
"accumulatedCost": [
|
),
|
||||||
|
"accumulatedCost": TibberSensorMetadata(
|
||||||
"accumulated cost",
|
"accumulated cost",
|
||||||
DEVICE_CLASS_MONETARY,
|
DEVICE_CLASS_MONETARY,
|
||||||
None,
|
None,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
ResetType.DAILY,
|
||||||
"powerFactor": [
|
),
|
||||||
|
"powerFactor": TibberSensorMetadata(
|
||||||
"power factor",
|
"power factor",
|
||||||
DEVICE_CLASS_POWER_FACTOR,
|
DEVICE_CLASS_POWER_FACTOR,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
],
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -312,39 +350,30 @@ class TibberSensorRT(TibberSensor):
|
|||||||
|
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, tibber_home, metadata: TibberSensorMetadata, initial_state):
|
||||||
self, tibber_home, sensor_name, device_class, unit, initial_state, state_class
|
|
||||||
):
|
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(tibber_home)
|
super().__init__(tibber_home)
|
||||||
self._sensor_name = sensor_name
|
|
||||||
self._model = "Tibber Pulse"
|
self._model = "Tibber Pulse"
|
||||||
self._device_name = f"{self._model} {self._home_name}"
|
self._device_name = f"{self._model} {self._home_name}"
|
||||||
|
self._metadata = metadata
|
||||||
|
|
||||||
self._attr_device_class = device_class
|
self._attr_device_class = metadata.device_class
|
||||||
self._attr_name = f"{self._sensor_name} {self._home_name}"
|
self._attr_name = f"{metadata.name} {self._home_name}"
|
||||||
self._attr_state = initial_state
|
self._attr_state = initial_state
|
||||||
self._attr_unique_id = f"{self._tibber_home.home_id}_rt_{self._sensor_name}"
|
self._attr_unique_id = f"{self._tibber_home.home_id}_rt_{metadata.name}"
|
||||||
self._attr_unit_of_measurement = unit
|
|
||||||
self._attr_state_class = state_class
|
if metadata.name in ["accumulated cost", "accumulated reward"]:
|
||||||
if sensor_name in [
|
self._attr_unit_of_measurement = tibber_home.currency
|
||||||
"last meter consumption",
|
else:
|
||||||
"last meter production",
|
self._attr_unit_of_measurement = metadata.unit
|
||||||
]:
|
self._attr_state_class = metadata.state_class
|
||||||
|
if metadata.reset_type == ResetType.NEVER:
|
||||||
self._attr_last_reset = dt_util.utc_from_timestamp(0)
|
self._attr_last_reset = dt_util.utc_from_timestamp(0)
|
||||||
elif self._sensor_name in [
|
elif metadata.reset_type == ResetType.DAILY:
|
||||||
"accumulated consumption",
|
|
||||||
"accumulated production",
|
|
||||||
"accumulated cost",
|
|
||||||
"accumulated reward",
|
|
||||||
]:
|
|
||||||
self._attr_last_reset = dt_util.as_utc(
|
self._attr_last_reset = dt_util.as_utc(
|
||||||
dt_util.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
dt_util.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
)
|
)
|
||||||
elif self._sensor_name in [
|
elif metadata.reset_type == ResetType.HOURLY:
|
||||||
"accumulated consumption current hour",
|
|
||||||
"accumulated production current hour",
|
|
||||||
]:
|
|
||||||
self._attr_last_reset = dt_util.as_utc(
|
self._attr_last_reset = dt_util.as_utc(
|
||||||
dt_util.now().replace(minute=0, second=0, microsecond=0)
|
dt_util.now().replace(minute=0, second=0, microsecond=0)
|
||||||
)
|
)
|
||||||
@ -369,19 +398,11 @@ class TibberSensorRT(TibberSensor):
|
|||||||
@callback
|
@callback
|
||||||
def _set_state(self, state, timestamp):
|
def _set_state(self, state, timestamp):
|
||||||
"""Set sensor state."""
|
"""Set sensor state."""
|
||||||
if state < self._attr_state and self._sensor_name in [
|
if state < self._attr_state and self._metadata.reset_type == ResetType.DAILY:
|
||||||
"accumulated consumption",
|
|
||||||
"accumulated production",
|
|
||||||
"accumulated cost",
|
|
||||||
"accumulated reward",
|
|
||||||
]:
|
|
||||||
self._attr_last_reset = dt_util.as_utc(
|
self._attr_last_reset = dt_util.as_utc(
|
||||||
timestamp.replace(hour=0, minute=0, second=0, microsecond=0)
|
timestamp.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
)
|
)
|
||||||
if state < self._attr_state and self._sensor_name in [
|
if state < self._attr_state and self._metadata.reset_type == ResetType.HOURLY:
|
||||||
"accumulated consumption current hour",
|
|
||||||
"accumulated production current hour",
|
|
||||||
]:
|
|
||||||
self._attr_last_reset = dt_util.as_utc(
|
self._attr_last_reset = dt_util.as_utc(
|
||||||
timestamp.replace(minute=0, second=0, microsecond=0)
|
timestamp.replace(minute=0, second=0, microsecond=0)
|
||||||
)
|
)
|
||||||
@ -427,18 +448,11 @@ class TibberRtDataHandler:
|
|||||||
timestamp,
|
timestamp,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
sensor_name, device_class, unit, state_class = RT_SENSOR_MAP[
|
sensor_meta = RT_SENSOR_MAP[sensor_type]
|
||||||
sensor_type
|
|
||||||
]
|
|
||||||
if sensor_type in ["accumulatedCost", "accumulatedReward"]:
|
|
||||||
unit = self._tibber_home.currency
|
|
||||||
entity = TibberSensorRT(
|
entity = TibberSensorRT(
|
||||||
self._tibber_home,
|
self._tibber_home,
|
||||||
sensor_name,
|
sensor_meta,
|
||||||
device_class,
|
|
||||||
unit,
|
|
||||||
state,
|
state,
|
||||||
state_class,
|
|
||||||
)
|
)
|
||||||
new_entities.append(entity)
|
new_entities.append(entity)
|
||||||
self._entities[sensor_type] = entity.unique_id
|
self._entities[sensor_type] = entity.unique_id
|
||||||
|
Loading…
x
Reference in New Issue
Block a user