Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -376,19 +376,62 @@ async def _request_cloud_api_data(
return response.get("data")


def _plugin_product_ids(home_data: HomeData) -> list[int]:
def _positive_int(raw: Any) -> int | None:
value = str(raw if raw is not None else "").strip()
if not value.isdigit():
return None
number = int(value)
return number if number > 0 else None


def _numeric_product_ids_by_model(product_catalog: Any) -> dict[str, int]:
"""Model -> numeric product id, from the cloud's ``GET /api/v5/product`` answer.

Newer products (a226, a296, ...) are listed in ``home_data`` with a string id only, but the cloud
still has a numeric id for each model, and ``/api/v1/appplugin`` only takes numeric ids (a string
is answered with code 1002 "parameter error"). The answer's nesting is not documented, so every
object holding a numeric ``id`` and a ``model`` is taken.
"""
found: dict[str, int] = {}

def walk(node: Any) -> None:
if isinstance(node, dict):
model = node.get("model")
number = _positive_int(node.get("id"))
if isinstance(model, str) and model and number is not None:
found.setdefault(model, number)
for value in node.values():
walk(value)
elif isinstance(node, list):
for value in node:
walk(value)

walk(product_catalog)
return found


def _plugin_product_ids(home_data: HomeData, numeric_ids_by_model: dict[str, int] | None = None) -> list[int]:
"""The numeric product ids to ask ``/api/v1/appplugin`` for.

Numeric ids are taken as they are. A string id is looked up through its product's model in
[numeric_ids_by_model]; one that cannot be mapped is left out, as the catalog refuses strings.
Before string ids were mapped, an account with only such products sent no ids at all, so the
device plugin catalog was never fetched for them.
"""
by_model = numeric_ids_by_model or {}
model_by_string_id = {
str(product.id): product.model for product in home_data.products or [] if getattr(product, "model", None)
}
raw_ids = [product.id for product in home_data.products or []]
raw_ids += [getattr(device, "product_id", None) for device in _all_home_devices(home_data)]
product_ids: set[int] = set()
for product in home_data.products or []:
try:
product_ids.add(int(product.id))
except (TypeError, ValueError):
continue
for device in _all_home_devices(home_data):
try:
product_ids.add(int(getattr(device, "product_id", 0) or 0))
except (TypeError, ValueError):
continue
return sorted(product_id for product_id in product_ids if product_id > 0)
for raw in raw_ids:
number = _positive_int(raw)
if number is None and raw is not None:
number = by_model.get(model_by_string_id.get(str(raw).strip(), ""))
if number is not None:
product_ids.add(number)
return sorted(product_ids)


async def _fetch_plugin_catalogs(
Expand Down Expand Up @@ -442,7 +485,12 @@ async def fetch_catalog(
"/api/v1/plugins",
params={"type": 2, "apiLevel": 10043},
)
product_ids = _plugin_product_ids(home_data)
# Only needed for products home_data lists with a string id: the catalog takes numeric ids.
numeric_ids_by_model: dict[str, int] = {}
if any(_positive_int(product.id) is None for product in home_data.products or []):
await fetch_catalog("product", "/api/v5/product")
numeric_ids_by_model = _numeric_product_ids_by_model(catalogs["product"].get("data"))
product_ids = _plugin_product_ids(home_data, numeric_ids_by_model)
if product_ids:
await fetch_catalog(
"appplugin",
Expand Down
56 changes: 56 additions & 0 deletions tests/test_plugin_product_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""The product ids sent to the cloud's /api/v1/appplugin catalog during cloud import."""

from __future__ import annotations

from types import SimpleNamespace

# Importing the backend bridge puts the bundled_backend dir on sys.path.
import roborock_local_server.backend # noqa: F401
from import_web_inventory_from_cloud import ( # noqa: E402
_numeric_product_ids_by_model,
_plugin_product_ids,
)

A296_STRING_ID = "3PlD6TMLNY5LwI8kwxntCL"


def _home_data(products: list[tuple[object, str]], device_product_ids: list) -> SimpleNamespace:
return SimpleNamespace(
products=[SimpleNamespace(id=pid, model=model) for pid, model in products],
devices=[SimpleNamespace(product_id=pid) for pid in device_product_ids],
received_devices=[],
)


def test_string_product_ids_map_to_the_numeric_id_of_their_model() -> None:
# A Saros 20 Neo (a296) is listed with a string id only. The catalog answers a string with
# code 1002 "parameter error", so it has to be sent as the model's numeric id.
home_data = _home_data([(A296_STRING_ID, "roborock.vacuum.a296")], [A296_STRING_ID])
assert _plugin_product_ids(home_data, {"roborock.vacuum.a296": 4242}) == [4242]


def test_unmapped_string_ids_are_left_out() -> None:
home_data = _home_data([(A296_STRING_ID, "roborock.vacuum.a296")], [A296_STRING_ID])
assert _plugin_product_ids(home_data) == []
assert _plugin_product_ids(home_data, {"roborock.vacuum.other": 7}) == []


def test_numeric_ids_are_kept_and_sorted() -> None:
home_data = _home_data([(110, "roborock.vacuum.a87"), ("abc", "roborock.vacuum.a226")], ["20", None, "", "0"])
assert _plugin_product_ids(home_data, {"roborock.vacuum.a226": 199}) == [20, 110, 199]


def test_numeric_ids_are_read_from_the_product_answer_wherever_they_are_nested() -> None:
# Shape of the cloud's GET /api/v5/product answer is undocumented; issue #221 shows
# {"id": 199, "model": "roborock.vacuum.a226", "packagename": "com.roborock.tanos"} in it.
answer = {
"categoryDetailList": [
{"productList": [{"id": 199, "model": "roborock.vacuum.a226", "packagename": "com.roborock.tanos"}]},
{"productList": [{"id": "4242", "model": "roborock.vacuum.a296"}, {"id": "x", "model": "bad"}]},
]
}
assert _numeric_product_ids_by_model(answer) == {
"roborock.vacuum.a226": 199,
"roborock.vacuum.a296": 4242,
}
assert _numeric_product_ids_by_model(None) == {}