Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 30 additions & 78 deletions homeassistant/components/tibber/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
from __future__ import annotations

import asyncio
from dataclasses import dataclass
from datetime import timedelta
from enum import Enum
import logging
from random import randrange

Expand All @@ -19,6 +17,7 @@
DEVICE_CLASS_SIGNAL_STRENGTH,
DEVICE_CLASS_VOLTAGE,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
SensorEntityDescription,
)
Expand Down Expand Up @@ -49,166 +48,143 @@
SIGNAL_UPDATE_ENTITY = "tibber_rt_update_{}"


class ResetType(Enum):
"""Data reset type."""

HOURLY = "hourly"
DAILY = "daily"
NEVER = "never"


@dataclass
class TibberSensorEntityDescription(SensorEntityDescription):
"""Describes Tibber sensor entity."""

reset_type: ResetType | None = None


RT_SENSORS: tuple[TibberSensorEntityDescription, ...] = (
TibberSensorEntityDescription(
RT_SENSORS: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="averagePower",
name="average power",
device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="power",
name="power",
device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT,
native_unit_of_measurement=POWER_WATT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="powerProduction",
name="power production",
device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT,
native_unit_of_measurement=POWER_WATT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="minPower",
name="min power",
device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="maxPower",
name="max power",
device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="accumulatedConsumption",
name="accumulated consumption",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.DAILY,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="accumulatedConsumptionLastHour",
name="accumulated consumption current hour",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.HOURLY,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="accumulatedProduction",
name="accumulated production",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.DAILY,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="accumulatedProductionLastHour",
name="accumulated production current hour",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.HOURLY,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="lastMeterConsumption",
name="last meter consumption",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.NEVER,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="lastMeterProduction",
name="last meter production",
device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.NEVER,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="voltagePhase1",
name="voltage phase1",
device_class=DEVICE_CLASS_VOLTAGE,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
state_class=STATE_CLASS_MEASUREMENT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="voltagePhase2",
name="voltage phase2",
device_class=DEVICE_CLASS_VOLTAGE,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
state_class=STATE_CLASS_MEASUREMENT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="voltagePhase3",
name="voltage phase3",
device_class=DEVICE_CLASS_VOLTAGE,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
state_class=STATE_CLASS_MEASUREMENT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="currentL1",
name="current L1",
device_class=DEVICE_CLASS_CURRENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
state_class=STATE_CLASS_MEASUREMENT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="currentL2",
name="current L2",
device_class=DEVICE_CLASS_CURRENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
state_class=STATE_CLASS_MEASUREMENT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="currentL3",
name="current L3",
device_class=DEVICE_CLASS_CURRENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
state_class=STATE_CLASS_MEASUREMENT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="signalStrength",
name="signal strength",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
state_class=STATE_CLASS_MEASUREMENT,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="accumulatedReward",
name="accumulated reward",
device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.DAILY,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="accumulatedCost",
name="accumulated cost",
device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_MEASUREMENT,
reset_type=ResetType.DAILY,
),
TibberSensorEntityDescription(
SensorEntityDescription(
key="powerFactor",
name="power factor",
device_class=DEVICE_CLASS_POWER_FACTOR,
Expand Down Expand Up @@ -376,12 +352,10 @@ async def _fetch_data(self):
class TibberSensorRT(TibberSensor, update_coordinator.CoordinatorEntity):
"""Representation of a Tibber sensor for real time consumption."""

entity_description: TibberSensorEntityDescription

def __init__(
self,
tibber_home,
description: TibberSensorEntityDescription,
description: SensorEntityDescription,
initial_state,
coordinator: TibberRtDataCoordinator,
):
Expand All @@ -397,18 +371,6 @@ def __init__(

if description.key in ("accumulatedCost", "accumulatedReward"):
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
def available(self):
Expand All @@ -422,16 +384,6 @@ def _handle_coordinator_update(self) -> None:
state = live_measurement.get(self.entity_description.key)
if state is None:
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":
state *= 100.0
self._attr_native_value = state
Expand Down