mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Use EntityDescription - tibber (#53569)
This commit is contained in:
parent
13443310fe
commit
f599c5a39e
@ -2,11 +2,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
from enum import Enum
|
||||
import logging
|
||||
from random import randrange
|
||||
from typing import NamedTuple
|
||||
|
||||
import aiohttp
|
||||
|
||||
@ -20,6 +20,7 @@ from homeassistant.components.sensor import (
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ELECTRIC_CURRENT_AMPERE,
|
||||
@ -58,140 +59,158 @@ class ResetType(Enum):
|
||||
NEVER = "never"
|
||||
|
||||
|
||||
class TibberSensorMetadata(NamedTuple):
|
||||
"""Metadata for an individual Tibber sensor."""
|
||||
@dataclass
|
||||
class TibberSensorEntityDescription(SensorEntityDescription):
|
||||
"""Describes Tibber sensor entity."""
|
||||
|
||||
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",
|
||||
RT_SENSOR_MAP: dict[str, TibberSensorEntityDescription] = {
|
||||
"averagePower": TibberSensorEntityDescription(
|
||||
key="averagePower",
|
||||
name="average power",
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
unit=POWER_WATT,
|
||||
unit_of_measurement=POWER_WATT,
|
||||
),
|
||||
"power": TibberSensorMetadata(
|
||||
"power",
|
||||
"power": TibberSensorEntityDescription(
|
||||
key="power",
|
||||
name="power",
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
unit=POWER_WATT,
|
||||
unit_of_measurement=POWER_WATT,
|
||||
),
|
||||
"powerProduction": TibberSensorMetadata(
|
||||
"power production",
|
||||
"powerProduction": TibberSensorEntityDescription(
|
||||
key="powerProduction",
|
||||
name="power production",
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
unit=POWER_WATT,
|
||||
unit_of_measurement=POWER_WATT,
|
||||
),
|
||||
"minPower": TibberSensorMetadata(
|
||||
"min power",
|
||||
"minPower": TibberSensorEntityDescription(
|
||||
key="minPower",
|
||||
name="min power",
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
unit=POWER_WATT,
|
||||
unit_of_measurement=POWER_WATT,
|
||||
),
|
||||
"maxPower": TibberSensorMetadata(
|
||||
"max power",
|
||||
"maxPower": TibberSensorEntityDescription(
|
||||
key="maxPower",
|
||||
name="max power",
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
unit=POWER_WATT,
|
||||
unit_of_measurement=POWER_WATT,
|
||||
),
|
||||
"accumulatedConsumption": TibberSensorMetadata(
|
||||
"accumulated consumption",
|
||||
"accumulatedConsumption": TibberSensorEntityDescription(
|
||||
key="accumulatedConsumption",
|
||||
name="accumulated consumption",
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
reset_type=ResetType.DAILY,
|
||||
),
|
||||
"accumulatedConsumptionLastHour": TibberSensorMetadata(
|
||||
"accumulated consumption current hour",
|
||||
"accumulatedConsumptionLastHour": TibberSensorEntityDescription(
|
||||
key="accumulatedConsumptionLastHour",
|
||||
name="accumulated consumption current hour",
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
reset_type=ResetType.HOURLY,
|
||||
),
|
||||
"accumulatedProduction": TibberSensorMetadata(
|
||||
"accumulated production",
|
||||
"accumulatedProduction": TibberSensorEntityDescription(
|
||||
key="accumulatedProduction",
|
||||
name="accumulated production",
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
reset_type=ResetType.DAILY,
|
||||
),
|
||||
"accumulatedProductionLastHour": TibberSensorMetadata(
|
||||
"accumulated production current hour",
|
||||
"accumulatedProductionLastHour": TibberSensorEntityDescription(
|
||||
key="accumulatedProductionLastHour",
|
||||
name="accumulated production current hour",
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
reset_type=ResetType.HOURLY,
|
||||
),
|
||||
"lastMeterConsumption": TibberSensorMetadata(
|
||||
"last meter consumption",
|
||||
"lastMeterConsumption": TibberSensorEntityDescription(
|
||||
key="lastMeterConsumption",
|
||||
name="last meter consumption",
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"lastMeterProduction": TibberSensorMetadata(
|
||||
"last meter production",
|
||||
"lastMeterProduction": TibberSensorEntityDescription(
|
||||
key="lastMeterProduction",
|
||||
name="last meter production",
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"voltagePhase1": TibberSensorMetadata(
|
||||
"voltage phase1",
|
||||
"voltagePhase1": TibberSensorEntityDescription(
|
||||
key="voltagePhase1",
|
||||
name="voltage phase1",
|
||||
device_class=DEVICE_CLASS_VOLTAGE,
|
||||
unit=ELECTRIC_POTENTIAL_VOLT,
|
||||
unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"voltagePhase2": TibberSensorMetadata(
|
||||
"voltage phase2",
|
||||
"voltagePhase2": TibberSensorEntityDescription(
|
||||
key="voltagePhase2",
|
||||
name="voltage phase2",
|
||||
device_class=DEVICE_CLASS_VOLTAGE,
|
||||
unit=ELECTRIC_POTENTIAL_VOLT,
|
||||
unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"voltagePhase3": TibberSensorMetadata(
|
||||
"voltage phase3",
|
||||
"voltagePhase3": TibberSensorEntityDescription(
|
||||
key="voltagePhase3",
|
||||
name="voltage phase3",
|
||||
device_class=DEVICE_CLASS_VOLTAGE,
|
||||
unit=ELECTRIC_POTENTIAL_VOLT,
|
||||
unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"currentL1": TibberSensorMetadata(
|
||||
"current L1",
|
||||
"currentL1": TibberSensorEntityDescription(
|
||||
key="currentL1",
|
||||
name="current L1",
|
||||
device_class=DEVICE_CLASS_CURRENT,
|
||||
unit=ELECTRIC_CURRENT_AMPERE,
|
||||
unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"currentL2": TibberSensorMetadata(
|
||||
"current L2",
|
||||
"currentL2": TibberSensorEntityDescription(
|
||||
key="currentL2",
|
||||
name="current L2",
|
||||
device_class=DEVICE_CLASS_CURRENT,
|
||||
unit=ELECTRIC_CURRENT_AMPERE,
|
||||
unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"currentL3": TibberSensorMetadata(
|
||||
"current L3",
|
||||
"currentL3": TibberSensorEntityDescription(
|
||||
key="currentL3",
|
||||
name="current L3",
|
||||
device_class=DEVICE_CLASS_CURRENT,
|
||||
unit=ELECTRIC_CURRENT_AMPERE,
|
||||
unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"signalStrength": TibberSensorMetadata(
|
||||
"signal strength",
|
||||
"signalStrength": TibberSensorEntityDescription(
|
||||
key="signalStrength",
|
||||
name="signal strength",
|
||||
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
unit=SIGNAL_STRENGTH_DECIBELS,
|
||||
unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"accumulatedReward": TibberSensorMetadata(
|
||||
"accumulated reward",
|
||||
"accumulatedReward": TibberSensorEntityDescription(
|
||||
key="accumulatedReward",
|
||||
name="accumulated reward",
|
||||
device_class=DEVICE_CLASS_MONETARY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
reset_type=ResetType.DAILY,
|
||||
),
|
||||
"accumulatedCost": TibberSensorMetadata(
|
||||
"accumulated cost",
|
||||
"accumulatedCost": TibberSensorEntityDescription(
|
||||
key="accumulatedCost",
|
||||
name="accumulated cost",
|
||||
device_class=DEVICE_CLASS_MONETARY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
reset_type=ResetType.DAILY,
|
||||
),
|
||||
"powerFactor": TibberSensorMetadata(
|
||||
"power factor",
|
||||
"powerFactor": TibberSensorEntityDescription(
|
||||
key="powerFactor",
|
||||
name="power factor",
|
||||
device_class=DEVICE_CLASS_POWER_FACTOR,
|
||||
unit=PERCENTAGE,
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
}
|
||||
@ -358,31 +377,33 @@ class TibberSensorRT(TibberSensor):
|
||||
"""Representation of a Tibber sensor for real time consumption."""
|
||||
|
||||
_attr_should_poll = False
|
||||
entity_description: TibberSensorEntityDescription
|
||||
|
||||
def __init__(self, tibber_home, metadata: TibberSensorMetadata, initial_state):
|
||||
def __init__(
|
||||
self,
|
||||
tibber_home,
|
||||
description: TibberSensorEntityDescription,
|
||||
initial_state,
|
||||
):
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(tibber_home)
|
||||
self.entity_description = description
|
||||
self._model = "Tibber Pulse"
|
||||
self._device_name = f"{self._model} {self._home_name}"
|
||||
self._metadata = metadata
|
||||
|
||||
self._attr_device_class = metadata.device_class
|
||||
self._attr_name = f"{metadata.name} {self._home_name}"
|
||||
self._attr_name = f"{description.name} {self._home_name}"
|
||||
self._attr_state = initial_state
|
||||
self._attr_unique_id = f"{self._tibber_home.home_id}_rt_{metadata.name}"
|
||||
self._attr_unique_id = f"{self._tibber_home.home_id}_rt_{description.name}"
|
||||
|
||||
if metadata.name in ("accumulated cost", "accumulated reward"):
|
||||
if description.name in ("accumulated cost", "accumulated reward"):
|
||||
self._attr_unit_of_measurement = tibber_home.currency
|
||||
else:
|
||||
self._attr_unit_of_measurement = metadata.unit
|
||||
self._attr_state_class = metadata.state_class
|
||||
if metadata.reset_type == ResetType.NEVER:
|
||||
if description.reset_type == ResetType.NEVER:
|
||||
self._attr_last_reset = dt_util.utc_from_timestamp(0)
|
||||
elif metadata.reset_type == ResetType.DAILY:
|
||||
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 metadata.reset_type == ResetType.HOURLY:
|
||||
elif description.reset_type == ResetType.HOURLY:
|
||||
self._attr_last_reset = dt_util.as_utc(
|
||||
dt_util.now().replace(minute=0, second=0, microsecond=0)
|
||||
)
|
||||
@ -407,11 +428,17 @@ class TibberSensorRT(TibberSensor):
|
||||
@callback
|
||||
def _set_state(self, state, timestamp):
|
||||
"""Set sensor state."""
|
||||
if state < self._attr_state and self._metadata.reset_type == ResetType.DAILY:
|
||||
if (
|
||||
state < self._attr_state
|
||||
and 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)
|
||||
)
|
||||
if state < self._attr_state and self._metadata.reset_type == ResetType.HOURLY:
|
||||
if (
|
||||
state < self._attr_state
|
||||
and self.entity_description.reset_type == ResetType.HOURLY
|
||||
):
|
||||
self._attr_last_reset = dt_util.as_utc(
|
||||
timestamp.replace(minute=0, second=0, microsecond=0)
|
||||
)
|
||||
@ -457,10 +484,9 @@ class TibberRtDataHandler:
|
||||
timestamp,
|
||||
)
|
||||
else:
|
||||
sensor_meta = RT_SENSOR_MAP[sensor_type]
|
||||
entity = TibberSensorRT(
|
||||
self._tibber_home,
|
||||
sensor_meta,
|
||||
RT_SENSOR_MAP[sensor_type],
|
||||
state,
|
||||
)
|
||||
new_entities.append(entity)
|
||||
|
Loading…
x
Reference in New Issue
Block a user