Skip to content
Open
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
6 changes: 6 additions & 0 deletions docs/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <simplipy.errors.BaseStationUnreachableError>`
(a subclass of {meth}`RequestError <simplipy.errors.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
Expand Down
24 changes: 20 additions & 4 deletions simplipy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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.
Expand All @@ -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"])
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/base_station_unreachable_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "NotFound",
"message": "Active sim not returned from comlink",
"code": "002",
"statusCode": 404,
"props": {}
}
45 changes: 45 additions & 0 deletions tests/system/test_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from simplipy import API
from simplipy.errors import (
BaseStationUnreachableError,
EndpointUnavailableError,
InvalidCredentialsError,
MaxUserPinsExceededError,
Expand Down Expand Up @@ -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,
Expand Down