Skip to content

Commit

Permalink
Fix energy commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 8, 2024
1 parent 72ea6d8 commit 23ee851
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
30 changes: 19 additions & 11 deletions tesla_fleet_api/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async def backup(
return await self._request(
Methods.POST,
f"api/1/energy_sites/{energy_site_id}/backup",
data={"backup_reserve_percent": backup_reserve_percent},
json={"backup_reserve_percent": backup_reserve_percent},
)

async def backup_history(
Expand All @@ -36,7 +36,7 @@ async def backup_history(
return await self._request(
Methods.GET,
f"api/1/energy_sites/{energy_site_id}/calendar_history",
{
json={
"kind": kind,
"start_date": start_date,
"end_date": end_date,
Expand Down Expand Up @@ -90,17 +90,25 @@ async def energy_history(
async def grid_import_export(
self,
energy_site_id: int,
disallow_charge_from_grid_with_solar_installed: bool,
customer_preferred_export_rule: str,
disallow_charge_from_grid_with_solar_installed: bool | None = None,
customer_preferred_export_rule: str | None = None,
) -> dict[str, Any]:
"""Allow/disallow charging from the grid and exporting energy to the grid."""
data = {}
if disallow_charge_from_grid_with_solar_installed is not None:
data[
"disallow_charge_from_grid_with_solar_installed"
] = disallow_charge_from_grid_with_solar_installed
if customer_preferred_export_rule is not None:
data["customer_preferred_export_rule"] = customer_preferred_export_rule
if not data:
raise ValueError(
"At least one of disallow_charge_from_grid_with_solar_installed or customer_preferred_export_rule must be set."
)
return await self._request(
Methods.POST,
f"api/1/energy_sites/{energy_site_id}/grid_import_export",
data={
"disallow_charge_from_grid_with_solar_installed": disallow_charge_from_grid_with_solar_installed,
"customer_preferred_export_rule": customer_preferred_export_rule,
},
json=data,
)

async def live_status(self, energy_site_id: int) -> dict[str, Any]:
Expand All @@ -117,7 +125,7 @@ async def off_grid_vehicle_charging_reserve(
return await self._request(
Methods.POST,
f"api/1/energy_sites/{energy_site_id}/off_grid_vehicle_charging_reserve",
data={
json={
"off_grid_vehicle_charging_reserve_percent": off_grid_vehicle_charging_reserve_percent
},
)
Expand All @@ -129,7 +137,7 @@ async def operation(
return await self._request(
Methods.POST,
f"api/1/energy_sites/{energy_site_id}/operation",
data={"default_real_mode": default_real_mode},
json={"default_real_mode": default_real_mode},
)

async def site_info(self, energy_site_id: int) -> dict[str, Any]:
Expand All @@ -144,5 +152,5 @@ async def storm_mode(self, energy_site_id: int, enabled: bool) -> dict[str, Any]
return await self._request(
Methods.POST,
f"api/1/energy_sites/{energy_site_id}/storm_mode",
data={"enabled": enabled},
json={"enabled": enabled},
)
6 changes: 3 additions & 3 deletions tesla_fleet_api/energyspecific.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def backup(self, backup_reserve_percent: int) -> dict[str, Any]:
"""Adjust the site's backup reserve."""
return await self._parent.backup(
self.energy_site_id,
data={"backup_reserve_percent": backup_reserve_percent},
backup_reserve_percent,
)

async def backup_history(
Expand Down Expand Up @@ -68,8 +68,8 @@ async def energy_history(

async def grid_import_export(
self,
disallow_charge_from_grid_with_solar_installed: bool,
customer_preferred_export_rule: str,
disallow_charge_from_grid_with_solar_installed: bool | None = None,
customer_preferred_export_rule: str | None = None,
) -> dict[str, Any]:
"""Allow/disallow charging from the grid and exporting energy to the grid."""
return await self._parent.grid_import_export(
Expand Down
2 changes: 1 addition & 1 deletion tesla_fleet_api/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, parent):
async def public_key(self, domain: str | None = None) -> dict[str, Any]:
"""Returns the public key associated with a domain. It can be used to ensure the registration was successful."""
return await self._request(
Methods.GET, "api/1/partner_accounts/public_key", {"domain": domain}
Methods.GET, "api/1/partner_accounts/public_key", params={"domain": domain}
)

async def register(self, domain: str) -> dict[str, Any]:
Expand Down
8 changes: 2 additions & 6 deletions tesla_fleet_api/teslafleetapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,18 @@ async def _request(
method: Methods,
path: str,
params: dict[str:Any] | None = None,
data: dict[str:Any] | None = None,
json: dict[str:Any] | None = None,
):
"""Send a request to the Tesla Fleet API."""

if not self.server:
raise ValueError("Server was not set at init. Call find_server() first.")

if method == Methods.GET and (data is not None or json is not None):
raise ValueError("GET requests cannot have data or json parameters.")
if method == Methods.GET and json is not None:
raise ValueError("GET requests cannot have a body.")

if params:
params = {k: v for k, v in params.items() if v is not None}
if data:
data = {k: v for k, v in data.items() if v is not None}
if json:
json = {k: v for k, v in json.items() if v is not None}

Expand All @@ -95,7 +92,6 @@ async def _request(
"Authorization": f"Bearer {self.access_token}",
"Content-Type": "application/json",
},
data=data,
json=json,
params=params,
) as resp:
Expand Down

0 comments on commit 23ee851

Please sign in to comment.