From 4bd28614cc18a7bfa62b017d91365baee425fc48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Thu, 2 Oct 2025 22:51:18 +0200 Subject: [PATCH] Fix battery charging endpoint --- calculator/calc.py | 10 ++++------ calculator/main.py | 2 +- calculator/miner.py | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/calculator/calc.py b/calculator/calc.py index 7718ec0..4850fa4 100644 --- a/calculator/calc.py +++ b/calculator/calc.py @@ -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) diff --git a/calculator/main.py b/calculator/main.py index 85d84a2..d024794 100644 --- a/calculator/main.py +++ b/calculator/main.py @@ -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 diff --git a/calculator/miner.py b/calculator/miner.py index 73fdd44..048b7e9 100644 --- a/calculator/miner.py +++ b/calculator/miner.py @@ -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)