mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
Add timestamp sensors for highest and lowest price times (#161639)
This commit is contained in:
@@ -4,12 +4,17 @@ from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from greenplanet_energy_api import GreenPlanetEnergyAPI
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import CURRENCY_EURO, UnitOfEnergy
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
@@ -28,7 +33,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
class GreenPlanetEnergySensorEntityDescription(SensorEntityDescription):
|
||||
"""Describes Green Planet Energy sensor entity."""
|
||||
|
||||
value_fn: Callable[[GreenPlanetEnergyAPI, dict[str, Any]], float | None]
|
||||
value_fn: Callable[[GreenPlanetEnergyAPI, dict[str, Any]], float | datetime | None]
|
||||
|
||||
|
||||
SENSOR_DESCRIPTIONS: list[GreenPlanetEnergySensorEntityDescription] = [
|
||||
@@ -40,6 +45,16 @@ SENSOR_DESCRIPTIONS: list[GreenPlanetEnergySensorEntityDescription] = [
|
||||
suggested_display_precision=4,
|
||||
value_fn=lambda api, data: api.get_highest_price_today(data),
|
||||
),
|
||||
GreenPlanetEnergySensorEntityDescription(
|
||||
key="gpe_highest_price_time",
|
||||
translation_key="highest_price_time",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
value_fn=lambda api, data: (
|
||||
dt_util.start_of_local_day().replace(hour=hour)
|
||||
if (hour := api.get_highest_price_today_with_hour(data)[1]) is not None
|
||||
else None
|
||||
),
|
||||
),
|
||||
GreenPlanetEnergySensorEntityDescription(
|
||||
key="gpe_lowest_price_day",
|
||||
translation_key="lowest_price_day",
|
||||
@@ -48,6 +63,17 @@ SENSOR_DESCRIPTIONS: list[GreenPlanetEnergySensorEntityDescription] = [
|
||||
translation_placeholders={"time_range": "(06:00-18:00)"},
|
||||
value_fn=lambda api, data: api.get_lowest_price_day(data),
|
||||
),
|
||||
GreenPlanetEnergySensorEntityDescription(
|
||||
key="gpe_lowest_price_day_time",
|
||||
translation_key="lowest_price_day_time",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
translation_placeholders={"time_range": "(06:00-18:00)"},
|
||||
value_fn=lambda api, data: (
|
||||
dt_util.start_of_local_day().replace(hour=hour)
|
||||
if (hour := api.get_lowest_price_day_with_hour(data)[1]) is not None
|
||||
else None
|
||||
),
|
||||
),
|
||||
GreenPlanetEnergySensorEntityDescription(
|
||||
key="gpe_lowest_price_night",
|
||||
translation_key="lowest_price_night",
|
||||
@@ -56,6 +82,17 @@ SENSOR_DESCRIPTIONS: list[GreenPlanetEnergySensorEntityDescription] = [
|
||||
translation_placeholders={"time_range": "(18:00-06:00)"},
|
||||
value_fn=lambda api, data: api.get_lowest_price_night(data),
|
||||
),
|
||||
GreenPlanetEnergySensorEntityDescription(
|
||||
key="gpe_lowest_price_night_time",
|
||||
translation_key="lowest_price_night_time",
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
translation_placeholders={"time_range": "(18:00-06:00)"},
|
||||
value_fn=lambda api, data: (
|
||||
dt_util.start_of_local_day().replace(hour=hour)
|
||||
if (hour := api.get_lowest_price_night_with_hour(data)[1]) is not None
|
||||
else None
|
||||
),
|
||||
),
|
||||
GreenPlanetEnergySensorEntityDescription(
|
||||
key="gpe_current_price",
|
||||
translation_key="current_price",
|
||||
@@ -105,7 +142,7 @@ class GreenPlanetEnergySensor(
|
||||
)
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
def native_value(self) -> float | datetime | None:
|
||||
"""Return the state of the sensor."""
|
||||
return self.entity_description.value_fn(
|
||||
self.coordinator.api, self.coordinator.data
|
||||
|
||||
@@ -19,14 +19,23 @@
|
||||
"current_price": {
|
||||
"name": "Current price"
|
||||
},
|
||||
"highest_price_time": {
|
||||
"name": "Highest price time"
|
||||
},
|
||||
"highest_price_today": {
|
||||
"name": "Highest price today"
|
||||
},
|
||||
"lowest_price_day": {
|
||||
"name": "Lowest price day {time_range}"
|
||||
},
|
||||
"lowest_price_day_time": {
|
||||
"name": "Lowest price day time {time_range}"
|
||||
},
|
||||
"lowest_price_night": {
|
||||
"name": "Lowest price night {time_range}"
|
||||
},
|
||||
"lowest_price_night_time": {
|
||||
"name": "Lowest price night time {time_range}"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -52,6 +52,56 @@
|
||||
'state': '0.25',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.green_planet_energy_highest_price_time-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.green_planet_energy_highest_price_time',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Highest price time',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Highest price time',
|
||||
'platform': 'green_planet_energy',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'highest_price_time',
|
||||
'unique_id': 'gpe_highest_price_time',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.green_planet_energy_highest_price_time-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'timestamp',
|
||||
'friendly_name': 'Green Planet Energy Highest price time',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.green_planet_energy_highest_price_time',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '2024-01-02T07:00:00+00:00',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.green_planet_energy_highest_price_today-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
@@ -158,6 +208,56 @@
|
||||
'state': '0.26',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.green_planet_energy_lowest_price_day_time_06_00_18_00-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.green_planet_energy_lowest_price_day_time_06_00_18_00',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Lowest price day time (06:00-18:00)',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Lowest price day time (06:00-18:00)',
|
||||
'platform': 'green_planet_energy',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'lowest_price_day_time',
|
||||
'unique_id': 'gpe_lowest_price_day_time',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.green_planet_energy_lowest_price_day_time_06_00_18_00-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'timestamp',
|
||||
'friendly_name': 'Green Planet Energy Lowest price day time (06:00-18:00)',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.green_planet_energy_lowest_price_day_time_06_00_18_00',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '2024-01-01T14:00:00+00:00',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.green_planet_energy_lowest_price_night_18_00_06_00-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
@@ -211,3 +311,53 @@
|
||||
'state': '0.2',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.green_planet_energy_lowest_price_night_time_18_00_06_00-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.green_planet_energy_lowest_price_night_time_18_00_06_00',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Lowest price night time (18:00-06:00)',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Lowest price night time (18:00-06:00)',
|
||||
'platform': 'green_planet_energy',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'lowest_price_night_time',
|
||||
'unique_id': 'gpe_lowest_price_night_time',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.green_planet_energy_lowest_price_night_time_18_00_06_00-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'timestamp',
|
||||
'friendly_name': 'Green Planet Energy Lowest price night time (18:00-06:00)',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.green_planet_energy_lowest_price_night_time_18_00_06_00',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '2024-01-01T08:00:00+00:00',
|
||||
})
|
||||
# ---
|
||||
|
||||
Reference in New Issue
Block a user