Skip to content

Commit

Permalink
Merge pull request #140 from codesquadnest/pb/cast-fix
Browse files Browse the repository at this point in the history
fix(int cast): fix int cast raise `Invalid numeric value`
  • Loading branch information
pmpbaptista authored Dec 1, 2024
2 parents 438e2a4 + 6398c91 commit 1a52c6e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions tesla_smart_charger/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"}

Expand Down
15 changes: 8 additions & 7 deletions tesla_smart_charger/handlers/overload_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()

Expand All @@ -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:
Expand All @@ -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

Expand Down

0 comments on commit 1a52c6e

Please sign in to comment.