Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion tesla_fleet_api/tesla/powerwall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,43 @@
DEFAULT_GATEWAY_HOST,
GRID_EXPORT_RULES,
OPERATION_MODES,
IslanderPhase,
IslanderSide,
PowerwallClient,
SiteInfo,
backup_time_remaining,
battery_level,
battery_level_raw,
current_power,
islander_frequency,
islander_voltage,
raw_to_scaled_reserve,
raw_to_scaled_soc,
scaled_to_raw_reserve,
scaled_to_raw_soc,
site_info,
)
from .components import (
PV_STRING_STATES,
PV_STRINGS,
BatteryRole,
BatteryUnit,
BatteryUnitReadings,
PvString,
PvStringState,
SignalValue,
battery_unit_readings,
battery_units,
component,
component_float,
component_part_number,
component_serial,
component_signal,
pv_power,
pv_string_current,
pv_string_state,
pv_string_voltage,
pv_strings,
)
from .energysite import (
ISLAND_MODE_OFF_GRID,
Expand Down Expand Up @@ -81,6 +97,8 @@
"ISLAND_MODE_OFF_GRID",
"ISLAND_MODE_ON_GRID",
"OPERATION_MODES",
"PV_STRINGS",
"PV_STRING_STATES",
"AuthorizationRole",
"AuthorizedClient",
"AuthorizedClientState",
Expand All @@ -92,40 +110,54 @@
"BatteryCalibrationDelayStatus",
"BatteryRole",
"BatteryUnit",
"BatteryUnitReadings",
"ComponentsPayload",
"ConfigPayload",
"ControllableDeviceProgram",
"ControllableDeviceProgramsPayload",
"ControllerPayload",
"FirmwareDetails",
"IslanderPhase",
"IslanderSide",
"ManualBackupInfo",
"PowerLocation",
"PowerwallAuthenticationError",
"PowerwallClient",
"PowerwallCommandError",
"PowerwallConnectionError",
"PowerwallEnergySite",
"PowerwallEmptyReplyError",
"PowerwallEnergySite",
"PowerwallError",
"PowerwallFaultError",
"PowerwallNotSupportedError",
"PowerwallProtocolError",
"PowerwallRateLimitError",
"PvString",
"PvStringState",
"SignalValue",
"SiteInfo",
"StatusPayload",
"backup_time_remaining",
"battery_level",
"battery_level_raw",
"battery_unit_readings",
"battery_units",
"component",
"component_float",
"component_part_number",
"component_serial",
"component_signal",
"current_power",
"islander_frequency",
"islander_voltage",
"pv_power",
"pv_string_current",
"pv_string_state",
"pv_string_voltage",
"pv_strings",
"raw_to_scaled_reserve",
"raw_to_scaled_soc",
"scaled_to_raw_reserve",
"scaled_to_raw_soc",
"site_info",
]
64 changes: 63 additions & 1 deletion tesla_fleet_api/tesla/powerwall/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import time
from collections.abc import Callable, Mapping
from types import TracebackType
from typing import Any, Final, cast
from dataclasses import dataclass
from typing import Any, Final, Literal, cast

import aiohttp
from google.protobuf.timestamp_pb2 import Timestamp
Expand Down Expand Up @@ -1702,6 +1703,61 @@ def current_power(status: StatusPayload) -> dict[str, float | None]:
}


IslanderPhase = Literal[1, 2, 3]
IslanderSide = Literal["main", "load"]
"""``main`` is the grid side of the islanding contactor, ``load`` the home side."""


def _islander_measurement(status: StatusPayload, field: str) -> float | None:
value = _lookup(status, "esCan", "bus", "ISLANDER", "ISLAND_AcMeasurements", field)
if isinstance(value, bool) or not isinstance(value, (int, float)):
return None
return float(value)


def islander_frequency(
status: StatusPayload, phase: IslanderPhase, side: IslanderSide
) -> float | None:
"""Return the islander's measured frequency in Hz for ``phase`` and ``side``."""
return _islander_measurement(status, f"ISLAND_FreqL{phase}_{side.capitalize()}")


def islander_voltage(
status: StatusPayload, phase: IslanderPhase, side: IslanderSide
) -> float | None:
"""Return the islander's measured line-to-neutral voltage for ``phase`` and ``side``.

A single phase site reports near zero on phases 2 and 3.
"""
return _islander_measurement(status, f"ISLAND_VL{phase}N_{side.capitalize()}")


@dataclass(frozen=True, slots=True)
class SiteInfo:
"""The grid connection settings in the gateway's ``config.json`` ``site_info``.

Each value is None when the gateway does not report it as text.
"""

grid_code: str | None
country: str | None
distributor: str | None


def site_info(config: ConfigPayload) -> SiteInfo:
"""Return the grid connection settings from a :meth:`PowerwallClient.get_config` payload."""

def text(key: str) -> str | None:
value = _lookup(config, "site_info", key)
return value if isinstance(value, str) else None

return SiteInfo(
grid_code=text("grid_code"),
country=text("country"),
distributor=text("distributor"),
)


def backup_time_remaining(status: StatusPayload) -> float | None:
"""Estimated backup runtime in hours at current load, or None if unknown."""
remaining = _lookup(
Expand All @@ -1717,13 +1773,19 @@ def backup_time_remaining(status: StatusPayload) -> float | None:
"DEFAULT_GATEWAY_HOST",
"GRID_EXPORT_RULES",
"OPERATION_MODES",
"IslanderPhase",
"IslanderSide",
"PowerwallClient",
"SiteInfo",
"backup_time_remaining",
"battery_level",
"battery_level_raw",
"current_power",
"islander_frequency",
"islander_voltage",
"raw_to_scaled_reserve",
"raw_to_scaled_soc",
"scaled_to_raw_reserve",
"scaled_to_raw_soc",
"site_info",
]
Loading
Loading