Skip to content

Commit

Permalink
Energy Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 12, 2024
1 parent f1df74d commit b340809
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
16 changes: 16 additions & 0 deletions tesla_fleet_api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ class Scope(StrEnum):
ENERGY_CMDS = "energy_cmds"


class EnergyOperationMode(StrEnum):
"""Energy Operation Mode options"""

AUTONOMOUS = "autonomous"
SELF_CONSUMPTION = "self_consumption"
BACKUP = "backup"


class EnergyExportMode(StrEnum):
"""Energy Export Mode options"""

BATTERY_OK = "battery_ok"
PV_ONLY = "pv_only"
NEVER = "never"


class TelemetryField(StrEnum):
"""Fields available in telemetry streams"""

Expand Down
22 changes: 7 additions & 15 deletions tesla_fleet_api/energy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any
from .const import Method
from .const import Method, EnergyOperationMode, EnergyExportMode
from .energyspecific import EnergySpecific


Expand Down Expand Up @@ -91,24 +91,16 @@ async def grid_import_export(
self,
energy_site_id: int,
disallow_charge_from_grid_with_solar_installed: bool | None = None,
customer_preferred_export_rule: str | None = None,
customer_preferred_export_rule: EnergyExportMode|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(
Method.POST,
f"api/1/energy_sites/{energy_site_id}/grid_import_export",
json=data,
json={
"disallow_charge_from_grid_with_solar_installed": disallow_charge_from_grid_with_solar_installed,
"customer_preferred_export_rule": customer_preferred_export_rule,
},
)

async def live_status(self, energy_site_id: int) -> dict[str, Any]:
Expand All @@ -131,7 +123,7 @@ async def off_grid_vehicle_charging_reserve(
)

async def operation(
self, energy_site_id: int, default_real_mode: str
self, energy_site_id: int, default_real_mode: EnergyOperationMode | str
) -> dict[str, Any]:
"""Set the site's mode."""
return await self._request(
Expand Down
7 changes: 5 additions & 2 deletions tesla_fleet_api/energyspecific.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any
from .const import EnergyExportMode, EnergyOperationMode


class EnergySpecific:
Expand Down Expand Up @@ -69,7 +70,7 @@ async def energy_history(
async def grid_import_export(
self,
disallow_charge_from_grid_with_solar_installed: bool | None = None,
customer_preferred_export_rule: str | None = None,
customer_preferred_export_rule: EnergyExportMode | 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 All @@ -90,7 +91,9 @@ async def off_grid_vehicle_charging_reserve(
self.energy_site_id, off_grid_vehicle_charging_reserve_percent
)

async def operation(self, default_real_mode: str) -> dict[str, Any]:
async def operation(
self, default_real_mode: EnergyOperationMode | str
) -> dict[str, Any]:
"""Set the site's mode."""
return await self._parent.operation(
self.energy_site_id,
Expand Down
6 changes: 3 additions & 3 deletions tesla_fleet_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TeslaFleetError(BaseException):
"""Base class for all Tesla exceptions."""

message: str
status: int
status: int = 0
error: str | None
error_description: str | None

Expand Down Expand Up @@ -245,15 +245,15 @@ async def raise_for_status(resp: aiohttp.ClientResponse) -> None:
elif error == Error.UNAUTHORIZED_CLIENT:
raise UnauthorizedClient(data) from e
else:
raise TeslaFleetError(data) from e
raise InvalidRequest({error: e.message}) from e
elif resp.status == 401:
error = data.get("error")
if error == Error.TOKEN_EXPIRED:
raise OAuthExpired(data) from e
elif error == Error.MOBILE_ACCESS_DISABLED:
raise MobileAccessDisabled(data) from e
else:
raise InvalidToken(data) from e
raise InvalidToken({error: e.message}) from e
elif resp.status == 402:
raise PaymentRequired(data) from e
elif resp.status == 403:
Expand Down
3 changes: 2 additions & 1 deletion tesla_fleet_api/teslafleetapi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import aiohttp
from json import dumps
from .exceptions import raise_for_status, InvalidRegion, LibraryError
from typing import Any
from .const import SERVERS, Method, Error
Expand Down Expand Up @@ -93,7 +94,7 @@ async def _request(
LOGGER.debug("Parameters: %s", params)
if json:
json = {k: v for k, v in json.items() if v is not None}
LOGGER.debug("Body: %s", json)
LOGGER.debug("Body: %s", dumps(json))

async with self.session.request(
method,
Expand Down

0 comments on commit b340809

Please sign in to comment.