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
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Home card specs per product category.

The Android app only shows the scene quick-start tiles of a device when its
category carries a card spec (RRDeviceBeanV2.supportScene() returns false when
CategoryEntity.cardSpec is null). The app reads the JSON field ``cardspec`` of
the category as a string and decodes it as CardSpecV2Data, i.e. the spec
wrapped in ``{"data": {...}}``. The Roborock cloud sends it the same way.

The vacuum spec follows the cloud's /api/v5/product answer (EU, Nov 2026) and
the template bundled with the app (assets/cardSpec/robot.vacuum.cleaner.json).
The state and offline codes that only the bundled template has are kept.

This spec is only the fallback: when the cloud snapshot taken at import holds
the category's own ``cardspec`` (all 22 languages), product_service sends that
instead. The labels here are English only.
"""

from __future__ import annotations

import json
from typing import Any


def _state(label: str, *codes: int) -> dict[str, Any]:
return {"desc": {"en": label}, "value": list(codes)}


_ROBOT_VACUUM_CARD_SPEC: dict[str, Any] = {
"state": {
"dps": 121,
"desc": {"en": "State"},
"value": [
_state("Sleeping", 2),
_state("Ready", 3),
_state("Cleaning", 5, 11, 17, 18, 102, 103, 104),
_state("Returning to Dock", 6),
_state("Remote Control", 7, 39),
_state("Charging", 8),
_state("Charging Error", 9),
_state("Pause", 10),
_state("Error", 12),
_state("Washing the mop", 13),
_state("Updating", 14),
_state("Returning to Dock", 15),
_state("Pin n Go", 16),
_state("Emptying", 22),
_state("Washing the mop", 23),
_state("Going to wash the mop", 26),
_state("In call", 28),
_state("Mapping", 29),
_state("On cruise", 32),
_state("Reinstalling mop cloth mounts", 33),
_state("Detaching mop cloth mounts", 34),
_state("Demo Mode", 36, 37),
_state("Housework in progress", 38),
_state("Emergency Stop", 40),
_state("Positioning", 101),
_state("Charge Pending", 108),
],
},
"extra": {"dps": 128},
"battery": {"dps": 122},
"offline": {
"dps": 135,
"desc": {"en": "Offline State"},
"value": [
_state("Powered Off Manually", 1),
# The cloud lists only 34, 51 and 103; the app's bundled template
# also maps 2, 3 and 4 to these labels.
_state("Sleep & Powered Off", 2, 34),
_state("Low Battery & Powered Off", 3, 51),
_state("Updating & Restarting", 4, 103),
],
},
"offpeak": {"dps": 133},
}

CATEGORY_CARD_SPECS: dict[str, dict[str, Any]] = {
"robot.vacuum.cleaner": _ROBOT_VACUUM_CARD_SPEC,
}


def card_spec_for_category(category_name: str) -> str | None:
"""Return the category's ``cardspec`` value as the app expects it.

That is a JSON string holding ``{"data": <spec>}``, or None when the
category has no known spec.
"""
spec = CATEGORY_CARD_SPECS.get(category_name)
if spec is None:
return None
return json.dumps({"data": spec}, ensure_ascii=False, separators=(",", ":"))
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import json
from typing import Any

from shared.constants import MODEL_PRODUCT_ID_OVERRIDES
Expand All @@ -9,13 +10,76 @@
from ..plugin.common import snapshot_plugin_catalog_data

from ..user.homes.service import home_payload
from .card_spec import card_spec_for_category

# Fallback for products the cloud snapshot does not list. Values as the cloud
# sends them for a vacuum (a307): forceShow gates the scene tiles,
# requirePlugin/pluginLevel are only read when a scene entry opens the device
# plugin.
_FALLBACK_SCENE_TAG: dict[str, Any] = {
"name": "scene_2.0",
"requirePlugin": True,
"pluginLevel": 3000,
"forceShow": True,
}
# Only vacuums get the fallback tag: the tiles also need the category card
# spec, which we only have for vacuums, and the tag values were only seen on
# a vacuum. Mowers, washers etc. get it only when the cloud sends it.
_FALLBACK_SCENE_TAG_CATEGORIES = frozenset({"robot.vacuum.cleaner"})


def _cloud_card_spec(value: Any) -> str | None:
"""The category's cloud ``cardspec`` as the app reads it, or None if empty."""
if isinstance(value, str):
return value if value.strip() else None
if isinstance(value, dict) and value:
wrapped = value if set(value) == {"data"} else {"data": value}
return json.dumps(wrapped, ensure_ascii=False, separators=(",", ":"))
return None


def _cloud_product_details(
cloud_products: Any,
) -> tuple[dict[str, list[dict[str, Any]]], dict[str, str]]:
"""Map model -> productTags and category code -> cardspec from a cloud
/api/v5/product payload (the snapshot taken at import)."""
tags_by_model: dict[str, list[dict[str, Any]]] = {}
card_specs: dict[str, str] = {}
category_details = cloud_products.get("categoryDetailList") if isinstance(cloud_products, dict) else None
for category_detail in category_details or []:
if not isinstance(category_detail, dict):
continue
category = category_detail.get("category")
if isinstance(category, dict):
code = str(category.get("code") or "").strip()
card_spec = _cloud_card_spec(category.get("cardspec"))
if code and card_spec is not None:
card_specs.setdefault(code, card_spec)
for product in category_detail.get("productList") or []:
if not isinstance(product, dict):
continue
model = str(product.get("model") or "").strip().lower()
tags = product.get("productTags")
if model and isinstance(tags, list):
tags_by_model[model] = [dict(tag) for tag in tags if isinstance(tag, dict)]
return tags_by_model, card_specs


def _fallback_product_tags(category_name: str) -> list[dict[str, Any]]:
if category_name in _FALLBACK_SCENE_TAG_CATEGORIES:
return [dict(_FALLBACK_SCENE_TAG)]
return []


def build_product_response(ctx: ServerContext) -> dict[str, Any]:
home_data = home_payload(ctx)
products_value = home_data.get("products")
products = products_value if isinstance(products_value, list) else []
cloud_product_ids = model_product_ids(snapshot_plugin_catalog_data(ctx, "product"))
cloud_products = snapshot_plugin_catalog_data(ctx, "product")
cloud_product_ids = model_product_ids(cloud_products)
# The cloud's own productTags and category cardspec (22 languages) win;
# the built-in values are only a fallback.
cloud_product_tags, cloud_card_specs = _cloud_product_details(cloud_products)
categories: dict[str, dict[str, Any]] = {}
for product in products:
if not isinstance(product, dict):
Expand All @@ -24,15 +88,21 @@ def build_product_response(ctx: ServerContext) -> dict[str, Any]:
category_name = str(product.get("category") or "robot.vacuum.cleaner")
if category_name not in categories:
category_id = len(categories) + 1
category_entry: dict[str, Any] = {
"id": category_id,
# CategoryEntity.code becomes Product.code in the app, which
# the device plugin receives as deviceCategory.
"code": category_name,
"displayName": category_name,
"iconUrl": "",
}
card_spec = cloud_card_specs.get(category_name) or card_spec_for_category(category_name)
if card_spec is not None:
# Required for the scene tiles. Like the cloud, a JSON string
# with the spec wrapped in {"data": ...}.
category_entry["cardspec"] = card_spec
categories[category_name] = {
"category": {
"id": category_id,
# CategoryEntity.code becomes Product.code in the app, which
# the device plugin receives as deviceCategory.
"code": category_name,
"displayName": category_name,
"iconUrl": "",
},
"category": category_entry,
"productList": [],
}
model = str(product.get("model") or "roborock.vacuum.a117")
Expand All @@ -59,6 +129,14 @@ def build_product_response(ctx: ServerContext) -> dict[str, Any]:
# Read by PluginSDKModule.getProductAgreements; a missing field is
# null and the device plugin's privacy check never completes.
"agreements": [],
# ProductEntity.sceneProduct() looks up the "scene_2.0" tag; without
# it the scene tiles stay hidden. The app builds productTagsMap from
# this list itself.
"productTags": (
cloud_product_tags[model_key]
if model_key in cloud_product_tags
else _fallback_product_tags(category_name)
),
}
icon_url = product.get("iconUrl")
if isinstance(icon_url, str) and icon_url:
Expand Down
Loading