From 6398c91310a718d7fc949511a3cd202cd9180c8a Mon Sep 17 00:00:00 2001 From: Pedro Baptista Date: Sun, 1 Dec 2024 17:51:17 +0000 Subject: [PATCH] fix(int cast): fix int cast raise `Invalid numeric value` --- tesla_smart_charger/__main__.py | 4 ++-- tesla_smart_charger/handlers/overload_handler.py | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tesla_smart_charger/__main__.py b/tesla_smart_charger/__main__.py index 68cef1c..b3000dd 100644 --- a/tesla_smart_charger/__main__.py +++ b/tesla_smart_charger/__main__.py @@ -150,13 +150,13 @@ def overload() -> JSONResponse: charger_actual_current = vehicle_data["charge_state"]["charger_actual_current"] # Calculate the new charge limit - new_charge_limit = int(charger_actual_current) * float( + new_charge_limit = round(float(charger_actual_current)) * float( tesla_config.config["downStepPercentage"], ) try: # Set the new charge limit - response = tesla_api.set_charge_amp_limit(int(new_charge_limit)) + response = tesla_api.set_charge_amp_limit(round(float(new_charge_limit))) except HTTPException as e: return {"error": f"Set charge limit failed: {e!s}"} diff --git a/tesla_smart_charger/handlers/overload_handler.py b/tesla_smart_charger/handlers/overload_handler.py index 0732447..2390336 100644 --- a/tesla_smart_charger/handlers/overload_handler.py +++ b/tesla_smart_charger/handlers/overload_handler.py @@ -16,11 +16,11 @@ tesla_api = TeslaAPI(tesla_config) # Constants -SLEEP_TIME = int(tesla_config.config["sleepTimeSecs"]) +SLEEP_TIME = round(float(tesla_config.config["sleepTimeSecs"])) HOME_MAX_AMPS = float(tesla_config.config["homeMaxAmps"]) CHARGER_MAX_AMPS = float(tesla_config.config["chargerMaxAmps"]) CHARGER_MIN_AMPS = float(tesla_config.config["chargerMinAmps"]) -MAX_TESLA_API_QUERIES = int(constants.MAX_QUERIES) +MAX_TESLA_API_QUERIES = round(float(constants.MAX_QUERIES)) def _reload_config() -> None: @@ -57,7 +57,7 @@ def _calculate_new_charge_limit( f"consumption difference: {consumption_difference:.2f}A)" ) - return int(new_charge_limit) + return round(float(new_charge_limit)) def _get_current_consumption_in_amps(em_controller) -> float: @@ -112,7 +112,8 @@ def handle_overload() -> None: vehicle_data["state"] == "online" and vehicle_data["charge_state"]["charging_state"] == "Charging" and tesla_api_calls < MAX_TESLA_API_QUERIES - and int(charger_actual_current) < int(tesla_config.config["chargerMaxAmps"]) + and round(float(charger_actual_current)) + < round(float(tesla_config.config["chargerMaxAmps"])) ): _reload_config() @@ -134,7 +135,7 @@ def handle_overload() -> None: HOME_MAX_AMPS, ) - if int(new_charge_limit) != int(charger_actual_current): + if round(float(new_charge_limit)) != round(float(charger_actual_current)): tsc_logger.info(f"Setting new charge limit: {new_charge_limit}A") # Set the new charge limit try: @@ -145,8 +146,8 @@ def handle_overload() -> None: tesla_api_calls = 0 else: tsc_logger.info("No change in charge limit") - if int(charger_actual_current) == int( - tesla_config.config["chargerMaxAmps"] + if round(float(charger_actual_current)) == round( + float(tesla_config.config["chargerMaxAmps"]) ): tesla_api_calls += 1