From ccdc39447ac0b59199cf270bec6a0694226b6c44 Mon Sep 17 00:00:00 2001 From: "Brett Adams (Firstmate)" Date: Thu, 26 Nov 2026 10:12:18 +1000 Subject: [PATCH 1/2] Add is_legacy_gateway to detect a Backup Gateway 1 from site_info --- docs/energy_local_control.md | 8 +++ tesla_fleet_api/tesla/powerwall/__init__.py | 2 + tesla_fleet_api/tesla/powerwall/client.py | 42 ++++++++++- tests/powerwall/test_legacy_gateway.py | 78 +++++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 tests/powerwall/test_legacy_gateway.py diff --git a/docs/energy_local_control.md b/docs/energy_local_control.md index 5abec2b..73ce740 100644 --- a/docs/energy_local_control.md +++ b/docs/energy_local_control.md @@ -108,6 +108,14 @@ async def make_local_energysite(session: aiohttp.ClientSession) -> PowerwallEner return PowerwallEnergySite(powerwall_client) ``` +An early Tesla Backup Gateway 1 (Powerwall 2) rejects the last-5-characters +login `PowerwallClient` sends and needs the full gateway password. Build its +client with `PowerwallClient.legacy_gateway(...)`, which takes the same +arguments. `is_legacy_gateway(site_info)` tells you which one a site needs: pass +it the body of the cloud `site_info()` response (`response`), and it returns +True when a listed gateway, or the site DIN when none is listed, has the +Gateway 1 part number `1118431-`. + The example uses `192.168.91.1`, the gateway's own WiFi access point. When your client connects over your own LAN instead, you can discover the gateway's LAN IP over the cloud with diff --git a/tesla_fleet_api/tesla/powerwall/__init__.py b/tesla_fleet_api/tesla/powerwall/__init__.py index a32f704..d238856 100644 --- a/tesla_fleet_api/tesla/powerwall/__init__.py +++ b/tesla_fleet_api/tesla/powerwall/__init__.py @@ -30,6 +30,7 @@ current_power, islander_frequency, islander_voltage, + is_legacy_gateway, raw_to_scaled_reserve, raw_to_scaled_soc, scaled_to_raw_reserve, @@ -150,6 +151,7 @@ "current_power", "islander_frequency", "islander_voltage", + "is_legacy_gateway", "pv_power", "pv_string_current", "pv_string_state", diff --git a/tesla_fleet_api/tesla/powerwall/client.py b/tesla_fleet_api/tesla/powerwall/client.py index 14283b9..12f0adb 100644 --- a/tesla_fleet_api/tesla/powerwall/client.py +++ b/tesla_fleet_api/tesla/powerwall/client.py @@ -42,6 +42,7 @@ transport_pb2, ) +from ...exceptions import InvalidResponse from ..island import island_mode_rejection from . import queries from .exceptions import ( @@ -132,6 +133,10 @@ _TEG_FIELD_SET_ISLAND_MODE_REQUEST: Final = 3 _TEG_FIELD_TRIGGER_ISLANDING_REQUEST: Final = 5 +# Backup Gateway 1 part number prefix, as in a gateway's ``part_number`` and +# at the start of its DIN. +_GATEWAY_1_PART_NUMBER: Final = "1118431-" + def _lookup(data: Any, *keys: str) -> Any: """Walk a nested mapping; return None if any key is missing.""" for key in keys: @@ -226,7 +231,9 @@ def legacy_gateway( These units reject the last-5-characters login that the default constructor sends, and need the full ``gateway_password`` instead. - Everything else is the same as the default constructor. + Everything else is the same as the default constructor. Use + :func:`is_legacy_gateway` on the site's cloud ``site_info`` to tell + which constructor a site needs. """ return cls( host=host, @@ -1758,6 +1765,38 @@ def text(key: str) -> str | None: ) +def is_legacy_gateway(site_info: Any) -> bool: + """Return True when a site's gateway is a Backup Gateway 1. + + ``site_info`` is the body of the cloud ``site_info`` response (the + ``"response"`` object). A Gateway 1 has part number ``1118431-``; it is + read from ``components.gateways[].part_number``, or from the site ``id`` + (the gateway DIN, which starts with the part number) when no gateways are + listed. A True result means the site needs + :meth:`PowerwallClient.legacy_gateway`, which logs in with the full + gateway password. Raises :class:`InvalidResponse` when ``site_info`` is + not a mapping or carries neither gateways nor an ``id``. + """ + if not isinstance(site_info, Mapping): + raise InvalidResponse(repr(site_info)) + body = cast(Mapping[str, Any], site_info) + gateways = _lookup(body, "components", "gateways") + if isinstance(gateways, list) and gateways: + part_numbers = [ + _lookup(gateway, "part_number") + for gateway in cast(list[Any], gateways) + ] + elif "id" in body: + part_numbers = [body["id"]] + else: + raise InvalidResponse("site_info has no gateways and no id") + return any( + isinstance(part_number, str) + and part_number.startswith(_GATEWAY_1_PART_NUMBER) + for part_number in part_numbers + ) + + def backup_time_remaining(status: StatusPayload) -> float | None: """Estimated backup runtime in hours at current load, or None if unknown.""" remaining = _lookup( @@ -1783,6 +1822,7 @@ def backup_time_remaining(status: StatusPayload) -> float | None: "current_power", "islander_frequency", "islander_voltage", + "is_legacy_gateway", "raw_to_scaled_reserve", "raw_to_scaled_soc", "scaled_to_raw_reserve", diff --git a/tests/powerwall/test_legacy_gateway.py b/tests/powerwall/test_legacy_gateway.py new file mode 100644 index 0000000..dd329b7 --- /dev/null +++ b/tests/powerwall/test_legacy_gateway.py @@ -0,0 +1,78 @@ +"""Tests for is_legacy_gateway over the cloud site_info body.""" + +from __future__ import annotations + +from typing import Any + +import pytest + +from tesla_fleet_api.exceptions import InvalidResponse +from tesla_fleet_api.tesla.powerwall import is_legacy_gateway + +GATEWAY_1_DIN = "1118431-01-F--TG000000000000" +GATEWAY_2_DIN = "1232100-00-E--TG000000000000" + + +def _gateway(part_number: str) -> dict[str, Any]: + return { + "device_id": "device", + "din": f"{part_number}--TG000000000000", + "serial_number": "TG000000000000", + "part_number": part_number, + "is_active": True, + } + + +@pytest.mark.parametrize( + ("site_info", "expected"), + [ + pytest.param( + { + "id": GATEWAY_2_DIN, + "components": {"gateways": [_gateway("1118431-01-F")]}, + }, + True, + id="gateway_1_part_number", + ), + pytest.param( + { + "id": GATEWAY_2_DIN, + "components": {"gateways": [_gateway("1232100-00-E")]}, + }, + False, + id="gateway_2_part_number", + ), + pytest.param( + { + "id": GATEWAY_1_DIN, + "components": {"gateways": [_gateway("1232100-00-E")]}, + }, + False, + id="listed_gateway_wins_over_id", + ), + pytest.param( + {"id": GATEWAY_1_DIN, "components": {"gateways": []}}, + True, + id="gateway_1_site_din", + ), + pytest.param( + {"id": GATEWAY_1_DIN, "components": {}}, True, id="no_gateways_key" + ), + pytest.param({"id": GATEWAY_2_DIN}, False, id="gateway_2_site_din"), + ], +) +def test_is_legacy_gateway(site_info: dict[str, Any], expected: bool) -> None: + assert is_legacy_gateway(site_info) is expected + + +@pytest.mark.parametrize( + "site_info", + [ + pytest.param(None, id="null_body"), + pytest.param({}, id="empty_body"), + pytest.param({"components": {"gateways": []}}, id="no_gateways_no_id"), + ], +) +def test_is_legacy_gateway_unknown_shape(site_info: Any) -> None: + with pytest.raises(InvalidResponse): + is_legacy_gateway(site_info) From 1b117672579cdd5bb3d435d48a33eac01ef9f034 Mon Sep 17 00:00:00 2001 From: "Brett Adams (Firstmate)" Date: Thu, 26 Nov 2026 10:16:13 +1000 Subject: [PATCH 2/2] Fall back to the site id when no listed gateway has a part number --- tesla_fleet_api/tesla/powerwall/client.py | 30 +++++++++++------------ tests/powerwall/test_legacy_gateway.py | 9 +++++++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/tesla_fleet_api/tesla/powerwall/client.py b/tesla_fleet_api/tesla/powerwall/client.py index 12f0adb..d592812 100644 --- a/tesla_fleet_api/tesla/powerwall/client.py +++ b/tesla_fleet_api/tesla/powerwall/client.py @@ -1771,29 +1771,29 @@ def is_legacy_gateway(site_info: Any) -> bool: ``site_info`` is the body of the cloud ``site_info`` response (the ``"response"`` object). A Gateway 1 has part number ``1118431-``; it is read from ``components.gateways[].part_number``, or from the site ``id`` - (the gateway DIN, which starts with the part number) when no gateways are - listed. A True result means the site needs + (the gateway DIN, which starts with the part number) when no listed + gateway has a part number. A True result means the site needs :meth:`PowerwallClient.legacy_gateway`, which logs in with the full gateway password. Raises :class:`InvalidResponse` when ``site_info`` is - not a mapping or carries neither gateways nor an ``id``. + not a mapping or carries neither a gateway part number nor an ``id``. """ if not isinstance(site_info, Mapping): raise InvalidResponse(repr(site_info)) body = cast(Mapping[str, Any], site_info) gateways = _lookup(body, "components", "gateways") - if isinstance(gateways, list) and gateways: - part_numbers = [ - _lookup(gateway, "part_number") - for gateway in cast(list[Any], gateways) - ] - elif "id" in body: - part_numbers = [body["id"]] - else: - raise InvalidResponse("site_info has no gateways and no id") + part_numbers: list[str] = [] + if isinstance(gateways, list): + for gateway in cast(list[Any], gateways): + part_number = _lookup(gateway, "part_number") + if isinstance(part_number, str): + part_numbers.append(part_number) + if not part_numbers: + site_id = body.get("id") + if not isinstance(site_id, str): + raise InvalidResponse("site_info has no gateway part number and no id") + part_numbers = [site_id] return any( - isinstance(part_number, str) - and part_number.startswith(_GATEWAY_1_PART_NUMBER) - for part_number in part_numbers + part_number.startswith(_GATEWAY_1_PART_NUMBER) for part_number in part_numbers ) diff --git a/tests/powerwall/test_legacy_gateway.py b/tests/powerwall/test_legacy_gateway.py index dd329b7..c8ebfa9 100644 --- a/tests/powerwall/test_legacy_gateway.py +++ b/tests/powerwall/test_legacy_gateway.py @@ -59,6 +59,11 @@ def _gateway(part_number: str) -> dict[str, Any]: {"id": GATEWAY_1_DIN, "components": {}}, True, id="no_gateways_key" ), pytest.param({"id": GATEWAY_2_DIN}, False, id="gateway_2_site_din"), + pytest.param( + {"id": GATEWAY_1_DIN, "components": {"gateways": [{"part_number": None}]}}, + True, + id="gateway_without_part_number_falls_back_to_id", + ), ], ) def test_is_legacy_gateway(site_info: dict[str, Any], expected: bool) -> None: @@ -71,6 +76,10 @@ def test_is_legacy_gateway(site_info: dict[str, Any], expected: bool) -> None: pytest.param(None, id="null_body"), pytest.param({}, id="empty_body"), pytest.param({"components": {"gateways": []}}, id="no_gateways_no_id"), + pytest.param( + {"components": {"gateways": [{"din": GATEWAY_1_DIN}]}}, + id="no_part_number_no_id", + ), ], ) def test_is_legacy_gateway_unknown_shape(site_info: Any) -> None: