diff --git a/tesla_fleet_api/energy.py b/tesla_fleet_api/energy.py index a5a6fe5..03755cf 100644 --- a/tesla_fleet_api/energy.py +++ b/tesla_fleet_api/energy.py @@ -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( @@ -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, @@ -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]: @@ -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 }, ) @@ -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]: @@ -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}, ) diff --git a/tesla_fleet_api/energyspecific.py b/tesla_fleet_api/energyspecific.py index d08652c..c76510b 100644 --- a/tesla_fleet_api/energyspecific.py +++ b/tesla_fleet_api/energyspecific.py @@ -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( @@ -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( diff --git a/tesla_fleet_api/partner.py b/tesla_fleet_api/partner.py index 8a4391b..525ec44 100644 --- a/tesla_fleet_api/partner.py +++ b/tesla_fleet_api/partner.py @@ -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]: diff --git a/tesla_fleet_api/teslafleetapi.py b/tesla_fleet_api/teslafleetapi.py index f9d6a7f..b65ce9d 100644 --- a/tesla_fleet_api/teslafleetapi.py +++ b/tesla_fleet_api/teslafleetapi.py @@ -70,7 +70,6 @@ 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.""" @@ -78,13 +77,11 @@ async def _request( 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} @@ -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: