Skip to content
Merged
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 @@ -109,7 +109,9 @@
from .routes.auth.login import match_login_sms_code_send as _match_login_sms_code_send_v1
from .routes.user.devices.detail import build as _build_get_device
from .routes.user.devices.detail import build_extra as _build_get_device_extra
from .routes.user.devices.detail import build_put as _build_put_device
from .routes.user.devices.detail import match as _match_get_device
from .routes.user.devices.detail import match_put as _match_put_device
from .routes.user.devices.detail import match_extra as _match_get_device_extra
from .routes.user.deviceshare import build_received_devices as _build_get_received_devices
from .routes.user.deviceshare import build_rooms as _build_get_shared_device_rooms
Expand Down Expand Up @@ -596,6 +598,7 @@ def default_endpoint_rules() -> Sequence[EndpointRule]:
EndpointRule("put_scene_validity", _match_put_scene_validity, _build_put_scene_validity),
EndpointRule("execute_scene", _match_execute_scene, _build_execute_scene),
EndpointRule("delete_scene", _match_delete_scene, _build_delete_scene),
EndpointRule("put_device", _match_put_device, _build_put_device),
EndpointRule("get_device", _match_get_device, _build_get_device),
EndpointRule("get_device_extra", _match_get_device_extra, _build_get_device_extra),
EndpointRule("get_schedules", _match_get_schedules, _build_get_schedules),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from shared.context import ServerContext
from shared.http_helpers import wrap_response

from .service import device_detail_payload
from .service import device_detail_payload, parse_device_name, parse_strict_bool, store_device_settings


def _device_id_from_path(clean_path: str) -> str:
Expand All @@ -33,6 +33,38 @@ def build(
return wrap_response(device_detail_payload(ctx, device_id))


def match_put(path: str, method: str = "GET") -> bool:
return method.upper() == "PUT" and match(path)


def _first_value(body_params: dict[str, list[str]], key: str) -> str | None:
values = body_params.get(key) or []
return str(values[0]) if values else None


def build_put(
ctx: ServerContext,
_query_params: dict[str, list[str]],
body_params: dict[str, list[str]],
clean_path: str,
) -> dict[str, Any]:
device_id = _device_id_from_path(clean_path)
updates: dict[str, Any] = {}
raw_name = _first_value(body_params, "name")
if raw_name is not None:
name = parse_device_name(raw_name)
if name is not None:
updates["name"] = name
raw_switch = _first_value(body_params, "silentOtaSwitch")
if raw_switch is not None:
switch = parse_strict_bool(raw_switch)
if switch is not None:
updates["silentOtaSwitch"] = switch
if updates:
store_device_settings(ctx, device_id, updates)
return wrap_response(device_detail_payload(ctx, device_id))


def match_extra(path: str) -> bool:
clean = path.rstrip("/")
return bool(re.fullmatch(r"/user/devices/[^/]+/extra", clean))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,83 @@
from shared.constants import DEFAULT_PRODUCT_SCHEMA, DEFAULT_TIMEZONE
from shared.context import ServerContext
from shared.data_helpers import as_bool, as_int, default_product_name, get_value
from shared.inventory_io import WEB_API_INVENTORY_FILE, load_inventory
from shared.inventory_io import (
DEVICE_OVERRIDES_KEY,
WEB_API_INVENTORY_FILE,
device_overrides,
inventory_mutation,
load_inventory,
write_inventory,
)
_DEFAULT_DEVICE_NAME = "Vacuum 1"
_MAX_DEVICE_NAME_LENGTH = 64
_DEVICE_COLLECTIONS = ("devices", "receivedDevices", "received_devices")


def _apply_device_overrides(ctx: ServerContext, home_data: dict[str, Any]) -> dict[str, Any]:
overrides = device_overrides(load_inventory(ctx))
if not overrides:
return home_data
out = dict(home_data)
for key in _DEVICE_COLLECTIONS:
devices = home_data.get(key)
if not isinstance(devices, list):
continue
updated: list[Any] = []
for device in devices:
override = overrides.get(str(get_value(device, "duid", "did", default="")).strip()) if isinstance(device, dict) else None
if override:
device = {**device, **override}
updated.append(device)
out[key] = updated
return out


def parse_device_name(value: Any) -> str | None:
name = str(value or "").strip()
if not name or len(name) > _MAX_DEVICE_NAME_LENGTH:
return None
return name


def parse_strict_bool(value: Any) -> bool | None:
text = str(value or "").strip().lower()
if text == "true":
return True
if text == "false":
return False
return None


def _device_known(ctx: ServerContext, device_id: str) -> bool:
if not device_id:
return False
inventory = load_inventory(ctx)
if _find_device_record((_load_cloud_home_data(ctx), inventory), device_id):
return True
return any(
str(get_value(item, "duid", "did", default="")).strip() == device_id
for key in ("devices", "receivedDevices")
for item in (_home_data(ctx).get(key) or [])
if isinstance(item, dict)
)


@inventory_mutation
def store_device_settings(ctx: ServerContext, device_id: str, updates: dict[str, Any]) -> bool:
"""Persist local name/silentOtaSwitch overrides for a known device; False if unknown or unwritable."""
if not updates or not _device_known(ctx, device_id):
return False
inventory = load_inventory(ctx)
stored = device_overrides(inventory)
stored[device_id] = {**stored.get(device_id, {}), **updates}
inventory[DEVICE_OVERRIDES_KEY] = stored
# Also mirror onto inventory records so readers of the raw inventory see the change.
for key in ("devices", "received_devices", "receivedDevices"):
for item in inventory.get(key) or []:
if isinstance(item, dict) and str(get_value(item, "duid", "did", default="")).strip() == device_id:
item.update(updates)
return write_inventory(ctx, inventory)


def _has_value(value: Any) -> bool:
Expand Down Expand Up @@ -314,7 +389,7 @@ def _apply_runtime_online_status(ctx: ServerContext, home_data: dict[str, Any])
def enrich_home_data_with_cloud_snapshot(ctx: ServerContext, home_data: dict[str, Any]) -> dict[str, Any]:
cloud_home_data = _load_cloud_home_data(ctx) or {}
if not cloud_home_data:
return home_data
return _apply_device_overrides(ctx, home_data)

enriched_home = dict(home_data)

Expand Down Expand Up @@ -389,7 +464,7 @@ def enrich_home_data_with_cloud_snapshot(ctx: ServerContext, home_data: dict[str
if enriched_devices:
enriched_home[collection_key] = enriched_devices

return enriched_home
return _apply_device_overrides(ctx, enriched_home)


def _home_data(ctx: ServerContext) -> dict[str, Any]:
Expand Down Expand Up @@ -427,7 +502,7 @@ def _home_data(ctx: ServerContext) -> dict[str, Any]:

home["id"] = resolve_home_id(home, cloud_home_data, default=0)
home["name"] = str(get_value(home, "name", "home_name", default="Local Home"))
return _apply_runtime_online_status(ctx, home)
return _apply_runtime_online_status(ctx, _apply_device_overrides(ctx, home))


def device_detail_payload(ctx: ServerContext, device_id: str) -> dict[str, Any]:
Expand All @@ -449,6 +524,7 @@ def device_detail_payload(ctx: ServerContext, device_id: str) -> dict[str, Any]:
{},
)
raw_device = _find_device_record((cloud_home_data, inventory), device_id)
raw_device.update(device_overrides(inventory).get(device_id, {}))

product_id = str(
get_value(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,15 @@ def _merge_existing_inventory_mutations(
merged["home_scenes"] = home_scenes

merged["scene_order"] = _merge_scene_order(merged.get("scene_order"), existing_inventory.get("scene_order"))

overrides = existing_inventory.get("device_overrides")
if isinstance(overrides, dict) and overrides:
merged["device_overrides"] = overrides
for key in ("devices", "received_devices", "receivedDevices"):
for item in merged.get(key) or []:
override = overrides.get(str(item.get("duid") or "").strip()) if isinstance(item, dict) else None
if isinstance(override, dict):
item.update(override)
return merged


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ def handle_request(self, request: dict[str, Any]) -> HandlerResult | None:
def _parse_fan_power(params: Any) -> int | None:
if isinstance(params, int):
return params
if isinstance(params, dict):
# set_clean_motor_mode sends [{"fan_power": 102, "water_box_mode": 202, ...}].
value = params.get("fan_power")
return value if isinstance(value, int) and not isinstance(value, bool) else None
if isinstance(params, list) and params:
first = params[0]
if isinstance(first, dict):
return _parse_fan_power(first)
if isinstance(first, int):
return first
if isinstance(first, str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .context import ServerContext

WEB_API_INVENTORY_FILE = "web_api_inventory.json"
DEVICE_OVERRIDES_KEY = "device_overrides"
_LOGGER = logging.getLogger(__name__)
_LOCKS: dict[Path, Any] = {}
_LOCKS_GUARD = threading.Lock()
Expand Down Expand Up @@ -82,3 +83,11 @@ def write_inventory(ctx: ServerContext, inventory: dict[str, Any]) -> bool:
_LOGGER.exception("Unable to persist inventory")
return False
return True


def device_overrides(inventory: dict[str, Any]) -> dict[str, dict[str, Any]]:
"""Return the local per-duid device overrides (name, silentOtaSwitch) stored in the inventory."""
value = inventory.get(DEVICE_OVERRIDES_KEY)
if not isinstance(value, dict):
return {}
return {str(duid): dict(item) for duid, item in value.items() if isinstance(item, dict)}
1 change: 1 addition & 0 deletions src/roborock_local_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ def _is_public_protocol_path(cls, clean_path: str) -> bool:
"/api/v1/appplugin",
"/api/v1/plugins",
"/api/v4/agreement/latest",
"/api/v4/app/agreement/latest",
}

@classmethod
Expand Down
Loading
Loading