Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 11 additions & 25 deletions packages/control/optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from control import data
from control.ocpp import OcppMixin
from control.optional_data import TARIFF_UPDATE_HOUR, FlexibleTariff, GridFee, OptionalData, PricingGet
from control.optional_data import FlexibleTariff, GridFee, OptionalData, PricingGet
from helpermodules import hardware_configuration
from helpermodules.constants import NO_ERROR
from helpermodules.pub import Pub
Expand Down Expand Up @@ -235,37 +235,23 @@ def ep_get_loading_hours(self, duration: float, remaining_time: float) -> List[i
return []

def et_price_update_required(self) -> bool:
def is_tomorrow(last_timestamp: str) -> bool:
return (day_of(date=datetime.now()) < day_of(datetime.fromtimestamp(float(last_timestamp)))
or day_of(date=datetime.now()).hour < TARIFF_UPDATE_HOUR)

def day_of(date: datetime) -> datetime:
return date.replace(hour=0, minute=0, second=0, microsecond=0)

def get_last_entry_time_stamp() -> str:
last_known_timestamp = "0"
if self.data.electricity_pricing.get.prices is not None:
last_known_timestamp = max(self.data.electricity_pricing.get.prices)
return last_known_timestamp
self._set_ep_configured()
if self.data.electricity_pricing.configured is False:
return False
if len(self.data.electricity_pricing.get.prices) == 0:
return True
if self.data.electricity_pricing.get.next_query_time is None:
return True
if is_tomorrow(get_last_entry_time_stamp()):
if timecheck.create_timestamp() > self.data.electricity_pricing.get.next_query_time:
next_query_formatted = datetime.fromtimestamp(
self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S")
log.info(f'Wartezeit {next_query_formatted} abgelaufen, Strompreise werden abgefragt')
return True
else:
next_query_formatted = datetime.fromtimestamp(
self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S")
log.info(f'Nächster Abruf der Strompreise {next_query_formatted}.')
return False
return False
if timecheck.create_timestamp() > self.data.electricity_pricing.get.next_query_time:
next_query_formatted = datetime.fromtimestamp(
self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S")
log.info(f'Wartezeit {next_query_formatted} abgelaufen, Strompreise werden abgefragt')
return True
else:
next_query_formatted = datetime.fromtimestamp(
self.data.electricity_pricing.get.next_query_time).strftime("%Y%m%d-%H:%M:%S")
log.info(f'Nächster Abruf der Strompreise {next_query_formatted}.')
return False

def ocpp_transfer_meter_values(self):
try:
Expand Down
25 changes: 17 additions & 8 deletions packages/control/optional_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from helpermodules.pub import Pub
from modules.display_themes.cards.config import CardsDisplayTheme

TARIFF_UPDATE_HOUR = 14 # latest expected time for daily tariff update
# Stunden für tägliche Tarifaktualisierung, manche Anbieter aktualisieren mehrfach täglich
TARIFF_UPDATE_HOURS = [2, 8, 14, 20]


@dataclass
Expand Down Expand Up @@ -53,13 +54,21 @@ def prices(self) -> Dict:
def prices(self, value: Dict):
self._prices = value
if value:
next_query_time = datetime.fromtimestamp(float(max(value))).replace(
hour=TARIFF_UPDATE_HOUR, minute=0, second=0
) + timedelta(
# actully ET providers issue next day prices up to half an hour earlier then 14:00
# reduce serverload on their site by trying early and randomizing query time
minutes=random.randint(1, 7) * -5
)
now = datetime.now()
current_hour = now.hour
next_hour = None
for hour in TARIFF_UPDATE_HOURS:
if hour > current_hour:
next_hour = hour
break
# Wenn keine Stunde heute gefunden, nimm die erste Stunde vom nächsten Tag
if next_hour is None:
next_hour = TARIFF_UPDATE_HOURS[0]
next_query_time = (now + timedelta(days=1)).replace(hour=next_hour, minute=0, second=0, microsecond=0)
else:
next_query_time = now.replace(hour=next_hour, minute=0, second=0, microsecond=0)
# reduce serverload on their site by trying early and randomizing query time
next_query_time += timedelta(minutes=random.randint(1, 7) * -5)
Pub().pub("openWB/set/optional/ep/get/next_query_time", next_query_time.timestamp())


Expand Down