Skip to content

Commit b61efdd

Browse files
Refactor, get price (#18)
* Refactor, get price Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * Refactor, get price Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * Refactor, get price Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
1 parent 9b95d4b commit b61efdd

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

custom_components/tibber_data/data_coordinator.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Data coordinator for Tibber."""
22
import datetime
33
import logging
4-
from typing import List
4+
from typing import List, Set
55

66
import tibber
77
from homeassistant.components.sensor import (
@@ -44,6 +44,7 @@ def __init__(self, hass, tibber_home: tibber.TibberHome, email: str, password: s
4444
self._password = password
4545
self._token = None
4646
self._chargers: List[str] = []
47+
self._month_consumption: Set[Consumption] = set()
4748

4849
_next_update = dt_util.now() - datetime.timedelta(minutes=1)
4950
self._update_functions = {
@@ -55,6 +56,14 @@ def __init__(self, hass, tibber_home: tibber.TibberHome, email: str, password: s
5556
if self.tibber_home.has_production:
5657
self._update_functions[self._get_production_data] = _next_update
5758

59+
def get_price_at(self, timestamp: datetime.datetime):
60+
"""Get price at a specific time."""
61+
timestamp = timestamp.replace(minute=0, second=0, microsecond=0)
62+
for consumption in self._month_consumption:
63+
if dt_util.as_local(consumption.timestamp) == dt_util.as_local(timestamp):
64+
return consumption.price
65+
return None
66+
5867
async def _async_update_data(self):
5968
"""Update data via API."""
6069
now = dt_util.now(dt_util.DEFAULT_TIME_ZONE)
@@ -282,6 +291,7 @@ async def _get_data(self, data, now):
282291
self.hass.data[DOMAIN][
283292
f"month_consumption_{self.tibber_home.home_id}"
284293
] = month_consumption
294+
self._month_consumption = month_consumption
285295

286296
if prices_tomorrow_available:
287297
next_update = min(

custom_components/tibber_data/sensor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def _attr_name(self):
8787
def _handle_coordinator_update(self) -> None:
8888
"""Handle updated data from the coordinator."""
8989
if self.entity_description.key == "est_current_price_with_subsidy":
90-
price_data = self.coordinator.tibber_home.current_price_data()
91-
native_value = price_data[0] - self.coordinator.data.get("est_subsidy", 0)
90+
price = self.coordinator.get_price_at(dt_util.now())
91+
native_value = price - self.coordinator.data.get("est_subsidy", 0)
9292
elif self.entity_description.key == "grid_price":
9393
native_value = self.coordinator.data.get(
9494
self.entity_description.key, {}
@@ -97,9 +97,9 @@ def _handle_coordinator_update(self) -> None:
9797
grid_price = self.coordinator.data.get("grid_price", {}).get(
9898
dt_util.now().replace(minute=0, second=0, microsecond=0)
9999
)
100-
price_data = self.coordinator.tibber_home.current_price_data()
100+
price = self.coordinator.get_price_at(dt_util.now())
101101
native_value = (
102-
grid_price + price_data[0] - self.coordinator.data.get("est_subsidy", 0)
102+
grid_price + price - self.coordinator.data.get("est_subsidy", 0)
103103
)
104104
else:
105105
native_value = self.coordinator.data.get(self.entity_description.key)

0 commit comments

Comments
 (0)