Fix battery charging endpoint

This commit is contained in:
Adam Štrauch 2025-10-02 22:51:18 +02:00
parent 6071a1ccc2
commit 4bd28614cc
Signed by: cx
GPG key ID: 7262DAFE292BCE20
3 changed files with 7 additions and 9 deletions

View file

@ -1,9 +1,6 @@
from dataclasses import dataclass
import datetime
from typing import List
from fastapi import HTTPException
from calculator.miner import PriceNotFound, get_energy_prices, get_eur_czk_ratio
from calculator.schema import BatteryChargingInfo, Price, SpotPrices
@ -41,7 +38,8 @@ def get_spot_prices(date: datetime.date, hour:str, kwh_fees_low:float, kwh_fees_
def battery_charging_info(kwh_fees_low:float, kwh_fees_high:float, sell_fees:float, VAT:float, low_tariff_hours:List[int], no_cache: bool = False, battery_kwh_price:float=2.5) -> BatteryChargingInfo:
today = datetime.date.today()
hour = datetime.datetime.now().hour
now = datetime.datetime.now()
hour = f"{now.hour}:{minutes_to_15mins(now.minute)}"
tomorrow = today + datetime.timedelta(days=1)
spot_prices_today:SpotPrices = get_spot_prices(today, hour, kwh_fees_low, kwh_fees_high, sell_fees, VAT, low_tariff_hours, no_cache)
@ -58,8 +56,8 @@ def battery_charging_info(kwh_fees_low:float, kwh_fees_high:float, sell_fees:flo
# Add charging hours if the price is just 10% above the most expensive charging hour
if charging_hours:
for h in range(5,24):
value = spot_prices_today.spot_hours_total_sorted.hours[str(h)]
for h in spot_prices_today.spot_hours_total_sorted.hours.keys():
value = spot_prices_today.spot_hours_total_sorted.hours[h]
if value <= max_cheapest_hour*1.1:
charging_hours.append(h)

View file

@ -8,7 +8,7 @@ from fastapi.responses import RedirectResponse, HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from calculator.calc import battery_charging_info, get_spot_prices, minutes_to_15mins
from calculator.miner import PriceNotFound, get_energy_prices, get_eur_czk_ratio
from calculator.miner import PriceNotFound
from calculator.schema import BatteryChargingInfo, CheapestHours, DayPrice, MostExpensiveHours, Price
from .consts import VAT

View file

@ -19,7 +19,7 @@ CACHE_PATH = "./cache"
def get_energy_prices(d: datetime.date=datetime.date.today(), no_cache:bool=False) -> Dict[str, float]:
"""
Get energy prices for a given date.
"""
date_str = d.strftime("%Y-%m-%d")
@ -82,7 +82,7 @@ def get_energy_prices(d: datetime.date=datetime.date.today(), no_cache:bool=Fals
else:
correct_format_hours = hours
return correct_format_hours
return hours
#def get_currency_ratio(currency):
# r = requests.get(url_currency)