From d5b5893aad3efad68990d55907268674c23e164c Mon Sep 17 00:00:00 2001 From: Victor Heorhiadi Date: Sun, 15 Nov 2026 10:48:03 -0800 Subject: [PATCH] feat: Raise BaseStationUnreachableError on comlink 404 Non-cached (forceUpdate=true) SS3 requests can fail with HTTP 404 and body {"type": "NotFound", "code": "002", "message": "Active sim not returned from comlink"} when the SimpliSafe cloud can't reach the base station, while cached requests still succeed. Map that type/code pair to a new BaseStationUnreachableError (a RequestError subclass, so existing handlers keep working) so callers can fall back to cached data. Other NotFound codes are unaffected. --- docs/system.md | 6 +++ simplipy/errors.py | 24 ++++++++-- tests/conftest.py | 13 ++++++ .../base_station_unreachable_response.json | 7 +++ tests/system/test_v3.py | 45 +++++++++++++++++++ 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/base_station_unreachable_response.json diff --git a/docs/system.md b/docs/system.md index 50da40c6..941b443e 100644 --- a/docs/system.md +++ b/docs/system.md @@ -234,6 +234,12 @@ refresh: await system.async_update(include_system=False, include_settings=False, cached=False) ``` +If the SimpliSafe cloud can't reach the base station to fulfill a non-cached +update, a +{meth}`BaseStationUnreachableError ` +(a subclass of {meth}`RequestError `) is raised; a +cached update may still succeed. + There are two crucial differences between V2 and V3 systems when updating: - V2 systems, which use only 2G cell connectivity, will be slower to update diff --git a/simplipy/errors.py b/simplipy/errors.py index 6ccfed84..ba8aac31 100644 --- a/simplipy/errors.py +++ b/simplipy/errors.py @@ -41,6 +41,12 @@ class RequestError(SimplipyError): pass +class BaseStationUnreachableError(RequestError): + """An error related to the SimpliSafe cloud not reaching the base station.""" + + pass + + class WebsocketError(SimplipyError): """An error related to generic websocket errors.""" @@ -82,6 +88,14 @@ class NotConnectedError(WebsocketError): "PinError": PinError, } +# Generic error types (like "NotFound") are only mapped for specific codes: +DATA_ERROR_CODE_MAP: dict[tuple[str, str], type[SimplipyError]] = { + # Observed (undocumented) response to a forceUpdate=true request when the cloud + # can't reach the base station; a cached request may still succeed. See + # https://github.com/home-assistant/core/issues/96848 for the same 404 symptom. + ("NotFound", "002"): BaseStationUnreachableError, +} + def raise_on_data_error(data: dict[str, Any] | None) -> None: """Raise a specific error if the data payload suggests there is one. @@ -95,8 +109,10 @@ def raise_on_data_error(data: dict[str, Any] | None) -> None: if not data: return - if (error_type := data.get("type")) not in DATA_ERROR_MAP: - return + error_type = data.get("type") + + if error_type in DATA_ERROR_MAP: + raise DATA_ERROR_MAP[error_type](data["message"]) - error = DATA_ERROR_MAP[error_type](data["message"]) - raise error + if (error_key := (error_type, data.get("code"))) in DATA_ERROR_CODE_MAP: + raise DATA_ERROR_CODE_MAP[error_key](data["message"]) diff --git a/tests/conftest.py b/tests/conftest.py index 893eb3e6..88bbf28c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -207,6 +207,19 @@ def subscriptions_response_fixture() -> dict[str, Any]: return cast(dict[str, Any], json.loads(load_fixture("subscriptions_response.json"))) +@pytest.fixture(name="base_station_unreachable_response", scope="session") +def base_station_unreachable_response_fixture() -> dict[str, Any]: + """Define a fixture to return a base station unreachable response. + + Returns: + An API response payload. + """ + return cast( + dict[str, Any], + json.loads(load_fixture("base_station_unreachable_response.json")), + ) + + @pytest.fixture(name="unavailable_endpoint_response", scope="session") def unavailable_endpoint_response_fixture() -> dict[str, Any]: """Define a fixture to return an unavailable endpoint response. diff --git a/tests/fixtures/base_station_unreachable_response.json b/tests/fixtures/base_station_unreachable_response.json new file mode 100644 index 00000000..1b4a473d --- /dev/null +++ b/tests/fixtures/base_station_unreachable_response.json @@ -0,0 +1,7 @@ +{ + "type": "NotFound", + "message": "Active sim not returned from comlink", + "code": "002", + "statusCode": 404, + "props": {} +} diff --git a/tests/system/test_v3.py b/tests/system/test_v3.py index d1b1914c..3526998b 100644 --- a/tests/system/test_v3.py +++ b/tests/system/test_v3.py @@ -12,6 +12,7 @@ from simplipy import API from simplipy.errors import ( + BaseStationUnreachableError, EndpointUnavailableError, InvalidCredentialsError, MaxUserPinsExceededError, @@ -1723,6 +1724,50 @@ async def test_unavailable_endpoint( aresponses.assert_plan_strictly_followed() +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("response_code", "expected_error"), + [ + pytest.param("002", BaseStationUnreachableError, id="base_station_unreachable"), + pytest.param("999", RequestError, id="other_not_found"), + ], +) +async def test_live_update_not_found( + aresponses: ResponsesMockServer, + authenticated_simplisafe_server_v3: ResponsesMockServer, + base_station_unreachable_response: dict[str, Any], + response_code: str, + expected_error: type[RequestError], +) -> None: + """Test the error raised when a live update returns an HTTP 404.""" + async with authenticated_simplisafe_server_v3: + authenticated_simplisafe_server_v3.add( + "api.simplisafe.com", + f"/v1/ss3/subscriptions/{TEST_SUBSCRIPTION_ID}/settings/normal", + "get", + response=aiohttp.web_response.json_response( + {**base_station_unreachable_response, "code": response_code}, + status=404, + ), + ) + + async with aiohttp.ClientSession() as session: + simplisafe = await API.async_from_auth( + TEST_AUTHORIZATION_CODE, TEST_CODE_VERIFIER, session=session + ) + systems = await simplisafe.async_get_systems() + system = systems[TEST_SYSTEM_ID] + + with pytest.raises(RequestError) as err: + await system.async_update( + include_subscription=False, include_devices=False, cached=False + ) + + assert err.type is expected_error + + aresponses.assert_plan_strictly_followed() + + @pytest.mark.asyncio async def test_update_system_data( aresponses: ResponsesMockServer,