From 30fa8a397dc5e63aab3dbc5872fdc0c65a9dca05 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:38:22 -0400 Subject: [PATCH 01/27] Let the routine runner start a job without a scene Split _run_scene so a scheduled job can run the same step executor without syncing scene IDs to the robot, and add start_job(), which refuses to interrupt a routine that is already running on the device. get_routine_runner() gives other callers the per-context instance. Co-Authored-By: Claude Sonnet 5.5 (cherry picked from commit c65764810057f9491d35287e7b707e622eb19405) --- .../bundled_backend/shared/routine_runner.py | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/roborock_local_server/bundled_backend/shared/routine_runner.py b/src/roborock_local_server/bundled_backend/shared/routine_runner.py index c72f820b..bae65aa4 100644 --- a/src/roborock_local_server/bundled_backend/shared/routine_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/routine_runner.py @@ -822,6 +822,33 @@ def start_scene(self, scene: dict[str, Any]) -> dict[str, Any]: "stepCount": len(steps), } + def start_job( + self, + *, + device_id: str, + job_id: int, + name: str, + steps: list[RoutineStep], + ) -> dict[str, Any]: + """Run a scheduled job's steps; never interrupts a routine already running.""" + existing = self._tasks_by_device.get(device_id) + if existing is not None and not existing.task.done(): + return { + "accepted": False, + "status": "routine_in_progress", + "jobId": job_id, + "deviceId": device_id, + "activeSceneName": existing.scene_name, + } + scene = {"id": job_id, "name": name, "device_id": device_id} + task = asyncio.get_running_loop().create_task( + self._run_steps(scene=scene, steps=steps, sync_scene_tids=False), + name=f"routine-job-{job_id}-{device_id}", + ) + self._tasks_by_device[device_id] = _ActiveRoutine(task=task, scene_id=job_id, scene_name=name) + task.add_done_callback(lambda finished: self._on_scene_done(device_id, finished)) + return {"accepted": True, "status": "started", "jobId": job_id, "deviceId": device_id, "stepCount": len(steps)} + def _on_scene_done(self, device_id: str, task: asyncio.Task[None]) -> None: current = self._tasks_by_device.get(device_id) if current is not None and current.task is task: @@ -988,6 +1015,15 @@ async def _send_step_command( raise async def _run_scene(self, *, scene: dict[str, Any], steps: list[RoutineStep]) -> None: + await self._run_steps(scene=scene, steps=steps, sync_scene_tids=True) + + async def _run_steps( + self, + *, + scene: dict[str, Any], + steps: list[RoutineStep], + sync_scene_tids: bool, + ) -> None: device_id = scene_device_id(scene) device = self._device_record(device_id) logger = logging.LoggerAdapter( @@ -1009,7 +1045,8 @@ async def _run_scene(self, *, scene: dict[str, Any], steps: list[RoutineStep]) - client = _RoutineMqttClient(self._context, device, logger) await client.connect() try: - await self._sync_scene_tids(client=client, scene=scene, device_id=device_id, logger=logger) + if sync_scene_tids: + await self._sync_scene_tids(client=client, scene=scene, device_id=device_id, logger=logger) for step_index, step in enumerate(steps): commands = commands_for_step(step) waits_for_step_complete = RoborockDataProtocol.TASK_COMPLETE.value in step.finish_dp_ids @@ -1036,3 +1073,11 @@ async def _run_scene(self, *, scene: dict[str, Any], steps: list[RoutineStep]) - await client.close() logger.info("Completed routine scene=%s device=%s", _scene_name(scene), device_id) + + +def get_routine_runner(context: ServerContext) -> RoutineRunner: + runner = getattr(context, "_routine_runner", None) + if runner is None: + runner = RoutineRunner(context) + setattr(context, "_routine_runner", runner) + return runner From 487ccb1b22df31e21e26da93556026bd606a544f Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:38:22 -0400 Subject: [PATCH 02/27] Add a local executor for cloud-style schedules Roborock's cloud fires server_scheduled_start jobs at nextFireTime. With the vacuum pointed at this server nothing did, so imported schedules never ran. ScheduleRunner evaluates each enabled job's cron in its own timezone and hands due jobs to the routine runner, carrying fan_power, water_box_mode and mop_mode so vac-only and vac+mop schedules behave as configured. One-shot jobs disable themselves after firing. Co-Authored-By: Claude Sonnet 5.5 (cherry picked from commit b5203b2ee58a46f0f41fe1f0f9e0f323b1fc206c) --- .../bundled_backend/shared/schedule_runner.py | 313 ++++++++++++++++++ tests/test_schedule_runner.py | 160 +++++++++ 2 files changed, 473 insertions(+) create mode 100644 src/roborock_local_server/bundled_backend/shared/schedule_runner.py create mode 100644 tests/test_schedule_runner.py diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py new file mode 100644 index 00000000..c70fd3da --- /dev/null +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -0,0 +1,313 @@ +"""Local executor for cloud-style schedules (``server_scheduled_start`` jobs). + +Roborock's cloud fires these at ``nextFireTime``. With the vacuum pointed at this +server, nothing else would, so the server evaluates the stored cron entries +itself and hands each due job to the routine runner. +""" + +from __future__ import annotations + +import asyncio +from dataclasses import dataclass +from datetime import date, datetime, timedelta, timezone, tzinfo +import json +import logging +from typing import TYPE_CHECKING, Any +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError + +from .data_helpers import as_bool +from .inventory_io import inventory_mutation, load_inventory, write_inventory +from .routine_runner import RoutineExecutionError, RoutineStep, get_routine_runner + +if TYPE_CHECKING: + from .context import ServerContext + +_LOGGER = logging.getLogger(__name__) + +TICK_SECONDS = 20 +_SUPPORTED_JOB_METHOD = "server_scheduled_start" +_CARRIED_ENTRY_KEYS = ("repeat", "fan_power", "water_box_mode", "mop_mode", "mop_template_id") + + +class CronError(ValueError): + """Raised for a cron expression this scheduler cannot evaluate.""" + + +@dataclass(frozen=True) +class CronSpec: + minutes: tuple[int, ...] + hours: tuple[int, ...] + days_of_month: frozenset[int] + months: frozenset[int] + days_of_week: frozenset[int] + dom_restricted: bool + dow_restricted: bool + + def day_matches(self, day: date) -> bool: + if day.month not in self.months: + return False + dom_ok = day.day in self.days_of_month + # cron weekday 0 = Sunday; python's isoweekday() has Sunday as 7. + dow_ok = (day.isoweekday() % 7) in self.days_of_week + if self.dom_restricted and self.dow_restricted: + return dom_ok or dow_ok + return dom_ok and dow_ok + + +def _parse_field(text: str, low: int, high: int, *, name: str) -> tuple[frozenset[int], bool]: + text = text.strip() + if text in ("*", "?"): + return frozenset(range(low, high + 1)), False + values: set[int] = set() + for part in text.split(","): + part = part.strip() + if not part: + raise CronError(f"empty {name} entry in {text!r}") + step = 1 + if "/" in part: + part, _, step_text = part.partition("/") + try: + step = int(step_text) + except ValueError as exc: + raise CronError(f"bad {name} step {step_text!r}") from exc + if step <= 0: + raise CronError(f"{name} step must be positive") + if part in ("*", "?", ""): + start, end = low, high + elif "-" in part: + first, _, last = part.partition("-") + try: + start, end = int(first), int(last) + except ValueError as exc: + raise CronError(f"bad {name} range {part!r}") from exc + else: + try: + start = int(part) + except ValueError as exc: + raise CronError(f"bad {name} value {part!r}") from exc + end = high if step != 1 else start + if start < low or end > high or start > end: + raise CronError(f"{name} value out of range in {text!r}") + values.update(range(start, end + 1, step)) + return frozenset(values), True + + +def parse_cron(expression: str) -> CronSpec: + fields = str(expression or "").split() + if len(fields) != 5: + raise CronError(f"expected 5 cron fields, got {len(fields)}: {expression!r}") + minutes, _ = _parse_field(fields[0], 0, 59, name="minute") + hours, _ = _parse_field(fields[1], 0, 23, name="hour") + dom, dom_restricted = _parse_field(fields[2], 1, 31, name="day-of-month") + months, _ = _parse_field(fields[3], 1, 12, name="month") + raw_dow, dow_restricted = _parse_field(fields[4], 0, 7, name="day-of-week") + dow = frozenset(0 if value == 7 else value for value in raw_dow) + return CronSpec( + minutes=tuple(sorted(minutes)), + hours=tuple(sorted(hours)), + days_of_month=dom, + months=months, + days_of_week=dow, + dom_restricted=dom_restricted, + dow_restricted=dow_restricted, + ) + + +def next_fire(spec: CronSpec, after: datetime) -> datetime | None: + """First fire instant strictly after ``after`` (an aware datetime), in its timezone.""" + zone = after.tzinfo + if zone is None: + raise ValueError("after must be timezone-aware") + start = after.replace(second=0, microsecond=0) + timedelta(minutes=1) + start_utc = start.astimezone(timezone.utc) + first_day = start.date() + for offset in range(0, 800): + day = first_day + timedelta(days=offset) + if not spec.day_matches(day): + continue + for hour in spec.hours: + for minute in spec.minutes: + candidate = datetime(day.year, day.month, day.day, hour, minute, tzinfo=zone) + if candidate.astimezone(timezone.utc) >= start_utc: + return candidate + return None + + +def _zone(name: str, fallback: str) -> tzinfo: + for candidate in (name, fallback): + candidate = str(candidate or "").strip() + if not candidate: + continue + try: + return ZoneInfo(candidate) + except (ZoneInfoNotFoundError, ValueError): + continue + return timezone.utc + + +def _segment_ids(value: Any) -> list[int]: + if isinstance(value, str): + items: list[Any] = [part for part in value.replace(";", ",").split(",") if part.strip()] + elif isinstance(value, (list, tuple)): + items = list(value) + elif value is None: + items = [] + else: + items = [value] + segments: list[int] = [] + for item in items: + try: + number = int(str(item).strip()) + except ValueError: + continue + if number > 0: + segments.append(number) + return segments + + +def job_param(job: dict[str, Any]) -> dict[str, Any]: + param = job.get("param") + if isinstance(param, str): + try: + param = json.loads(param) + except json.JSONDecodeError as exc: + raise RoutineExecutionError("schedule param is not valid JSON") from exc + if not isinstance(param, dict): + raise RoutineExecutionError("schedule is missing its param payload") + return param + + +def steps_for_job(job: dict[str, Any]) -> list[RoutineStep]: + param = job_param(job) + method = str(param.get("method") or "").strip() + if method != _SUPPORTED_JOB_METHOD: + raise RoutineExecutionError(f"unsupported schedule method {method!r}") + params = param.get("params") + entry = params[0] if isinstance(params, list) and params else params + if not isinstance(entry, dict): + raise RoutineExecutionError("schedule params must contain one settings object") + + settings = {key: entry[key] for key in _CARRIED_ENTRY_KEYS if key in entry} + name = str(entry.get("name") or job.get("name") or "").strip() + if not name or name.isdigit(): + name = f"Schedule {job.get('id', '')}".strip() + segments = _segment_ids(entry.get("segments")) + if segments: + return [ + RoutineStep( + step_id=1, + name=name, + method="do_scenes_segments", + params={"data": [{**settings, "segs": segments}]}, + finish_dp_ids=(), + ) + ] + return [ + RoutineStep( + step_id=1, + name=name, + method="do_scenes_app_start", + params=[settings], + finish_dp_ids=(), + ) + ] + + +def next_fire_time_utc(job: dict[str, Any], *, default_timezone: str = "", now: datetime | None = None) -> datetime | None: + if not as_bool(job.get("enabled", True), True): + return None + try: + spec = parse_cron(str(job.get("cron") or "")) + except CronError: + return None + zone = _zone(str(job.get("timeZoneId") or ""), default_timezone) + current = (now or datetime.now(timezone.utc)).astimezone(zone) + upcoming = next_fire(spec, current) + return upcoming.astimezone(timezone.utc) if upcoming is not None else None + + +@inventory_mutation +def _disable_job(ctx: ServerContext, device_id: str, job_id: Any) -> None: + inventory = load_inventory(ctx) + schedules = inventory.get("schedules") + jobs = schedules.get(device_id) if isinstance(schedules, dict) else None + if not isinstance(jobs, list): + return + for job in jobs: + if isinstance(job, dict) and str(job.get("id")) == str(job_id): + job["enabled"] = False + write_inventory(ctx, inventory) + + +class ScheduleRunner: + def __init__(self, context: ServerContext, *, default_timezone: str = "") -> None: + self._context = context + self._default_timezone = default_timezone + self._last_tick: datetime | None = None + self._logger = context.loggers.get("api") or context.loggers.get("unknown") or _LOGGER + + def due_jobs(self, since: datetime, until: datetime) -> list[tuple[str, dict[str, Any]]]: + inventory = load_inventory(self._context) + schedules = inventory.get("schedules") + if not isinstance(schedules, dict): + return [] + due: list[tuple[str, dict[str, Any]]] = [] + for device_id, jobs in schedules.items(): + if not isinstance(jobs, list): + continue + for job in jobs: + if not isinstance(job, dict) or not as_bool(job.get("enabled", True), True): + continue + try: + spec = parse_cron(str(job.get("cron") or "")) + except CronError as exc: + self._logger.warning("Skipping schedule %s on %s: %s", job.get("id"), device_id, exc) + continue + zone = _zone(str(job.get("timeZoneId") or ""), self._default_timezone) + upcoming = next_fire(spec, since.astimezone(zone)) + if upcoming is not None and upcoming.astimezone(timezone.utc) <= until: + due.append((str(device_id), job)) + return due + + def tick(self, now: datetime | None = None) -> list[dict[str, Any]]: + """Fire anything that came due since the previous tick. Needs a running event loop.""" + current = (now or datetime.now(timezone.utc)).astimezone(timezone.utc) + previous = self._last_tick + self._last_tick = current + if previous is None: + return [] + results: list[dict[str, Any]] = [] + runner = get_routine_runner(self._context) + for device_id, job in self.due_jobs(previous, current): + job_id = job.get("id") + try: + steps = steps_for_job(job) + result = runner.start_job( + device_id=device_id, + job_id=int(job_id) if str(job_id).isdigit() else 0, + name=steps[0].name, + steps=steps, + ) + except (RoutineExecutionError, ValueError) as exc: + self._logger.warning("Schedule %s on %s not started: %s", job_id, device_id, exc) + result = {"accepted": False, "status": "error", "error": str(exc), "deviceId": device_id} + else: + self._logger.info("Schedule %s on %s fired: %s", job_id, device_id, result.get("status")) + if result.get("accepted") and not as_bool(job.get("repeated", True), True): + _disable_job(self._context, device_id, job_id) + results.append({"jobId": job_id, **result}) + return results + + async def run(self, stop_event: asyncio.Event, *, tick_seconds: float = TICK_SECONDS) -> None: + self.tick() + while not stop_event.is_set(): + try: + await asyncio.wait_for(stop_event.wait(), timeout=tick_seconds) + except TimeoutError: + pass + if stop_event.is_set(): + return + try: + self.tick() + except Exception as exc: # noqa: BLE001 + self._logger.warning("Schedule tick failed: %s", exc) diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py new file mode 100644 index 00000000..8769898b --- /dev/null +++ b/tests/test_schedule_runner.py @@ -0,0 +1,160 @@ +import json +from datetime import datetime, timedelta, timezone +from types import SimpleNamespace + +import pytest + +import roborock_local_server.backend # noqa: F401 (puts the bundled `shared` package on sys.path) +from shared import schedule_runner as sr +from shared.routine_runner import RoutineExecutionError + +NEW_YORK = "America/New_York" + + +def _job(**overrides): + job = { + "id": 3549431, + "cron": "00 11 ? * 1,3,5", + "timeZoneId": NEW_YORK, + "repeated": True, + "enabled": True, + "param": { + "id": 1, + "method": "server_scheduled_start", + "params": [ + { + "repeat": 1, + "water_box_mode": 203, + "segments": "0", + "fan_power": 104, + "mop_mode": 300, + "clean_mop": 1, + "map_index": -1, + "name": "1759965417864", + } + ], + }, + } + job.update(overrides) + return job + + +def _utc(*args): + return datetime(*args, tzinfo=timezone.utc) + + +def test_next_fire_matches_the_cloud_reported_fire_times(): + # Values taken from a real cloud snapshot: Monday 2026-09-28 for MWF 11:00 New York. + spec = sr.parse_cron("00 11 ? * 1,3,5") + zone = sr._zone(NEW_YORK, "") + after = _utc(2026, 9, 28, 12, 0).astimezone(zone) + assert sr.next_fire(spec, after).astimezone(timezone.utc) == _utc(2026, 9, 28, 15, 0) + + tue_thu = sr.parse_cron("00 11 ? * 2,4") + assert sr.next_fire(tue_thu, after).astimezone(timezone.utc) == _utc(2026, 9, 29, 15, 0) + + +def test_next_fire_is_strictly_after_and_skips_to_next_matching_day(): + spec = sr.parse_cron("00 11 ? * 1,3,5") + zone = sr._zone(NEW_YORK, "") + on_the_minute = _utc(2026, 9, 28, 15, 0).astimezone(zone) + assert sr.next_fire(spec, on_the_minute).astimezone(timezone.utc) == _utc(2026, 9, 30, 15, 0) + + +def test_weekend_cron_uses_sunday_zero(): + spec = sr.parse_cron("00 11 ? * 0,6") + zone = sr._zone(NEW_YORK, "") + monday = _utc(2026, 9, 28, 12, 0).astimezone(zone) + assert sr.next_fire(spec, monday).astimezone(timezone.utc) == _utc(2026, 10, 3, 15, 0) + + +@pytest.mark.parametrize("expression", ["", "* * * *", "61 * * * *", "* 25 * * *", "a b c d e", "*/0 * * * *"]) +def test_invalid_cron_is_rejected(expression): + with pytest.raises(sr.CronError): + sr.parse_cron(expression) + + +def test_whole_house_job_becomes_an_app_start_step_carrying_vac_and_mop_settings(): + steps = sr.steps_for_job(_job()) + assert len(steps) == 1 + step = steps[0] + assert step.method == "do_scenes_app_start" + assert step.params == [{"repeat": 1, "water_box_mode": 203, "fan_power": 104, "mop_mode": 300}] + assert step.name == "Schedule 3549431" + + +def test_segment_job_becomes_a_segment_clean_step(): + job = _job() + job["param"]["params"][0]["segments"] = "16,17" + step = sr.steps_for_job(job)[0] + assert step.method == "do_scenes_segments" + assert step.params["data"][0]["segs"] == [16, 17] + assert step.params["data"][0]["fan_power"] == 104 + + +def test_unsupported_method_is_refused(): + job = _job() + job["param"]["method"] = "something_else" + with pytest.raises(RoutineExecutionError): + sr.steps_for_job(job) + + +class _FakeRunner: + def __init__(self): + self.started = [] + + def start_job(self, **kwargs): + self.started.append(kwargs) + return {"accepted": True, "status": "started", "deviceId": kwargs["device_id"]} + + +def _runner_with_inventory(tmp_path, monkeypatch, jobs): + inventory = {"schedules": {"duid-1": jobs}} + (tmp_path / "web_api_inventory.json").write_text(json.dumps(inventory), encoding="utf-8") + ctx = SimpleNamespace(http_jsonl=tmp_path / "http.jsonl", loggers={}) + fake = _FakeRunner() + monkeypatch.setattr(sr, "get_routine_runner", lambda _ctx: fake) + return sr.ScheduleRunner(ctx), ctx, fake + + +def test_tick_fires_a_job_that_came_due_since_the_previous_tick(tmp_path, monkeypatch): + runner, _ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + assert runner.tick(_utc(2026, 9, 28, 14, 59, 40)) == [] # first tick only sets the baseline + results = runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert [r["status"] for r in results] == ["started"] + assert fake.started[0]["device_id"] == "duid-1" + assert fake.started[0]["steps"][0].method == "do_scenes_app_start" + + +def test_tick_does_not_fire_twice_or_when_disabled(tmp_path, monkeypatch): + runner, _ctx, fake = _runner_with_inventory( + tmp_path, monkeypatch, [_job(), _job(id=2, enabled=False)] + ) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + runner.tick(_utc(2026, 9, 28, 15, 0, 25)) + runner.tick(_utc(2026, 9, 28, 15, 0, 45) + timedelta(minutes=5)) + assert len(fake.started) == 1 + + +def test_one_shot_job_disables_itself_after_firing(tmp_path, monkeypatch): + runner, _ctx, _fake = _runner_with_inventory(tmp_path, monkeypatch, [_job(repeated=False)]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + saved = json.loads((tmp_path / "web_api_inventory.json").read_text(encoding="utf-8")) + assert saved["schedules"]["duid-1"][0]["enabled"] is False + + +@pytest.mark.parametrize("disabled_value", [False, 0, "false", "0"]) +def test_string_typed_disabled_flag_is_respected(tmp_path, monkeypatch, disabled_value): + # Imported inventory stores booleans loosely ("false", 0); "false" must not read as enabled. + runner, _ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job(enabled=disabled_value)]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert fake.started == [] + + +def test_next_fire_time_utc_is_none_when_disabled(): + now = _utc(2026, 9, 28, 12, 0) + assert sr.next_fire_time_utc(_job(), now=now) == _utc(2026, 9, 28, 15, 0) + assert sr.next_fire_time_utc(_job(enabled=False), now=now) is None From aaca2f9b5a985e265f8c04c50d9aedee86363dc1 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:38:22 -0400 Subject: [PATCH 03/27] Run the schedule loop with the server and report nextFireTime Start the scheduler alongside the other supervisor tasks and stop it on shutdown. GET /user/devices/{id}/jobs now computes nextFireTime from the cron instead of returning whatever value was captured at import time. Co-Authored-By: Claude Sonnet 5.5 (cherry picked from commit 14ddb5fcd8a7ec1b50fe3e90c8b7cc4cec5b0b97) --- .../https_server/routes/user/devices/service.py | 4 ++++ src/roborock_local_server/server.py | 14 ++++++++++++++ tests/test_schedules.py | 3 +++ 3 files changed, 21 insertions(+) diff --git a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py index aaa80517..5f4c4701 100644 --- a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py +++ b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py @@ -20,6 +20,7 @@ load_inventory, write_inventory, ) +from shared.schedule_runner import next_fire_time_utc _DEFAULT_DEVICE_NAME = "Vacuum 1" _MAX_DEVICE_NAME_LENGTH = 64 _DEVICE_COLLECTIONS = ("devices", "receivedDevices", "received_devices") @@ -728,6 +729,9 @@ def device_jobs_payload(ctx: ServerContext, device_id: str) -> list[dict[str, An payload["repeated"] = as_bool(get_value(raw, "repeated", default=True), True) payload["enabled"] = as_bool(get_value(raw, "enabled", default=True), True) payload["param"] = get_value(raw, "param", default={}) or {} + upcoming = next_fire_time_utc(payload, default_timezone=str(getattr(ctx, "timezone", "") or "")) + if upcoming is not None: + payload["nextFireTime"] = upcoming.strftime("%Y-%m-%dT%H:%M:%S.000+00:00") schedules.append(payload) return schedules diff --git a/src/roborock_local_server/server.py b/src/roborock_local_server/server.py index 99371d23..3f16ed5c 100644 --- a/src/roborock_local_server/server.py +++ b/src/roborock_local_server/server.py @@ -54,6 +54,7 @@ ) from shared.inventory_io import atomic_write_inventory, inventory_transaction from shared.protocol_auth import ProtocolAuthStore +from shared.schedule_runner import ScheduleRunner from https_server.routes.user.scene import validity as scene_validity from https_server.routes.auth.service import ( build_login_data_response, @@ -362,6 +363,7 @@ def __init__( self._mqtt_proxy: MqttTlsProxy | None = None self._http_server: ManagedFastApiServer | None = None self._renew_task: asyncio.Task[None] | None = None + self._schedule_task: asyncio.Task[None] | None = None self._stop_event = asyncio.Event() self.certificate_manager = CertificateManager(config=config, paths=paths) @@ -2037,10 +2039,22 @@ async def start(self) -> None: if self._uses_local_tls() and self.config.tls.mode == "cloudflare_acme": self._renew_task = asyncio.create_task(self._renew_loop(), name="tls-renew-loop") + schedule_runner = ScheduleRunner(self.context, default_timezone=self.config.network.timezone) + self._schedule_task = asyncio.create_task( + schedule_runner.run(self._stop_event), + name="schedule-runner", + ) + async def stop(self) -> None: self._stop_event.set() self._turn_relay.stop() self._refresh_turn_health() + if self._schedule_task is not None: + self._schedule_task.cancel() + try: + await self._schedule_task + except asyncio.CancelledError: + pass if self._renew_task is not None: self._renew_task.cancel() try: diff --git a/tests/test_schedules.py b/tests/test_schedules.py index f97b1039..1c0f26f2 100644 --- a/tests/test_schedules.py +++ b/tests/test_schedules.py @@ -104,6 +104,9 @@ def test_device_jobs_endpoint_returns_imported_schedules(tmp_path: Path) -> None payload = response.json() assert payload["success"] is True assert payload["data"] == payload["result"] + # nextFireTime is computed from the cron against "now", so check its shape, not its value. + next_fire_time = payload["result"][0].pop("nextFireTime") + assert next_fire_time.endswith("T13:03:00.000+00:00") assert payload["result"] == [ { **schedule, From 7b236557464b5ad6edf5b303cde5d66aa7996b4b Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:21:40 -0400 Subject: [PATCH 04/27] Add a schedule store and evaluate crons in the robot's timezone schedule_store holds create/update/delete for the per-device jobs kept in the inventory, so the timer RPC handler, HTTP routes and dashboard can share one writer. Crons without their own timeZoneId now evaluate in the zone the robot is told by /location (America/New_York unless configured) rather than UTC, so a 11:00 schedule fires at 11:00 local. Co-Authored-By: Claude Sonnet 5.5 (cherry picked from commit 0751a7b06b683701a3607eb143258565b548c560) --- .../bundled_backend/shared/schedule_runner.py | 22 +--- .../bundled_backend/shared/schedule_store.py | 119 ++++++++++++++++++ tests/test_schedules.py | 4 +- 3 files changed, 128 insertions(+), 17 deletions(-) create mode 100644 src/roborock_local_server/bundled_backend/shared/schedule_store.py diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index c70fd3da..33fa64ce 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -15,9 +15,11 @@ from typing import TYPE_CHECKING, Any from zoneinfo import ZoneInfo, ZoneInfoNotFoundError +from .constants import DEFAULT_TIMEZONE from .data_helpers import as_bool -from .inventory_io import inventory_mutation, load_inventory, write_inventory +from .inventory_io import load_inventory from .routine_runner import RoutineExecutionError, RoutineStep, get_routine_runner +from .schedule_store import set_enabled if TYPE_CHECKING: from .context import ServerContext @@ -134,7 +136,8 @@ def next_fire(spec: CronSpec, after: datetime) -> datetime | None: def _zone(name: str, fallback: str) -> tzinfo: - for candidate in (name, fallback): + # DEFAULT_TIMEZONE is what /location tells the robot when none is configured. + for candidate in (name, fallback, DEFAULT_TIMEZONE): candidate = str(candidate or "").strip() if not candidate: continue @@ -226,19 +229,6 @@ def next_fire_time_utc(job: dict[str, Any], *, default_timezone: str = "", now: return upcoming.astimezone(timezone.utc) if upcoming is not None else None -@inventory_mutation -def _disable_job(ctx: ServerContext, device_id: str, job_id: Any) -> None: - inventory = load_inventory(ctx) - schedules = inventory.get("schedules") - jobs = schedules.get(device_id) if isinstance(schedules, dict) else None - if not isinstance(jobs, list): - return - for job in jobs: - if isinstance(job, dict) and str(job.get("id")) == str(job_id): - job["enabled"] = False - write_inventory(ctx, inventory) - - class ScheduleRunner: def __init__(self, context: ServerContext, *, default_timezone: str = "") -> None: self._context = context @@ -294,7 +284,7 @@ def tick(self, now: datetime | None = None) -> list[dict[str, Any]]: else: self._logger.info("Schedule %s on %s fired: %s", job_id, device_id, result.get("status")) if result.get("accepted") and not as_bool(job.get("repeated", True), True): - _disable_job(self._context, device_id, job_id) + set_enabled(self._context, device_id, job_id, False) results.append({"jobId": job_id, **result}) return results diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_store.py b/src/roborock_local_server/bundled_backend/shared/schedule_store.py new file mode 100644 index 00000000..1d762fcc --- /dev/null +++ b/src/roborock_local_server/bundled_backend/shared/schedule_store.py @@ -0,0 +1,119 @@ +"""Create, update and delete the per-device schedules kept in the inventory.""" + +from __future__ import annotations + +import copy +from typing import TYPE_CHECKING, Any + +from .inventory_io import inventory_mutation, load_inventory, write_inventory + +if TYPE_CHECKING: + from .context import ServerContext + +DEFAULT_JOB_PARAM: dict[str, Any] = { + "id": 1, + "method": "server_scheduled_start", + "params": [{"repeat": 1, "segments": "0"}], +} + + +def _same_id(job: Any, job_id: Any) -> bool: + return isinstance(job, dict) and str(job.get("id")) == str(job_id) + + +def _jobs_for(inventory: dict[str, Any], duid: str, *, create: bool) -> list[Any] | None: + schedules = inventory.get("schedules") + if not isinstance(schedules, dict): + if not create: + return None + schedules = {} + inventory["schedules"] = schedules + jobs = schedules.get(duid) + if not isinstance(jobs, list): + if not create: + return None + jobs = [] + schedules[duid] = jobs + return jobs + + +def _coerce_id(job_id: Any) -> Any: + text = str(job_id).strip() + return int(text) if text.isdigit() else text + + +def list_jobs(ctx: ServerContext, duid: str) -> list[dict[str, Any]]: + jobs = _jobs_for(load_inventory(ctx), duid, create=False) or [] + return [copy.deepcopy(job) for job in jobs if isinstance(job, dict)] + + +@inventory_mutation +def upsert_job( + ctx: ServerContext, + duid: str, + *, + job_id: Any, + cron: str, + enabled: bool, + time_zone: str = "", + repeated: bool | None = None, + param: dict[str, Any] | None = None, + override_time_zone: bool = False, +) -> dict[str, Any]: + """Create the job, or update cron/enabled on an existing one while keeping its settings. + + An existing job keeps its own timezone unless ``override_time_zone`` is set. + """ + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=True) + assert jobs is not None + existing = next((job for job in jobs if _same_id(job, job_id)), None) + if existing is None: + existing = { + "id": _coerce_id(job_id), + "repeated": True if repeated is None else repeated, + "type": "clean", + "param": copy.deepcopy(param) if param is not None else copy.deepcopy(DEFAULT_JOB_PARAM), + } + if time_zone: + existing["timeZoneId"] = time_zone + jobs.append(existing) + else: + if repeated is not None: + existing["repeated"] = repeated + if param is not None: + existing["param"] = copy.deepcopy(param) + if override_time_zone and time_zone: + existing["timeZoneId"] = time_zone + existing["cron"] = cron + existing["enabled"] = enabled + write_inventory(ctx, inventory) + return copy.deepcopy(existing) + + +@inventory_mutation +def set_enabled(ctx: ServerContext, duid: str, job_id: Any, enabled: bool) -> bool: + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=False) or [] + found = False + for job in jobs: + if _same_id(job, job_id): + job["enabled"] = enabled + found = True + if found: + write_inventory(ctx, inventory) + return found + + +@inventory_mutation +def delete_job(ctx: ServerContext, duid: str, job_id: Any) -> bool: + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=False) + if not jobs: + return False + remaining = [job for job in jobs if not _same_id(job, job_id)] + if len(remaining) == len(jobs): + return False + jobs[:] = remaining + write_inventory(ctx, inventory) + return True diff --git a/tests/test_schedules.py b/tests/test_schedules.py index 1c0f26f2..e4a9ced3 100644 --- a/tests/test_schedules.py +++ b/tests/test_schedules.py @@ -105,8 +105,10 @@ def test_device_jobs_endpoint_returns_imported_schedules(tmp_path: Path) -> None assert payload["success"] is True assert payload["data"] == payload["result"] # nextFireTime is computed from the cron against "now", so check its shape, not its value. + # No timezone is configured, so the cron runs in the zone the robot is told (America/New_York): + # 13:03 local on Dec 15 is 18:03Z. next_fire_time = payload["result"][0].pop("nextFireTime") - assert next_fire_time.endswith("T13:03:00.000+00:00") + assert next_fire_time.endswith("T18:03:00.000+00:00") assert payload["result"] == [ { **schedule, From 83f6dfe323968fdd068b96918daad8b422252d01 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:21:40 -0400 Subject: [PATCH 05/27] Answer get/set/upd/del_timer from the schedule store Timer RPCs carry each schedule's vac and mop settings, and a schedule keeps the official app's link to its server_timer entry (the robot-side id in its name) so writing it back goes to the right robot entry. The four get/set/upd/del_server_timer methods are left to the robot: a robot that speaks server_timer stores those schedules itself, and answering them from this store hid the robot's real schedules and, tried once, caused data loss (the official app deletes server_timer entries it does not recognize when its schedule screen opens). A parametrized test over all four methods keeps the interceptor's method list from regressing. Co-Authored-By: Claude Sonnet 5.5 --- .../mqtt_broker_server/topic_bridge.py | 19 +- .../bundled_backend/shared/schedule_store.py | 73 +++++ .../bundled_backend/shared/timer_rpc.py | 233 ++++++++++++++++ src/roborock_local_server/server.py | 5 + tests/test_schedule_store.py | 21 ++ tests/test_timer_rpc.py | 249 ++++++++++++++++++ 6 files changed, 599 insertions(+), 1 deletion(-) create mode 100644 src/roborock_local_server/bundled_backend/shared/timer_rpc.py create mode 100644 tests/test_schedule_store.py create mode 100644 tests/test_timer_rpc.py diff --git a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py index 7bc087a0..1276126b 100644 --- a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py +++ b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py @@ -9,7 +9,7 @@ from pathlib import Path import re import time -from typing import Any +from typing import Any, Callable import aiomqtt @@ -73,7 +73,11 @@ def __init__( fixed_device_mqtt_usr: str = "", runtime_state: Any | None = None, inventory_path: Path | None = None, + request_interceptor: Callable[[str, bytes], bytes | None] | None = None, ) -> None: + # Given (duid, payload) for each rr/m request; returns an encoded reply to + # publish back to the requester instead of forwarding, or None to forward. + self._request_interceptor = request_interceptor self._host = host self._port = port self._logger = logger @@ -389,6 +393,19 @@ async def _handle_cloud_message( cloud_topic: CloudTopicKey, src_topic: str, ) -> None: + if self._request_interceptor is not None: + payload = message.payload + raw = payload if isinstance(payload, (bytes, bytearray)) else str(payload).encode() + try: + reply = self._request_interceptor(cloud_topic.duid, bytes(raw)) + except Exception as exc: # noqa: BLE001 + self._logger.warning("MQTT request interceptor failed for %s: %s", src_topic, exc) + reply = None + if reply is not None: + await client.publish(cloud_topic.topic_out, reply, qos=_extract_qos(message)) + self._logger.info("MQTT bridge answered %s locally -> %s", src_topic, cloud_topic.topic_out) + return + device_topic = self._resolve_device_for_cloud(cloud_topic) if device_topic is None: if cloud_topic.duid not in self._warned_unmapped_duids: diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_store.py b/src/roborock_local_server/bundled_backend/shared/schedule_store.py index 1d762fcc..8426e54e 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_store.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_store.py @@ -17,6 +17,79 @@ } +JOB_SETTING_KEYS = ( + "repeat", + "fan_power", + "water_box_mode", + "mop_mode", + "mop_template_id", + "segments", + "clean_mop", +) +_INT_SETTING_KEYS = frozenset(JOB_SETTING_KEYS) - {"segments"} + + +class SettingsError(ValueError): + """A schedule setting had a value this server cannot store.""" + + +def _normalize_setting(key: str, value: Any) -> Any: + if key == "segments": + items = value.replace(";", ",").split(",") if isinstance(value, str) else list(value) + try: + ids = [int(str(item).strip()) for item in items if str(item).strip()] + except ValueError as exc: + raise SettingsError(f"segments must be room ids, got {value!r}") from exc + ids = [room for room in ids if room > 0] + return ",".join(str(room) for room in ids) if ids else "0" + if isinstance(value, bool) or not isinstance(value, (int, str)): + raise SettingsError(f"{key} must be a number, got {value!r}") + try: + return int(value) + except ValueError as exc: + raise SettingsError(f"{key} must be a number, got {value!r}") from exc + + +def job_settings(job: dict[str, Any]) -> dict[str, Any]: + """The vac/mop settings a job carries, as a flat dict. + + ``name``, when the job has one, rides along too, outside ``JOB_SETTING_KEYS``: it is not a + vac/mop setting a client edits, it is the official app's own link from this job to the + robot's server_timer entry it saved alongside it (POST/PUT /user/devices/{duid}/jobs carries + it in exactly this spot, confirmed live). A client that reads the job back through get_timer + needs it to find the robot's entry to edit or delete. + """ + param = job.get("param") + params = param.get("params") if isinstance(param, dict) else None + entry = params[0] if isinstance(params, list) and params and isinstance(params[0], dict) else {} + settings = {key: entry[key] for key in JOB_SETTING_KEYS if key in entry} + name = entry.get("name") + if isinstance(name, str) and name: + settings["name"] = name + return settings + + +def merge_job_settings(param: dict[str, Any] | None, settings: dict[str, Any]) -> dict[str, Any]: + """Copy of ``param`` with ``settings`` merged in; a None value clears that setting. + + Settings not mentioned are left alone, so editing one option never resets the rest. + """ + merged = copy.deepcopy(param) if isinstance(param, dict) else copy.deepcopy(DEFAULT_JOB_PARAM) + params = merged.get("params") + if not isinstance(params, list) or not params or not isinstance(params[0], dict): + params = copy.deepcopy(DEFAULT_JOB_PARAM["params"]) + merged["params"] = params + entry = params[0] + for key, value in settings.items(): + if key not in JOB_SETTING_KEYS: + raise SettingsError(f"unknown schedule setting {key!r}") + if value is None: + entry.pop(key, None) + else: + entry[key] = _normalize_setting(key, value) + return merged + + def _same_id(job: Any, job_id: Any) -> bool: return isinstance(job, dict) and str(job.get("id")) == str(job_id) diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py new file mode 100644 index 00000000..3d93ef3c --- /dev/null +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -0,0 +1,233 @@ +"""Answer the vacuum's legacy timer RPCs (get/set/upd/del_timer) from the schedule store. + +On cloud-managed robots the schedules live server-side, not on the robot, so +clients such as LocalRock that ask the robot for its timers would otherwise see +nothing and could never change the schedules the cloud imported. The topic +bridge hands matching requests here instead of forwarding them to the robot. + +Deliberately not handled here: get/set/upd/del_server_timer, the newer sibling +protocol some firmware speaks instead of (or alongside) this one. The robot +stores those entries itself and the official app reads them back from it, so +they must always forward to the robot untouched. Answering them from this store +instead, tried once, hid the robot's real schedules from every client reading +through the server, and the official app then deleted them (it removes +server_timer entries it does not recognize when its schedule screen opens). + +What runs a server_timer schedule is the cloud's own record of it, the job the +official app also writes to /user/devices/{duid}/jobs, which ScheduleRunner +fires like any other. The robot was not seen to run them itself. +""" + +from __future__ import annotations + +import json +import logging +import time +from typing import TYPE_CHECKING, Any + +from roborock.protocol import create_mqtt_decoder, create_mqtt_encoder +from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol + +from .constants import DEFAULT_TIMEZONE +from .schedule_runner import CronError, parse_cron +from .schedule_store import ( + JOB_SETTING_KEYS, + SettingsError, + delete_job, + job_settings, + list_jobs, + merge_job_settings, + set_enabled, + upsert_job, +) + +if TYPE_CHECKING: + from .context import ServerContext + +_LOGGER = logging.getLogger(__name__) + +TIMER_METHODS = frozenset({"get_timer", "set_timer", "upd_timer", "del_timer"}) +_OK = ["ok"] + + +class TimerRequestError(ValueError): + """The request used a shape this handler cannot interpret.""" + + +def device_cron(cron: str) -> str | None: + """Cron in the robot's own 'm h dom month dow' form, with '?' written as '*'. + + A one-time schedule pins the day of the month and the month ('15 23 18 10 *'), exactly as + the robot and the official app write it (confirmed live), so it is shown like any other. + A job that pins only one of the two has no such form and is left out of get_timer rather + than shown wrong. + """ + fields = str(cron or "").split() + if len(fields) != 5: + return None + minute, hour, dom, month, dow = (field if field != "?" else "*" for field in fields) + if (dom == "*") != (month == "*"): + return None + return f"{minute} {hour} {dom} {month} {dow}" + + +def _timer_entry(job: dict[str, Any]) -> list[Any] | None: + cron = device_cron(str(job.get("cron") or "")) + if cron is None: + return None + enabled = job.get("enabled", True) + on = enabled not in (False, 0, "0", "false", "False") + return [str(job.get("id")), "on" if on else "off", [cron, ["start_clean", job_settings(job)]]] + + +def _parse_set_timer(params: Any) -> tuple[str, bool, str, dict[str, Any]]: + entry = params[0] if isinstance(params, list) and params and isinstance(params[0], list) else params + if not isinstance(entry, list) or len(entry) < 2: + raise TimerRequestError("set_timer needs [id, [status, [cron, action]]]") + job_id = str(entry[0]) + if len(entry) >= 3 and isinstance(entry[1], str): + status, detail = entry[1], entry[2] + else: + spec = entry[1] + if not isinstance(spec, list) or len(spec) < 2: + raise TimerRequestError("set_timer spec must be [status, [cron, action]]") + status, detail = spec[0], spec[1] + if not isinstance(detail, list) or not detail: + raise TimerRequestError("set_timer is missing its cron") + action = detail[1] if len(detail) > 1 else None + settings = action[1] if isinstance(action, list) and len(action) > 1 and isinstance(action[1], dict) else {} + return job_id, str(status).lower() == "on", str(detail[0]), settings + + +def _id_and_status(params: Any) -> tuple[str, str]: + entry = params[0] if isinstance(params, list) and params and isinstance(params[0], list) else params + if not isinstance(entry, list) or len(entry) < 2: + raise TimerRequestError("upd_timer needs [id, status]") + return str(entry[0]), str(entry[1]).lower() + + +def _single_id(params: Any) -> str: + if isinstance(params, list) and params: + first = params[0] + return str(first[0]) if isinstance(first, list) and first else str(first) + if params is None or params == []: + raise TimerRequestError("del_timer needs a timer id") + return str(params) + + +def handle_timer_request( + ctx: ServerContext, + duid: str, + method: str, + params: Any, + *, + default_timezone: str = "", +) -> Any: + """Apply one timer RPC to the store and return the RPC ``result`` value.""" + if method == "get_timer": + entries = (_timer_entry(job) for job in list_jobs(ctx, duid)) + return [entry for entry in entries if entry is not None] + + if method == "set_timer": + job_id, enabled, cron, action_settings = _parse_set_timer(params) + try: + parse_cron(cron) + except CronError as exc: + raise TimerRequestError(str(exc)) from exc + # An empty action object (what older clients send) leaves the job's settings alone; + # keys it does carry are merged in, so a client can change one option at a time. + settings = {key: value for key, value in action_settings.items() if key in JOB_SETTING_KEYS} + param = None + if settings: + existing = next((job for job in list_jobs(ctx, duid) if str(job.get("id")) == job_id), None) + try: + param = merge_job_settings(existing.get("param") if existing else None, settings) + except SettingsError as exc: + raise TimerRequestError(str(exc)) from exc + zone = str(getattr(ctx, "timezone", "") or default_timezone or DEFAULT_TIMEZONE) + upsert_job(ctx, duid, job_id=job_id, cron=cron, enabled=enabled, time_zone=zone, param=param) + return list(_OK) + + if method == "upd_timer": + job_id, status = _id_and_status(params) + if status not in ("on", "off"): + raise TimerRequestError(f"unknown timer status {status!r}") + set_enabled(ctx, duid, job_id, status == "on") + return list(_OK) + + if method == "del_timer": + delete_job(ctx, duid, _single_id(params)) + return list(_OK) + + raise TimerRequestError(f"not a timer method: {method!r}") + + +class TimerRpcInterceptor: + """Bridge hook: turn an encrypted timer request into an encrypted reply, or None to forward.""" + + def __init__(self, context: ServerContext, *, default_timezone: str = "") -> None: + self._context = context + self._default_timezone = default_timezone + + def _localkey(self, duid: str) -> str: + creds = self._context.runtime_credentials + device = creds.resolve_device(duid=duid) if creds is not None else None + key = str((device or {}).get("localkey") or "").strip() + return key or str(getattr(self._context, "localkey", "") or "").strip() + + def __call__(self, duid: str, payload: bytes) -> bytes | None: + localkey = self._localkey(duid) + if not localkey: + return None + try: + messages = create_mqtt_decoder(localkey)(payload) + except Exception: # noqa: BLE001 + return None + if len(messages) != 1: + return None + message = messages[0] + protocol = int(getattr(message.protocol, "value", message.protocol)) + if protocol != RoborockMessageProtocol.RPC_REQUEST.value or not message.payload: + return None + request = _request_body(message) + if request is None or request.get("method") not in TIMER_METHODS: + return None + + request_id = request.get("id") + try: + body: dict[str, Any] = { + "id": request_id, + "result": handle_timer_request( + self._context, + duid, + str(request["method"]), + request.get("params"), + default_timezone=self._default_timezone, + ), + } + except TimerRequestError as exc: + _LOGGER.warning("Rejected %s for %s: %s", request.get("method"), duid, exc) + body = {"id": request_id, "error": {"code": -1, "message": str(exc)}} + reply = RoborockMessage( + protocol=RoborockMessageProtocol.RPC_RESPONSE, + payload=json.dumps( + { + "t": int(time.time()), + "dps": {str(RoborockMessageProtocol.RPC_RESPONSE.value): json.dumps(body)}, + }, + separators=(",", ":"), + ).encode(), + seq=message.seq, + version=message.version, + ) + return create_mqtt_encoder(localkey)(reply) + + +def _request_body(message: RoborockMessage) -> dict[str, Any] | None: + try: + outer = json.loads(message.payload) + inner = outer["dps"][str(RoborockMessageProtocol.RPC_REQUEST.value)] + body = json.loads(inner) if isinstance(inner, str) else inner + except (ValueError, KeyError, TypeError): + return None + return body if isinstance(body, dict) else None diff --git a/src/roborock_local_server/server.py b/src/roborock_local_server/server.py index 3f16ed5c..20e1139f 100644 --- a/src/roborock_local_server/server.py +++ b/src/roborock_local_server/server.py @@ -55,6 +55,7 @@ from shared.inventory_io import atomic_write_inventory, inventory_transaction from shared.protocol_auth import ProtocolAuthStore from shared.schedule_runner import ScheduleRunner +from shared.timer_rpc import TimerRpcInterceptor from https_server.routes.user.scene import validity as scene_validity from https_server.routes.auth.service import ( build_login_data_response, @@ -2010,6 +2011,10 @@ async def start(self) -> None: logger=self.loggers["mqtt"], runtime_state=self.runtime_state, inventory_path=self.paths.inventory_path, + request_interceptor=TimerRpcInterceptor( + self.context, + default_timezone=self.config.network.timezone, + ), ) await self._topic_bridge.start() self.runtime_state.set_service("mqtt_topic_bridge", running=True, required=False, enabled=True) diff --git a/tests/test_schedule_store.py b/tests/test_schedule_store.py new file mode 100644 index 00000000..85d00ca5 --- /dev/null +++ b/tests/test_schedule_store.py @@ -0,0 +1,21 @@ +import roborock_local_server.backend # noqa: F401 (puts the bundled `shared` package on sys.path) +from shared.schedule_store import job_settings + + +def _job(**entry_overrides): + entry = {"repeat": 1, "fan_power": 104, "water_box_mode": 203} + entry.update(entry_overrides) + return {"id": "1", "enabled": True, "param": {"params": [entry]}} + + +def test_job_settings_carries_name_when_present(): + # "name" is the official app's link to the robot's server_timer entry, not a JOB_SETTING_KEYS + # vac/mop setting, confirmed live in its POST/PUT /user/devices/{duid}/jobs body. + job = _job(name="1759965417864") + assert job_settings(job) == {"repeat": 1, "fan_power": 104, "water_box_mode": 203, "name": "1759965417864"} + + +def test_job_settings_omits_name_when_absent_or_blank(): + assert "name" not in job_settings(_job()) + assert "name" not in job_settings(_job(name="")) + assert "name" not in job_settings(_job(name=None)) diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py new file mode 100644 index 00000000..2dcab6fe --- /dev/null +++ b/tests/test_timer_rpc.py @@ -0,0 +1,249 @@ +import asyncio +import json +import logging +from types import SimpleNamespace + +import pytest + +import roborock_local_server.backend # noqa: F401 (puts the bundled `shared` package on sys.path) +from roborock.protocol import create_mqtt_decoder, create_mqtt_encoder +from roborock.protocols.v1_protocol import decode_rpc_response +from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol +from roborock_local_server.bundled_backend.mqtt_broker_server.topic_bridge import ( + CloudTopicKey, + MqttTopicBridge, +) +from shared import schedule_store, timer_rpc + +DUID = "duid-test-1" +LOCALKEY = "testkey000000000" + + +def _cloud_job(job_id, cron, *, enabled=True, **extra): + job = { + "id": job_id, + "cron": cron, + "timeZoneId": "America/New_York", + "repeated": True, + "enabled": enabled, + "param": { + "id": 1, + "method": "server_scheduled_start", + "params": [{"repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300}], + }, + } + job.update(extra) + return job + + +@pytest.fixture +def ctx(tmp_path): + inventory = { + "schedules": { + DUID: [ + _cloud_job(3549431, "00 11 ? * 1,3,5"), + _cloud_job(3549434, "00 11 ? * 0,6", enabled=False), + _cloud_job(3878757, "03 13 15 12 ?", repeated=False), + ] + } + } + (tmp_path / "web_api_inventory.json").write_text(json.dumps(inventory), encoding="utf-8") + creds = SimpleNamespace(resolve_device=lambda duid="": {"localkey": LOCALKEY} if duid == DUID else None) + return SimpleNamespace( + http_jsonl=tmp_path / "http.jsonl", loggers={}, timezone="America/New_York", + runtime_credentials=creds, localkey="", + ) + + +def test_get_timer_lists_cloud_schedules_in_the_shape_localrock_parses(ctx): + result = timer_rpc.handle_timer_request(ctx, DUID, "get_timer", None) + # The one-off (Dec 15) keeps its day and month, the form the robot and the official app use. + settings = {"repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300} + assert result == [ + ["3549431", "on", ["00 11 * * 1,3,5", ["start_clean", settings]]], + ["3549434", "off", ["00 11 * * 0,6", ["start_clean", settings]]], + ["3878757", "on", ["03 13 15 12 *", ["start_clean", settings]]], + ] + + +@pytest.mark.parametrize( + ("cron", "expected"), + [ + ("15 23 18 10 ?", "15 23 18 10 *"), + ("15 23 18 10 *", "15 23 18 10 *"), + ("00 11 ? * 1,3,5", "00 11 * * 1,3,5"), + ("0 8 * * 0,1,2,3,4,5,6", "0 8 * * 0,1,2,3,4,5,6"), + # Only one of day and month pinned has no one-time form. + ("0 8 15 * *", None), + ("0 8 * 10 *", None), + ("not a cron", None), + ], +) +def test_device_cron_keeps_a_one_time_schedules_date(cron, expected): + assert timer_rpc.device_cron(cron) == expected + + +def test_set_timer_creates_a_job_and_get_timer_returns_it(ctx): + params = [["1790000000", ["on", ["30 7 * * 1,2", ["start_clean", {}]]]]] + assert timer_rpc.handle_timer_request(ctx, DUID, "set_timer", params) == ["ok"] + + job = next(j for j in schedule_store.list_jobs(ctx, DUID) if str(j["id"]) == "1790000000") + assert job["cron"] == "30 7 * * 1,2" + assert job["enabled"] is True + assert job["timeZoneId"] == "America/New_York" + assert job["param"]["method"] == "server_scheduled_start" + + listed = timer_rpc.handle_timer_request(ctx, DUID, "get_timer", None) + default_settings = {"repeat": 1, "segments": "0"} + assert ["1790000000", "on", ["30 7 * * 1,2", ["start_clean", default_settings]]] in listed + + +def test_editing_a_cloud_schedule_keeps_its_vac_and_mop_settings(ctx): + params = [["3549431", ["on", ["15 9 * * 1,3,5", ["start_clean", {}]]]]] + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", params) + + job = next(j for j in schedule_store.list_jobs(ctx, DUID) if j["id"] == 3549431) + assert job["cron"] == "15 9 * * 1,3,5" + entry = job["param"]["params"][0] + assert (entry["fan_power"], entry["water_box_mode"], entry["mop_mode"]) == (104, 203, 300) + + +def test_upd_timer_toggles_and_del_timer_removes_a_cloud_schedule(ctx): + assert timer_rpc.handle_timer_request(ctx, DUID, "upd_timer", [["3549431", "off"]]) == ["ok"] + job = next(j for j in schedule_store.list_jobs(ctx, DUID) if j["id"] == 3549431) + assert job["enabled"] is False + + assert timer_rpc.handle_timer_request(ctx, DUID, "del_timer", ["3549431"]) == ["ok"] + assert all(j["id"] != 3549431 for j in schedule_store.list_jobs(ctx, DUID)) + + +def test_set_timer_rejects_an_unparseable_cron(ctx): + with pytest.raises(timer_rpc.TimerRequestError): + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["1", ["on", ["not a cron", []]]]]) + + +def _encode_request(method, params, request_id=4242): + payload = json.dumps( + {"t": 1790000000, "dps": {"101": json.dumps({"id": request_id, "method": method, "params": params})}} + ).encode() + message = RoborockMessage(protocol=RoborockMessageProtocol.RPC_REQUEST, payload=payload) + return create_mqtt_encoder(LOCALKEY)(message) + + +def _decode_reply(reply): + (message,) = create_mqtt_decoder(LOCALKEY)(reply) + return decode_rpc_response(message) + + +def test_interceptor_answers_get_timer_with_an_encrypted_rpc_response(ctx): + reply = timer_rpc.TimerRpcInterceptor(ctx)(DUID, _encode_request("get_timer", [])) + assert reply is not None + response = _decode_reply(reply) + assert response.request_id == 4242 + assert [entry[0] for entry in response.data] == ["3549431", "3549434", "3878757"] + + +def test_interceptor_forwards_everything_that_is_not_a_timer_request(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + assert interceptor(DUID, _encode_request("get_status", [])) is None + assert interceptor(DUID, b"not a roborock frame") is None + assert interceptor("unknown-duid", _encode_request("get_timer", [])) is None + + +@pytest.mark.parametrize( + "method", ["get_server_timer", "set_server_timer", "upd_server_timer", "del_server_timer"] +) +def test_interceptor_leaves_server_timer_to_the_robot(ctx, method): + # A robot that speaks server_timer stores those entries itself and the official app reads + # them back from it; answering this locally hides the robot's real schedules from every + # client and has caused real data loss. This must always forward, never answer here. + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + assert interceptor(DUID, _encode_request(method, [])) is None + + +def test_bridge_answers_timer_requests_locally_and_does_not_forward_them(ctx): + class Client: + def __init__(self): + self.published = [] + + async def publish(self, topic, payload, *, qos=0, retain=False): + self.published.append((topic, payload)) + + bridge = MqttTopicBridge( + host="127.0.0.1", + port=1883, + logger=logging.getLogger("test.timer_bridge"), + request_interceptor=timer_rpc.TimerRpcInterceptor(ctx), + ) + cloud = CloudTopicKey(rriot_u="u", mqtt_username="m", duid=DUID) + message = SimpleNamespace(payload=_encode_request("get_timer", []), qos=0, retain=False) + client = Client() + + asyncio.run(bridge._handle_cloud_message(client, message, cloud, cloud.topic_in)) + + assert [topic for topic, _ in client.published] == [cloud.topic_out] + assert _decode_reply(client.published[0][1]).request_id == 4242 + + +def _job(ctx, job_id): + return next(j for j in schedule_store.list_jobs(ctx, DUID) if str(j["id"]) == str(job_id)) + + +def test_set_timer_carries_vac_and_mop_settings_in_the_action_object(ctx): + action = ["start_clean", {"fan_power": 102, "water_box_mode": 200, "repeat": 2}] + params = [["1790000001", ["on", ["0 9 * * 2,4", action]]]] + assert timer_rpc.handle_timer_request(ctx, DUID, "set_timer", params) == ["ok"] + + entry = _job(ctx, 1790000001)["param"]["params"][0] + assert (entry["fan_power"], entry["water_box_mode"], entry["repeat"]) == (102, 200, 2) + + listed = timer_rpc.handle_timer_request(ctx, DUID, "get_timer", None) + row = next(item for item in listed if item[0] == "1790000001") + assert row[2][1][1]["water_box_mode"] == 200 + + +def test_changing_one_setting_leaves_the_others_alone(ctx): + action = ["start_clean", {"water_box_mode": 200}] # vac only on this cloud schedule + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["3549431", ["on", ["00 11 * * 1,3,5", action]]]]) + + entry = _job(ctx, 3549431)["param"]["params"][0] + assert entry["water_box_mode"] == 200 + assert (entry["fan_power"], entry["mop_mode"]) == (104, 300) + + +def test_an_empty_action_object_does_not_reset_existing_settings(ctx): + # What older LocalRock builds send: ["start_clean", {}]. + action = ["start_clean", {}] + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["3549431", ["on", ["30 8 * * 1,3,5", action]]]]) + + job = _job(ctx, 3549431) + assert job["cron"] == "30 8 * * 1,3,5" + assert job["param"]["params"][0]["water_box_mode"] == 203 + + +def test_null_clears_a_setting_and_segments_are_normalised(ctx): + action = ["start_clean", {"mop_mode": None, "segments": [16, 17]}] + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["3549431", ["on", ["00 11 * * 1,3,5", action]]]]) + + entry = _job(ctx, 3549431)["param"]["params"][0] + assert "mop_mode" not in entry + assert entry["segments"] == "16,17" + + +def test_vac_only_schedule_fires_without_a_mop_template(ctx): + from shared import schedule_runner + + action = ["start_clean", {"water_box_mode": 200, "fan_power": 102}] + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["1790000002", ["on", ["0 9 * * *", action]]]]) + + (step,) = schedule_runner.steps_for_job(_job(ctx, 1790000002)) + assert step.params[0]["water_box_mode"] == 200 + assert step.params[0]["fan_power"] == 102 + + +def test_non_numeric_setting_is_rejected_and_nothing_is_saved(ctx): + before = schedule_store.list_jobs(ctx, DUID) + action = ["start_clean", {"fan_power": "loud"}] + with pytest.raises(timer_rpc.TimerRequestError): + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["9", ["on", ["0 9 * * *", action]]]]) + assert schedule_store.list_jobs(ctx, DUID) == before From 54002a24a4444b3775cc212f8c48de62cd09863b Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:21:40 -0400 Subject: [PATCH 06/27] Accept schedule create, update and delete over HTTP POST /user/devices/{duid}/jobs creates a job (the call python-roborock makes), and PUT/PATCH and DELETE on /jobs/{id} edit or remove one. The GET matcher is now method-aware so a write no longer falls through to the list route. Update and delete paths are not confirmed against the official app; they follow the scene routes' conventions. Co-Authored-By: Claude Sonnet 5.5 (cherry picked from commit 5b838531eaebe34fe369b59e511f9aab1f848d29) --- .../https_server/endpoint_rules.py | 9 ++ .../https_server/routes/user/devices/jobs.py | 126 +++++++++++++++++- tests/test_schedules.py | 87 ++++++++++++ 3 files changed, 219 insertions(+), 3 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/https_server/endpoint_rules.py b/src/roborock_local_server/bundled_backend/https_server/endpoint_rules.py index f2f2dcf8..e0987da0 100644 --- a/src/roborock_local_server/bundled_backend/https_server/endpoint_rules.py +++ b/src/roborock_local_server/bundled_backend/https_server/endpoint_rules.py @@ -120,7 +120,13 @@ from .routes.user.deviceshare import match_received_devices as _match_get_received_devices from .routes.user.deviceshare import match_rooms as _match_get_shared_device_rooms from .routes.user.devices.jobs import build as _build_get_schedules +from .routes.user.devices.jobs import build_create as _build_create_schedule +from .routes.user.devices.jobs import build_delete as _build_delete_schedule +from .routes.user.devices.jobs import build_update as _build_update_schedule from .routes.user.devices.jobs import match as _match_get_schedules +from .routes.user.devices.jobs import match_create as _match_create_schedule +from .routes.user.devices.jobs import match_delete as _match_delete_schedule +from .routes.user.devices.jobs import match_update as _match_update_schedule from .routes.user.devices.newadd import build as _build_add_device from .routes.user.devices.newadd import match as _match_add_device from .routes.user.homes.item import build as _build_get_home_data @@ -609,6 +615,9 @@ def default_endpoint_rules() -> Sequence[EndpointRule]: 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), + EndpointRule("create_schedule", _match_create_schedule, _build_create_schedule), + EndpointRule("update_schedule", _match_update_schedule, _build_update_schedule), + EndpointRule("delete_schedule", _match_delete_schedule, _build_delete_schedule), EndpointRule("post_app_info", _match_post_app_info, _build_post_app_info), EndpointRule("get_inbox_latest", _match_get_inbox_latest, _build_get_inbox_latest), EndpointRule("get_products_v4", _match_get_products_v4, _build_get_products_v4), diff --git a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py index d0ab3239..1aa3ef82 100644 --- a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py +++ b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py @@ -1,17 +1,25 @@ from __future__ import annotations +import json import re +import time from typing import Any +from shared.constants import DEFAULT_TIMEZONE from shared.context import ServerContext +from shared.data_helpers import as_bool from shared.http_helpers import wrap_response +from shared.schedule_runner import CronError, parse_cron +from shared.schedule_store import delete_job, list_jobs, upsert_job from .service import device_jobs_payload +_COLLECTION_RE = re.compile(r"/user/devices/([^/]+)/jobs") +_ITEM_RE = re.compile(r"/user/devices/([^/]+)/jobs/([^/]+)") -def match(path: str) -> bool: - clean = path.rstrip("/") - return bool(re.fullmatch(r"/user/devices/[^/]+/jobs", clean)) + +def match(path: str, method: str = "GET") -> bool: + return method.upper() == "GET" and bool(_COLLECTION_RE.fullmatch(path.rstrip("/"))) def build( @@ -24,3 +32,115 @@ def build( device_id = parts[-2] if len(parts) >= 2 else "" return wrap_response(device_jobs_payload(ctx, device_id)) + +def _error(code: int, message: str) -> dict[str, Any]: + return {"success": False, "code": code, "msg": message, "data": None, "result": None} + + +def _json_body(body_params: dict[str, list[str]]) -> dict[str, Any]: + for raw in body_params.get("__json") or []: + try: + parsed = json.loads(raw) + except (TypeError, json.JSONDecodeError): + continue + if isinstance(parsed, dict): + return parsed + return {} + + +def _parse_param(value: Any) -> dict[str, Any] | None: + if isinstance(value, str) and value.strip(): + try: + value = json.loads(value) + except json.JSONDecodeError: + return None + return value if isinstance(value, dict) else None + + +def match_create(path: str, method: str = "GET") -> bool: + return method.upper() == "POST" and bool(_COLLECTION_RE.fullmatch(path.rstrip("/"))) + + +def build_create( + ctx: ServerContext, + _query_params: dict[str, list[str]], + body_params: dict[str, list[str]], + clean_path: str, +) -> dict[str, Any]: + duid = _COLLECTION_RE.fullmatch(clean_path.rstrip("/")).group(1) # type: ignore[union-attr] + body = _json_body(body_params) + cron = str(body.get("cron") or "").strip() + try: + parse_cron(cron) + except CronError as exc: + return _error(400, f"invalid cron: {exc}") + param = _parse_param(body.get("param")) + if body.get("param") is not None and param is None: + return _error(400, "param must be a JSON object") + job_id = body.get("id") or int(time.time()) + created = upsert_job( + ctx, + duid, + job_id=job_id, + cron=cron, + enabled=as_bool(body.get("enabled", True), True), + time_zone=str(body.get("timeZoneId") or getattr(ctx, "timezone", "") or DEFAULT_TIMEZONE), + repeated=as_bool(body.get("repeated", True), True), + param=param, + ) + return wrap_response(created) + + +def match_update(path: str, method: str = "GET") -> bool: + return method.upper() in ("PUT", "PATCH") and bool(_ITEM_RE.fullmatch(path.rstrip("/"))) + + +def build_update( + ctx: ServerContext, + _query_params: dict[str, list[str]], + body_params: dict[str, list[str]], + clean_path: str, +) -> dict[str, Any]: + match_ = _ITEM_RE.fullmatch(clean_path.rstrip("/")) + duid, job_id = match_.group(1), match_.group(2) # type: ignore[union-attr] + existing = next((job for job in list_jobs(ctx, duid) if str(job.get("id")) == job_id), None) + if existing is None: + return _error(404, "schedule not found") + body = _json_body(body_params) + cron = str(body.get("cron") or existing.get("cron") or "").strip() + try: + parse_cron(cron) + except CronError as exc: + return _error(400, f"invalid cron: {exc}") + param = _parse_param(body.get("param")) if "param" in body else None + if "param" in body and param is None: + return _error(400, "param must be a JSON object") + updated = upsert_job( + ctx, + duid, + job_id=job_id, + cron=cron, + enabled=as_bool(body.get("enabled", existing.get("enabled", True)), True), + time_zone=str(body.get("timeZoneId") or ""), + repeated=as_bool(body["repeated"], True) if "repeated" in body else None, + param=param, + override_time_zone=bool(body.get("timeZoneId")), + ) + return wrap_response(updated) + + +def match_delete(path: str, method: str = "GET") -> bool: + return method.upper() == "DELETE" and bool(_ITEM_RE.fullmatch(path.rstrip("/"))) + + +def build_delete( + ctx: ServerContext, + _query_params: dict[str, list[str]], + _body_params: dict[str, list[str]], + clean_path: str, +) -> dict[str, Any]: + match_ = _ITEM_RE.fullmatch(clean_path.rstrip("/")) + duid, job_id = match_.group(1), match_.group(2) # type: ignore[union-attr] + if not delete_job(ctx, duid, job_id): + return _error(404, "schedule not found") + return wrap_response({"id": job_id}) diff --git a/tests/test_schedules.py b/tests/test_schedules.py index e4a9ced3..f362a6ab 100644 --- a/tests/test_schedules.py +++ b/tests/test_schedules.py @@ -166,3 +166,90 @@ def test_cloud_inventory_build_preserves_device_schedules() -> None: "6HL2zfniaoYYV01CkVuhkO": [schedule], "ignored": [], } + + +def _jobs_client(tmp_path: Path, device_id: str, schedules: list[dict[str, object]]): + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + _write_json( + paths.inventory_path, + { + "home": {"id": 1233716, "name": "My Home"}, + "devices": [{"duid": device_id, "name": "Roborock Qrevo"}], + "schedules": {device_id: schedules}, + }, + ) + _seed_cloud_snapshot(paths.cloud_snapshot_path) + supervisor = ReleaseSupervisor(config=config, paths=paths) + supervisor.refresh_inventory_state() + return TestClient(supervisor.app), paths + + +def _signed_json(paths, path: str, body: dict[str, object]) -> tuple[bytes, dict[str, str]]: + raw = json.dumps(body, separators=(",", ":")).encode() + user = ProtocolAuthStore(paths.cloud_snapshot_path).availability().user + assert user is not None + headers = { + # Hawk rejects replayed nonces, and _hawk_headers() uses nonce- for plain requests. + "Authorization": build_hawk_authorization( + user=user, path=path, json_body=raw, nonce=f"write-{path.replace('/', '-')}-{len(raw)}" + ), + "Content-Type": "application/json", + } + return raw, headers + + +def test_jobs_can_be_created_updated_and_deleted_over_http(tmp_path: Path) -> None: + device_id = "6HL2zfniaoYYV01CkVuhkO" + existing = { + "id": 3549431, + "cron": "00 11 ? * 1,3,5", + "timeZoneId": "America/New_York", + "repeated": True, + "enabled": True, + "param": { + "id": 1, + "method": "server_scheduled_start", + "params": [{"repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300}], + }, + } + client, paths = _jobs_client(tmp_path, device_id, [existing]) + collection = f"/user/devices/{device_id}/jobs" + + body, headers = _signed_json(paths, collection, {"cron": "30 7 * * 1,2", "enabled": True}) + created = client.post(collection, content=body, headers=headers) + assert created.status_code == 200 + assert created.json()["success"] is True + new_id = created.json()["result"]["id"] + + listed = client.get(collection, headers=_hawk_headers(paths.cloud_snapshot_path, collection)).json()["result"] + assert {job["id"] for job in listed} == {3549431, new_id} + assert all(job.get("nextFireTime") for job in listed) + + item = f"{collection}/3549431" + body, headers = _signed_json(paths, item, {"cron": "15 9 ? * 1,3,5", "enabled": False}) + updated = client.put(item, content=body, headers=headers) + assert updated.status_code == 200 + result = updated.json()["result"] + assert (result["cron"], result["enabled"]) == ("15 9 ? * 1,3,5", False) + # Vac/mop settings are untouched by a cron/enabled edit. + assert result["param"]["params"][0]["mop_mode"] == 300 + + deleted = client.delete(item, headers=_hawk_headers(paths.cloud_snapshot_path, item)) + assert deleted.status_code == 200 and deleted.json()["success"] is True + user = ProtocolAuthStore(paths.cloud_snapshot_path).availability().user + assert user is not None + second_list = {"Authorization": build_hawk_authorization(user=user, path=collection, nonce="second-list")} + remaining = client.get(collection, headers=second_list).json()["result"] + assert [job["id"] for job in remaining] == [new_id] + + +def test_creating_a_job_with_a_bad_cron_is_rejected(tmp_path: Path) -> None: + device_id = "6HL2zfniaoYYV01CkVuhkO" + client, paths = _jobs_client(tmp_path, device_id, []) + collection = f"/user/devices/{device_id}/jobs" + body, headers = _signed_json(paths, collection, {"cron": "nonsense"}) + response = client.post(collection, content=body, headers=headers) + assert response.json()["success"] is False + assert response.json()["code"] == 400 From 4154b069919722aa4a17d3d87217394550561e03 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sun, 18 Oct 2026 13:12:00 -0400 Subject: [PATCH 07/27] Run a schedule the way the cloud does: ask the robot to start its timer A schedule's job (POST /user/devices/{duid}/jobs) carries a settings entry whose "name" is the id of the matching timer the official app also saved on the robot. The cloud runs such a job by sending server_scheduled_start with that entry, which is the method name inside the job itself. The robot accepts it, runs the clean (rooms or whole home) and records it as "Schedules" in its history; without the entry it answers -10005 "Invalid timer", and with a bare id "Param should be name + fan + segment". python-roborock does not list the command, so it is sent through a small SchedulerCommand enum. A schedule with no robot-side id (one saved through the legacy set_timer), or one the robot refuses, falls back to the plainer ways, since a refused command changes nothing: a whole-home clean is one app_start carrying the schedule's fields plus source 3, which the robot also records as "Schedules"; a room schedule is the settings commands followed by a segment clean. Scenes and routines are untouched. The new steps are produced only by schedules. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/routine_runner.py | 118 +++++++++++-- .../bundled_backend/shared/schedule_runner.py | 28 ++- tests/test_routine_runner.py | 162 +++++++++++++++++- tests/test_schedule_runner.py | 34 +++- 4 files changed, 318 insertions(+), 24 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/shared/routine_runner.py b/src/roborock_local_server/bundled_backend/shared/routine_runner.py index bae65aa4..ad408ca7 100644 --- a/src/roborock_local_server/bundled_backend/shared/routine_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/routine_runner.py @@ -3,7 +3,8 @@ from __future__ import annotations import asyncio -from dataclasses import dataclass +from dataclasses import dataclass, replace +from enum import StrEnum import json import logging from pathlib import Path @@ -58,6 +59,21 @@ def _ensure_local_python_roborock_on_path() -> None: _ACTION_LOCK_RETRY_DELAY_SECONDS = 5.0 _ACTION_LOCK_RETRY_ATTEMPTS = 6 from .inventory_io import WEB_API_INVENTORY_FILE +class SchedulerCommand(StrEnum): + """A robot command python-roborock's RoborockCommand does not list.""" + + # The cloud's own way to run a schedule: the robot answers it with the schedule's settings entry + # (the same one the cloud job carries, including "name", the robot-side timer id) and records the + # clean as "Schedules" in its history, for whole-home and room schedules alike. Confirmed live; + # without the entry it answers -10005 "Invalid timer", and with a bare id "Param should be + # name + fan + segment". + SERVER_SCHEDULED_START = "server_scheduled_start" + + +# A whole-home clean started by a schedule rather than a scene; see commands_for_step. +SCHEDULE_APP_START = "schedule_app_start" +# Any schedule that knows its robot-side timer id; the robot runs it itself when asked this way. +SCHEDULE_SERVER_START = "schedule_server_start" _SUPPORTED_METHODS = { "do_scenes_app_start", "do_scenes_segments", @@ -67,8 +83,21 @@ def _ensure_local_python_roborock_on_path() -> None: RoborockCommand.APP_START, RoborockCommand.APP_SEGMENT_CLEAN, RoborockCommand.APP_ZONED_CLEAN, + SchedulerCommand.SERVER_SCHEDULED_START, } _ACTION_LOCK_ERROR_CODES = {-10003, -10007} +_PARAM_ERROR_CODE = -10005 +# What app_start takes in one command, as the official app's own schedule entries carry it. A +# robot accepted every one of these together and applied the modes (confirmed live); it answers +# -10005 "Param error" to a value it does not support, such as a route its model lacks. +_APP_START_KEYS = ("repeat", "water_box_mode", "segments", "fan_power", "mop_mode", "clean_mop", "map_index") +_APP_START_DEFAULTS = {"repeat": 1, "segments": "0", "clean_mop": 1, "map_index": -1} +_SETTING_ENTRY_KEYS = ("repeat", "fan_power", "water_box_mode", "mop_mode", "mop_template_id") +# app_start's "source" field is how the robot learns who started a clean, for its history. 3 is +# python-roborock's RoborockStartType.schedule, and a robot recorded the clean as "Schedule" in the +# official app's history; an app start is 2. "start_type", "startType", "start_from", "from" and +# "trigger" were all accepted but ignored, so the clean was recorded as "App". +_SCHEDULE_START_SOURCE = 3 _RESUME_COMMAND_BY_IN_CLEANING: dict[int, RoborockCommand] = { RoborockInCleaning.global_clean_not_complete.value: RoborockCommand.APP_START, RoborockInCleaning.zone_clean_not_complete.value: RoborockCommand.RESUME_ZONED_CLEAN, @@ -82,7 +111,7 @@ class RoutineExecutionError(RuntimeError): @dataclass(frozen=True) class RoutineCommand: - command: RoborockCommand + command: RoborockCommand | SchedulerCommand params: dict[str, Any] | list[Any] | None = None @@ -384,6 +413,15 @@ def commands_for_step(step: RoutineStep) -> list[RoutineCommand]: ), RoutineCommand(RoborockCommand.APP_START, None), ] + if step.method == SCHEDULE_SERVER_START: + entry = _single_start_entry(step) + body = {key: entry[key] for key in (*_APP_START_KEYS, "name") if key in entry} + return [RoutineCommand(SchedulerCommand.SERVER_SCHEDULED_START, [body])] + if step.method == SCHEDULE_APP_START: + entry = _single_start_entry(step) + body = {**_APP_START_DEFAULTS, **{key: entry[key] for key in _APP_START_KEYS if key in entry}} + body["source"] = _SCHEDULE_START_SOURCE + return [RoutineCommand(RoborockCommand.APP_START, [body])] raise RoutineExecutionError(f"Unsupported step method {step.method!r}") @@ -400,6 +438,32 @@ def commands_for_step(step: RoutineStep) -> list[RoutineCommand]: } +def fallback_commands_for_step(step: RoutineStep) -> list[RoutineCommand] | None: + """The longer way to run [step], for when the robot refuses the short one; None if there is none. + + A schedule is started by asking the robot to run it (server_scheduled_start), and failing that + by one app_start carrying its settings. A refused command changes nothing, so on "Param error" + or "Invalid timer" the next, plainer way is tried: a room schedule as a segment clean, a + whole-home one as separate setting commands, which is how a routine starts a whole-home clean. + """ + if step.method == SCHEDULE_SERVER_START: + entry = _single_start_entry(step) + rooms = [ + int(part) + for part in str(entry.get("segments") or "").replace(";", ",").split(",") + if part.strip().isdigit() and int(part) > 0 + ] + if rooms: + settings = {key: entry[key] for key in _SETTING_ENTRY_KEYS if key in entry} + return commands_for_step( + replace(step, method="do_scenes_segments", params={"data": [{**settings, "segs": rooms}]}) + ) + return commands_for_step(replace(step, method=SCHEDULE_APP_START)) + if step.method != SCHEDULE_APP_START: + return None + return commands_for_step(replace(step, method="do_scenes_app_start")) + + def _is_optional_unsupported_command(command: RoborockCommand, exc: Exception) -> bool: return command in _OPTIONAL_SETTING_COMMANDS and isinstance(exc, RoborockUnsupportedFeature) @@ -555,7 +619,7 @@ def _on_message(self, payload: bytes) -> None: async def send_command( self, - command: RoborockCommand, + command: RoborockCommand | SchedulerCommand, params: dict[str, Any] | list[Any] | None = None, ) -> Any: if self._session is None: @@ -1014,6 +1078,29 @@ async def _send_step_command( continue raise + async def _send_step_commands( + self, + *, + client: _RoutineMqttClient, + logger: logging.LoggerAdapter, + step: RoutineStep, + commands: list[RoutineCommand], + ) -> None: + for routine_command in commands: + logger.info( + "Routine step=%s method=%s command=%s params=%s", + step.step_id, + step.method, + routine_command.command.value, + routine_command.params, + ) + await self._send_step_command( + client=client, + logger=logger, + step=step, + routine_command=routine_command, + ) + async def _run_scene(self, *, scene: dict[str, Any], steps: list[RoutineStep]) -> None: await self._run_steps(scene=scene, steps=steps, sync_scene_tids=True) @@ -1048,22 +1135,21 @@ async def _run_steps( if sync_scene_tids: await self._sync_scene_tids(client=client, scene=scene, device_id=device_id, logger=logger) for step_index, step in enumerate(steps): - commands = commands_for_step(step) waits_for_step_complete = RoborockDataProtocol.TASK_COMPLETE.value in step.finish_dp_ids - for routine_command in commands: - logger.info( - "Routine step=%s method=%s command=%s params=%s", - step.step_id, - step.method, - routine_command.command.value, - routine_command.params, + try: + await self._send_step_commands( + client=client, logger=logger, step=step, commands=commands_for_step(step) ) - await self._send_step_command( - client=client, - logger=logger, - step=step, - routine_command=routine_command, + except Exception as exc: # noqa: BLE001 + fallback = fallback_commands_for_step(step) + if fallback is None or _exception_error_code(exc) != _PARAM_ERROR_CODE: + raise + logger.warning( + "Robot refused the single start step=%s (%s); sending the settings as separate commands", + step.step_id, + _exception_error_message(exc), ) + await self._send_step_commands(client=client, logger=logger, step=step, commands=fallback) if waits_for_step_complete: logger.info("Waiting for ready state step=%s scene=%s", step.step_id, _scene_name(scene)) await client.wait_for_step_complete() diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index 33fa64ce..99259982 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -18,7 +18,13 @@ from .constants import DEFAULT_TIMEZONE from .data_helpers import as_bool from .inventory_io import load_inventory -from .routine_runner import RoutineExecutionError, RoutineStep, get_routine_runner +from .routine_runner import ( + SCHEDULE_APP_START, + SCHEDULE_SERVER_START, + RoutineExecutionError, + RoutineStep, + get_routine_runner, +) from .schedule_store import set_enabled if TYPE_CHECKING: @@ -29,6 +35,7 @@ TICK_SECONDS = 20 _SUPPORTED_JOB_METHOD = "server_scheduled_start" _CARRIED_ENTRY_KEYS = ("repeat", "fan_power", "water_box_mode", "mop_mode", "mop_template_id") +_START_ENTRY_KEYS = (*_CARRIED_ENTRY_KEYS, "segments", "clean_mop", "map_index") class CronError(ValueError): @@ -191,9 +198,24 @@ def steps_for_job(job: dict[str, Any]) -> list[RoutineStep]: raise RoutineExecutionError("schedule params must contain one settings object") settings = {key: entry[key] for key in _CARRIED_ENTRY_KEYS if key in entry} + # A whole-home start also carries the rest of the entry, so one app_start can take it all. + start_settings = {key: entry[key] for key in _START_ENTRY_KEYS if key in entry} + # The robot-side timer id the official app (or LocalRock) saved this schedule under. A schedule + # that has one is run the way the cloud runs it: the robot is asked to start that timer. + timer_id = entry.get("name") name = str(entry.get("name") or job.get("name") or "").strip() if not name or name.isdigit(): name = f"Schedule {job.get('id', '')}".strip() + if isinstance(timer_id, str) and timer_id.strip(): + return [ + RoutineStep( + step_id=1, + name=name, + method=SCHEDULE_SERVER_START, + params=[{**start_settings, "name": timer_id.strip()}], + finish_dp_ids=(), + ) + ] segments = _segment_ids(entry.get("segments")) if segments: return [ @@ -209,8 +231,8 @@ def steps_for_job(job: dict[str, Any]) -> list[RoutineStep]: RoutineStep( step_id=1, name=name, - method="do_scenes_app_start", - params=[settings], + method=SCHEDULE_APP_START, + params=[start_settings], finish_dp_ids=(), ) ] diff --git a/tests/test_routine_runner.py b/tests/test_routine_runner.py index eddac37d..01ca4da9 100644 --- a/tests/test_routine_runner.py +++ b/tests/test_routine_runner.py @@ -9,7 +9,13 @@ from roborock.exceptions import RoborockException, RoborockUnsupportedFeature from roborock_local_server.bundled_backend.shared.context import ServerContext import roborock_local_server.bundled_backend.shared.routine_runner as routine_runner_module -from roborock_local_server.bundled_backend.shared.routine_runner import RoutineRunner, parse_scene_steps +from roborock_local_server.bundled_backend.shared.routine_runner import ( + SCHEDULE_APP_START, + SCHEDULE_SERVER_START, + RoutineRunner, + RoutineStep, + parse_scene_steps, +) from roborock.roborock_typing import RoborockCommand @@ -877,3 +883,157 @@ async def wait_for_step_complete(self) -> None: await runner._run_scene(scene=scene, steps=parse_scene_steps(scene)) asyncio.run(exercise()) + + +def _schedule_step(**entry) -> RoutineStep: + return RoutineStep(step_id=1, name="Schedule 1", method=SCHEDULE_APP_START, params=[entry], finish_dp_ids=()) + + +def _run_schedule_step(tmp_path: Path, monkeypatch, step: RoutineStep, *, refuse_single_start_with: dict | None): + """Run [step] against a fake robot; returns every command it was sent, refused ones included.""" + sent_commands: list[tuple[RoborockCommand, object]] = [] + + class FakeRoutineClient: + def __init__(self, context, device, logger) -> None: + _ = context, device, logger + + async def connect(self) -> None: + return None + + async def close(self) -> None: + return None + + async def send_command(self, command, params=None): + sent_commands.append((command, params)) + if refuse_single_start_with is not None and command == RoborockCommand.APP_START and params is not None: + raise RoborockException(refuse_single_start_with) + return ["ok"] + + async def wait_for_step_complete(self) -> None: + return None + + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", FakeRoutineClient) + scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") + runner = RoutineRunner(_test_context(tmp_path)) + return sent_commands, asyncio.run(runner._run_steps(scene=scene, steps=[step], sync_scene_tids=False)) + + +def test_a_schedule_start_is_one_app_start_carrying_the_whole_entry(tmp_path: Path, monkeypatch) -> None: + # A robot accepted app_start with all of these together and applied the modes (confirmed live). + step = _schedule_step(fan_power=104, water_box_mode=203, mop_mode=300) + + sent, _ = _run_schedule_step(tmp_path, monkeypatch, step, refuse_single_start_with=None) + + assert sent == [ + ( + RoborockCommand.APP_START, + [{"fan_power": 104, "water_box_mode": 203, "mop_mode": 300, + "repeat": 1, "segments": "0", "clean_mop": 1, "map_index": -1, + # "source" 3 is what makes the robot's history say "Schedule" instead of "App". + "source": 3}], + ) + ] + + +def test_a_schedule_start_falls_back_to_separate_commands_on_a_param_error(tmp_path: Path, monkeypatch) -> None: + # A route the robot's model lacks makes it answer -10005 "Param error", and a refused command + # changes nothing, so the same settings go out the longer way. + step = _schedule_step(fan_power=104, water_box_mode=203, mop_mode=300) + + sent, _ = _run_schedule_step( + tmp_path, monkeypatch, step, refuse_single_start_with={"code": -10005, "message": "Param error"} + ) + + assert [command for command, _ in sent] == [ + RoborockCommand.APP_START, + RoborockCommand.SET_CUSTOM_MODE, + RoborockCommand.SET_WATER_BOX_CUSTOM_MODE, + RoborockCommand.SET_MOP_MODE, + RoborockCommand.SET_CLEAN_REPEAT_TIMES, + RoborockCommand.APP_START, + ] + assert sent[-1] == (RoborockCommand.APP_START, None) + + +def test_a_schedule_start_does_not_fall_back_on_any_other_refusal(tmp_path: Path, monkeypatch) -> None: + step = _schedule_step(fan_power=104) + + with pytest.raises(RoborockException): + _run_schedule_step( + tmp_path, monkeypatch, step, refuse_single_start_with={"code": -10001, "message": "Busy"} + ) + + +def _run_schedule_step_refusing(tmp_path: Path, monkeypatch, step: RoutineStep, error: dict): + """Like _run_schedule_step, but the robot refuses server_scheduled_start and accepts the rest.""" + sent_commands: list[tuple[object, object]] = [] + + class FakeRoutineClient: + def __init__(self, context, device, logger) -> None: + _ = context, device, logger + + async def connect(self) -> None: + return None + + async def close(self) -> None: + return None + + async def send_command(self, command, params=None): + sent_commands.append((command, params)) + if command == routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START: + raise RoborockException(error) + return ["ok"] + + async def wait_for_step_complete(self) -> None: + return None + + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", FakeRoutineClient) + scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") + runner = RoutineRunner(_test_context(tmp_path)) + return sent_commands, asyncio.run(runner._run_steps(scene=scene, steps=[step], sync_scene_tids=False)) + + +def _server_start_step(**entry) -> RoutineStep: + return RoutineStep(step_id=1, name="Schedule 1", method=SCHEDULE_SERVER_START, params=[entry], finish_dp_ids=()) + + +def test_a_server_start_asks_the_robot_to_run_the_schedule_with_its_whole_entry(tmp_path: Path, monkeypatch) -> None: + # Sent exactly as the cloud job's settings entry, "name" (the robot-side timer id) included; + # confirmed live to start a clean the robot records as "Schedules | Rooms". + step = _server_start_step( + repeat=1, water_box_mode=202, segments="16,17", fan_power=102, mop_mode=300, + clean_mop=1, map_index=0, name="1792336598179", + ) + + sent, _ = _run_schedule_step(tmp_path, monkeypatch, step, refuse_single_start_with=None) + + assert [command for command, _ in sent] == [routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START] + assert sent[0][1] == [ + {"repeat": 1, "water_box_mode": 202, "segments": "16,17", "fan_power": 102, "mop_mode": 300, + "clean_mop": 1, "map_index": 0, "name": "1792336598179"} + ] + + +def test_a_refused_server_start_falls_back_to_a_segment_clean_for_a_room_schedule(tmp_path: Path, monkeypatch) -> None: + # "Invalid timer" (the robot no longer holds that timer) is a -10005 like any bad parameter. + step = _server_start_step(fan_power=102, water_box_mode=202, segments="16,17", name="gone") + sent, _ = _run_schedule_step_refusing(tmp_path, monkeypatch, step, {"code": -10005, "message": "Invalid timer"}) + + commands = [command for command, _ in sent] + assert commands[0] == routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START + assert commands[-1] == RoborockCommand.APP_SEGMENT_CLEAN + assert sent[-1][1] == [{"segments": [16, 17], "repeat": 1}] + + +def test_a_refused_server_start_falls_back_to_the_single_app_start_for_a_whole_home_schedule( + tmp_path: Path, monkeypatch +) -> None: + step = _server_start_step(fan_power=102, water_box_mode=202, segments="0", name="gone") + sent, _ = _run_schedule_step_refusing(tmp_path, monkeypatch, step, {"code": -10005, "message": "Invalid timer"}) + + assert sent[0][0] == routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START + assert sent[1] == ( + RoborockCommand.APP_START, + [{"repeat": 1, "segments": "0", "clean_mop": 1, "map_index": -1, "fan_power": 102, + "water_box_mode": 202, "source": 3}], + ) diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py index 8769898b..2ee5f098 100644 --- a/tests/test_schedule_runner.py +++ b/tests/test_schedule_runner.py @@ -30,7 +30,6 @@ def _job(**overrides): "mop_mode": 300, "clean_mop": 1, "map_index": -1, - "name": "1759965417864", } ], }, @@ -78,11 +77,38 @@ def test_whole_house_job_becomes_an_app_start_step_carrying_vac_and_mop_settings steps = sr.steps_for_job(_job()) assert len(steps) == 1 step = steps[0] - assert step.method == "do_scenes_app_start" - assert step.params == [{"repeat": 1, "water_box_mode": 203, "fan_power": 104, "mop_mode": 300}] + assert step.method == "schedule_app_start" + assert step.params == [ + {"repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300, "clean_mop": 1, "map_index": -1} + ] assert step.name == "Schedule 3549431" +def test_job_with_a_robot_side_timer_id_is_run_by_asking_the_robot_to_start_it(): + # The official app saves every schedule on the robot too and records its timer id as "name" in + # the cloud job's settings; the cloud runs such a job with server_scheduled_start (confirmed + # live: the robot records "Schedules" for it, for rooms and whole-home alike). + job = _job() + job["param"]["params"][0]["name"] = "1759965417864" + step = sr.steps_for_job(job)[0] + assert step.method == "schedule_server_start" + assert step.params == [ + { + "repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300, + "clean_mop": 1, "map_index": -1, "name": "1759965417864", + } + ] + + +def test_room_job_with_a_robot_side_timer_id_carries_its_rooms_in_the_same_step(): + job = _job() + job["param"]["params"][0].update({"name": "1759965417864", "segments": "16,17", "map_index": 0}) + step = sr.steps_for_job(job)[0] + assert step.method == "schedule_server_start" + assert step.params[0]["segments"] == "16,17" + assert step.params[0]["map_index"] == 0 + + def test_segment_job_becomes_a_segment_clean_step(): job = _job() job["param"]["params"][0]["segments"] = "16,17" @@ -123,7 +149,7 @@ def test_tick_fires_a_job_that_came_due_since_the_previous_tick(tmp_path, monkey results = runner.tick(_utc(2026, 9, 28, 15, 0, 5)) assert [r["status"] for r in results] == ["started"] assert fake.started[0]["device_id"] == "duid-1" - assert fake.started[0]["steps"][0].method == "do_scenes_app_start" + assert fake.started[0]["steps"][0].method == "schedule_app_start" def test_tick_does_not_fire_twice_or_when_disabled(tmp_path, monkeypatch): From 1aaf386fe13de461a7771199a94d2da87ba3d09b Mon Sep 17 00:00:00 2001 From: pponce Date: Mon, 26 Oct 2026 14:24:24 -0700 Subject: [PATCH 08/27] Integrate schedule dispatch hardening and verified enablement --- docs/known_limitations.md | 2 +- docs/schedules.md | 137 +++++++++ mkdocs.yml | 1 + .../https_server/routes/user/devices/jobs.py | 160 ++++++++--- .../routes/user/devices/service.py | 2 + .../import_web_inventory_from_cloud.py | 7 + .../mqtt_broker_server/topic_bridge.py | 7 + .../shared/native_schedule_sync.py | 268 ++++++++++++++++++ .../bundled_backend/shared/routine_runner.py | 62 ++-- .../bundled_backend/shared/schedule_runner.py | 72 +++-- .../bundled_backend/shared/schedule_store.py | 161 ++++++++++- .../bundled_backend/shared/timer_rpc.py | 5 + src/roborock_local_server/server.py | 37 ++- tests/test_cloud.py | 10 + tests/test_native_schedule_sync.py | 235 +++++++++++++++ tests/test_routine_runner.py | 111 ++++++-- tests/test_schedule_runner.py | 101 +++++++ tests/test_schedules.py | 179 +++++++++++- tests/test_timer_rpc.py | 65 +++++ 19 files changed, 1487 insertions(+), 135 deletions(-) create mode 100644 docs/schedules.md create mode 100644 src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py create mode 100644 tests/test_native_schedule_sync.py diff --git a/docs/known_limitations.md b/docs/known_limitations.md index b7b8c59d..ebc6a20b 100644 --- a/docs/known_limitations.md +++ b/docs/known_limitations.md @@ -4,6 +4,6 @@ I have tried to copy over most functionality into this server, but some is not 1 - Vacuum camera live streaming — available; see the [model-specific results](camera.md#reported-camera-results). Bootstrap, activation and audio support vary by firmware, and some reports require custom builds. - Firmware Updates -- Schedules (Routines/Scenes do work though) +- Schedules are experimental. A scheduled start has been reported on an S7; broader hardware compatibility, DST parity, and expired one-time jobs still need validation. See [schedule support](schedules.md). Routines/Scenes are separate. - China-market devices have model/firmware-specific region and access-renewal restrictions. Unresolved a143/a146/a96 reports reject ordinary commands with `-10002 / rrcheck access denied` after local onboarding. See [known failures](tested_vacuums.md#known-failures). Factory-firmware a184 success was reported with custom interception and compromised-intermediate certificates; it does not establish support through the standard setup or resolve those other reports. - Some users report success with the official cloud through a China-exit VPN; see [this comment](https://github.com/iobroker-community-adapters/ioBroker.mihome-vacuum/issues/842#issuecomment-1736230673). A China VPN did not resolve the G30 local-server report [#161](https://github.com/Python-roborock/local_roborock_server/issues/161), so it is not an established general workaround here. diff --git a/docs/schedules.md b/docs/schedules.md new file mode 100644 index 00000000..06b67aee --- /dev/null +++ b/docs/schedules.md @@ -0,0 +1,137 @@ +# Experimental schedule support + +This implementation provides schedule storage, job APIs, time calculation, and dispatch. It does +not establish complete Roborock cloud compatibility on every model. + +## Responsibilities and compatibility + +Homebridge retains schedule switches, pause/delay decisions, original-time +restoration, daily reset, and recovery. The server provides the underlying APIs. + +| Operation | Behavior in this branch | +| --- | --- | +| GET `/user/devices/{duid}/jobs` | Stored jobs with calculated UTC `nextFireTime` for enabled, valid jobs | +| POST to the collection | Persist a new job with a unique numeric ID | +| PUT/PATCH `/user/devices/{duid}/jobs/{id}` | Verify native enablement when supplied, then save; preserve omitted fields and unknown model fields | +| DELETE an item | Remove the stored job and its occurrence record | +| `get/set/upd/del_server_timer` | Forward unchanged to the robot, which owns these timer entries | +| Legacy `get/set/upd/del_timer` | Local compatibility view for jobs without a native timer name | +| Native scheduled execution | Verify the robot timer is on, then send `server_scheduled_start` with the complete settings entry | + +A job's numeric `id` is different from the robot timer's string ID in +`param.params[0].name`. Imported or previously unsynchronized jobs can disagree with the native timer. +The server now reconciles enablement and still checks both before execution. +Native jobs are not duplicated in the legacy `get_timer` view; clients must read the jobs API to list native-linked jobs. + +Time updates accept the existing-job JSON PUT used by Homebridge, without +requiring `id` or `nextFireTime` in the body. If `param` is supplied, it replaces +the previous object exactly, so app removal of an optional field is preserved. +Settings such as `clean_order_mode`, `mop_template_id`, `map_index`, room-list +strings, and unknown future options survive storage and native dispatch. +Invalid cron/timezone/body values are rejected. A persistence failure does not +receive a successful write acknowledgement. + +## Enablement synchronization across clients + +Native schedule on/off changes are synchronized by the server, independent of +whether Homebridge, LocalRock, the OEM app, or the dashboard made the change. +A forwarded `upd/set/del_server_timer` request prompts a fresh native timer read. +The request and the robot's reply remain unchanged; sending a request or receiving +an `ok` is not used as proof of the final state. No client-specific HTTP write is +required after a native toggle. + +A background read also runs at startup and approximately every 30 seconds to +recover missed traffic, reconnects, and changes made directly over the LAN. +Successful reconciliation changes only `enabled` on existing, uniquely matched +jobs. It preserves IDs, cron/timezone, settings, unknown fields, and occurrence +claims. The dashboard's normal refresh shows the updated saved state. Other +clients see it on their next normal read; this is eventual synchronization, +not an unsolicited push into every app. + +An HTTP job PUT/PATCH that supplies `enabled` verifies the matching native timer +before saving. A timer already in the requested state needs no extra write, +so the OEM app's native-update-then-PUT sequence remains compatible. A lost +acknowledgement is checked with one read-back, without repeating the write. +Time-only requests that omit `enabled` remain store-only edits. + +Background reads and server edits share a per-vacuum lock. A native write that +overlaps a background read invalidates that read; edits/deletions during a read +are checked again before persistence. Missing, malformed, ambiguous or unreachable +timers leave the last saved state intact and never create replacement timers. +A normally polled, still-on timer does not re-enable a consumed one-time job. +An explicit native enable request can do so only after a fresh matching read; +that intent expires after two minutes. Unlinked server-only jobs retain their +existing behavior. Native timer time/settings edits are not imported into job +cron/settings by this enablement reconciliation. + +## Execution and missed runs + +The scheduler evaluates five-field cron expressions in each job's IANA timezone. +It checks approximately every 20 seconds, allowing less than 60 seconds of normal +dispatch latency. Startup establishes a fresh baseline; suspended/late ticks do +not drain historical jobs. Recurring jobs remain enabled for future occurrences. + +Before dispatch, the server saves an occurrence claim in the persistent inventory +under `schedule_occurrences`. A repeated tick, restart, or backward clock change +cannot dispatch the same claimed occurrence again. If that write fails, no start +is attempted. This is an at-most-once policy: a crash after claiming but before +sending can miss a clean. Run only one server process against an inventory; the +inventory lock does not coordinate multiple containers or external file editors. +Cloud re-import replaces schedule definitions, so back up local edits before +refreshing. Occurrence claims are retained across that refresh. + +A fresh native timer read must find exactly one matching `on` entry. Off, missing, +ambiguous, malformed, or unreachable timer state prevents a start. After that +network wait, dispatch rechecks the stored job and its deadline, so a concurrent +time edit or deletion cancels stale work. The remaining read-to-start race is +resolved by the robot's native command; real firmware behavior needs testing. + +A refused or timed-out native scheduled start is never retried as `app_start` or +`app_segment_clean`, and action-locked native starts are not automatically retried. +A robot refusal can mean a disabled/deleted timer; substituting an ordinary clean +could bypass that state. Legacy jobs retain upstream's separate cleaning-command +path; they do not establish native timer support on other firmware. + +## Provisional behavior and remaining validation + +- Native dispatch preserves the job's method and complete settings entry. + Validate room selection, map, cleaning order, mop mode and history on each model. +- DST policy currently skips nonexistent spring-forward times and runs only the + first occurrence of an ambiguous fall-back time. Automated tests cover this + policy; matching cloud behavior has not been established. +- One-time jobs are consumed when evaluated as due, including when dispatch + subsequently fails. Expired imported one-time jobs and extended downtime need + further investigation: a five-field cron has no year and can match again in a + later year. Begin hardware testing with recurring jobs. +- Test create/edit/delete through the actual app, disabled/missing native timers, + robot reboot, server/container restart, offline/reconnect, and busy/DND refusal. +- B01 and other scheduling protocols require separate model validation. + Scene/routine scheduling is separate from these device job routes. +- A startup log saying a job task was accepted is not proof of a clean; check + subsequent timer-read/start replies, robot status, and cleaning history. + +## First hardware test + +Build the selected Git commit into a versioned Docker image. Run `uv sync --locked --extra dev` +and `uv run pytest -q` to execute the Python test suite before deployment. + +1. Prepare the runtime configuration, DNS/certificate path, data backup, and + explicit cloud-to-local onboarding procedure. Start with one robot. +2. Import current jobs and verify every numeric job ID maps to the expected + native timer name. Confirm local jobs reads and robot timer reads separately. +3. Disable a recurring test schedule through Homebridge. Confirm the robot timer + reads off and the saved job becomes disabled without a separate HTTP update. + Re-enable it and confirm both states agree and a later occurrence runs once. +4. Test a room schedule and a whole-home schedule. Compare settings, rooms, map, + and history against the app. A refusal should stop dispatch, with no fallback + ordinary clean. +5. Exercise Homebridge delay and cancellation/restoration. Confirm only intended + times change, settings survive, and each resulting occurrence runs once. +6. Disconnect the robot across a due time and reconnect. Verify no replay, then + a future occurrence. Repeat with a server restart across the due time and + after a run. Test pending edits while a timer read is delayed. + +Record image tag/commit, model and firmware, timezone, expected time, actual time, +timer state, settings, and relevant sanitized replies for each result. Returning +to the cloud requires the appropriate onboarding transition; stopping Docker is +not an automatic cloud/local switch. diff --git a/mkdocs.yml b/mkdocs.yml index 7dd21531..b47b27cd 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -60,6 +60,7 @@ nav: - Reference: - Log Retention: logging.md - Known Limitations: known_limitations.md + - Schedule Support (Experimental): schedules.md - Scene Validity Reports: scene_validity.md - Tested Vacuums: tested_vacuums.md - Compatibility Report Details: compatibility_reports.md diff --git a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py index 1aa3ef82..20291c82 100644 --- a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py +++ b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py @@ -2,15 +2,16 @@ import json import re -import time from typing import Any +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError from shared.constants import DEFAULT_TIMEZONE from shared.context import ServerContext from shared.data_helpers import as_bool from shared.http_helpers import wrap_response -from shared.schedule_runner import CronError, parse_cron -from shared.schedule_store import delete_job, list_jobs, upsert_job +from shared.schedule_runner import parse_cron +from shared.schedule_store import SettingsError, delete_job, list_jobs, native_timer_name, upsert_job +from shared.native_schedule_sync import TimerSyncError, sync_timer from .service import device_jobs_payload @@ -45,7 +46,16 @@ def _json_body(body_params: dict[str, list[str]]) -> dict[str, Any]: continue if isinstance(parsed, dict): return parsed - return {} + raise ValueError("request body must be a JSON object") + + +def _validate_zone(value: Any) -> str: + name = str(value or "").strip() + try: + ZoneInfo(name) + except (ZoneInfoNotFoundError, ValueError) as exc: + raise ValueError("timeZoneId must be a valid IANA timezone") from exc + return name def _parse_param(value: Any) -> dict[str, Any] | None: @@ -68,26 +78,27 @@ def build_create( clean_path: str, ) -> dict[str, Any]: duid = _COLLECTION_RE.fullmatch(clean_path.rstrip("/")).group(1) # type: ignore[union-attr] - body = _json_body(body_params) - cron = str(body.get("cron") or "").strip() try: + body = _json_body(body_params) + cron = str(body.get("cron") or "").strip() parse_cron(cron) - except CronError as exc: - return _error(400, f"invalid cron: {exc}") + zone = _validate_zone(body.get("timeZoneId", getattr(ctx, "timezone", "") or DEFAULT_TIMEZONE)) + except ValueError as exc: + return _error(400, str(exc)) param = _parse_param(body.get("param")) if body.get("param") is not None and param is None: return _error(400, "param must be a JSON object") - job_id = body.get("id") or int(time.time()) - created = upsert_job( - ctx, - duid, - job_id=job_id, - cron=cron, - enabled=as_bool(body.get("enabled", True), True), - time_zone=str(body.get("timeZoneId") or getattr(ctx, "timezone", "") or DEFAULT_TIMEZONE), - repeated=as_bool(body.get("repeated", True), True), - param=param, - ) + try: + created = upsert_job( + ctx, duid, job_id=body.get("id"), cron=cron, + enabled=as_bool(body.get("enabled", True), True), time_zone=zone, + repeated=as_bool(body.get("repeated", True), True), param=param, + extra_fields=body, create_only=True, + ) + except ValueError as exc: + return _error(409, str(exc)) + except OSError: + return _error(500, "unable to persist schedule") return wrap_response(created) @@ -95,38 +106,92 @@ def match_update(path: str, method: str = "GET") -> bool: return method.upper() in ("PUT", "PATCH") and bool(_ITEM_RE.fullmatch(path.rstrip("/"))) -def build_update( - ctx: ServerContext, - _query_params: dict[str, list[str]], - body_params: dict[str, list[str]], - clean_path: str, -) -> dict[str, Any]: - match_ = _ITEM_RE.fullmatch(clean_path.rstrip("/")) - duid, job_id = match_.group(1), match_.group(2) # type: ignore[union-attr] - existing = next((job for job in list_jobs(ctx, duid) if str(job.get("id")) == job_id), None) - if existing is None: - return _error(404, "schedule not found") - body = _json_body(body_params) - cron = str(body.get("cron") or existing.get("cron") or "").strip() +class ScheduleUpdateError(ValueError): + def __init__(self, code: int, message: str) -> None: + super().__init__(message) + self.code = code + + +def _prepare_update(ctx, duid, job_id, body_params): + matches = [job for job in list_jobs(ctx, duid) if str(job.get("id")) == job_id] + if not matches: + raise ScheduleUpdateError(404, "schedule not found") + if len(matches) != 1: + raise ScheduleUpdateError(409, "duplicate schedule IDs") + existing = matches[0] try: + body = _json_body(body_params) + cron = str(body.get("cron", existing.get("cron")) or "").strip() parse_cron(cron) - except CronError as exc: - return _error(400, f"invalid cron: {exc}") + zone = _validate_zone(body["timeZoneId"]) if "timeZoneId" in body else "" + except ValueError as exc: + raise ScheduleUpdateError(400, str(exc)) from exc param = _parse_param(body.get("param")) if "param" in body else None if "param" in body and param is None: - return _error(400, "param must be a JSON object") - updated = upsert_job( - ctx, - duid, - job_id=job_id, - cron=cron, + raise ScheduleUpdateError(400, "param must be a JSON object") + values = dict( + job_id=job_id, cron=cron, enabled=as_bool(body.get("enabled", existing.get("enabled", True)), True), - time_zone=str(body.get("timeZoneId") or ""), + time_zone=zone, repeated=as_bool(body["repeated"], True) if "repeated" in body else None, - param=param, - override_time_zone=bool(body.get("timeZoneId")), + param=param, override_time_zone=bool(zone), extra_fields=body, expected_job=existing, ) - return wrap_response(updated) + return existing, body, values + + +def _save_update(ctx, duid, values): + try: + return wrap_response(upsert_job(ctx, duid, **values)) + except ValueError as exc: + return _error(409, str(exc)) + except OSError: + return _error(500, "unable to persist schedule") + + +def build_update(ctx, _query_params, body_params, clean_path): + match_ = _ITEM_RE.fullmatch(clean_path.rstrip("/")) + duid, job_id = match_.group(1), match_.group(2) + try: + _, _, values = _prepare_update(ctx, duid, job_id, body_params) + except ScheduleUpdateError as exc: + return _error(exc.code, str(exc)) + return _save_update(ctx, duid, values) + + +async def build_update_synced(ctx, _query_params, body_params, clean_path, coordinator): + """Authenticated HTTP edits share the same native state as every MQTT client.""" + match_ = _ITEM_RE.fullmatch(clean_path.rstrip("/")) + duid, job_id = match_.group(1), match_.group(2) + async with coordinator.lock(duid): + try: + existing, body, values = _prepare_update(ctx, duid, job_id, body_params) + candidate = {**existing} + if values["param"] is not None: + candidate["param"] = values["param"] + name = native_timer_name(candidate) if "enabled" in body else None + + def check_current(): + jobs = list_jobs(ctx, duid) + if [job for job in jobs if str(job.get("id")) == job_id] != [existing]: + raise TimerSyncError("Schedule changed during timer verification; read it again", 409) + for other in jobs: + if str(other.get("id")) != job_id and native_timer_name(other) == name: + raise TimerSyncError("Multiple schedules reference this native timer", 409) + + if name is not None: + check_current() + await sync_timer(ctx, duid, name, values["enabled"], check_current=check_current) + check_current() + result = _save_update(ctx, duid, values) + if name is not None and not result.get("success"): + result["msg"] = "Robot timer was verified, but the schedule could not be saved; refresh before retrying" + return result + except ScheduleUpdateError as exc: + return _error(exc.code, str(exc)) + except SettingsError as exc: + return _error(409, str(exc)) + except TimerSyncError as exc: + return _error(exc.status, str(exc)) def match_delete(path: str, method: str = "GET") -> bool: @@ -141,6 +206,9 @@ def build_delete( ) -> dict[str, Any]: match_ = _ITEM_RE.fullmatch(clean_path.rstrip("/")) duid, job_id = match_.group(1), match_.group(2) # type: ignore[union-attr] - if not delete_job(ctx, duid, job_id): - return _error(404, "schedule not found") + try: + if not delete_job(ctx, duid, job_id): + return _error(404, "schedule not found") + except OSError: + return _error(500, "unable to persist schedule") return wrap_response({"id": job_id}) diff --git a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py index 5f4c4701..04642bf4 100644 --- a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py +++ b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py @@ -732,6 +732,8 @@ def device_jobs_payload(ctx: ServerContext, device_id: str) -> list[dict[str, An upcoming = next_fire_time_utc(payload, default_timezone=str(getattr(ctx, "timezone", "") or "")) if upcoming is not None: payload["nextFireTime"] = upcoming.strftime("%Y-%m-%dT%H:%M:%S.000+00:00") + else: + payload.pop("nextFireTime", None) schedules.append(payload) return schedules diff --git a/src/roborock_local_server/bundled_backend/import_web_inventory_from_cloud.py b/src/roborock_local_server/bundled_backend/import_web_inventory_from_cloud.py index ca095047..c371cfb4 100644 --- a/src/roborock_local_server/bundled_backend/import_web_inventory_from_cloud.py +++ b/src/roborock_local_server/bundled_backend/import_web_inventory_from_cloud.py @@ -9,6 +9,7 @@ import argparse import asyncio +import copy from dataclasses import asdict, is_dataclass from datetime import datetime, timezone import json @@ -764,6 +765,12 @@ def _merge_existing_inventory_mutations( merged = dict(cloud_inventory) + # Refreshing cloud definitions must not erase the local scheduler's record + # of occurrences already attempted (including a clean with a lost reply). + occurrences = existing_inventory.get("schedule_occurrences") + if isinstance(occurrences, dict): + merged["schedule_occurrences"] = copy.deepcopy(occurrences) + rooms = _merge_record_lists_by_identity( _inventory_rooms(merged), _inventory_rooms(existing_inventory), diff --git a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py index 1276126b..a21b890f 100644 --- a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py +++ b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py @@ -74,10 +74,12 @@ def __init__( runtime_state: Any | None = None, inventory_path: Path | None = None, request_interceptor: Callable[[str, bytes], bytes | None] | None = None, + request_observer: Callable[[str, bytes], None] | None = None, ) -> None: # Given (duid, payload) for each rr/m request; returns an encoded reply to # publish back to the requester instead of forwarding, or None to forward. self._request_interceptor = request_interceptor + self._request_observer = request_observer self._host = host self._port = port self._logger = logger @@ -433,6 +435,11 @@ async def _handle_cloud_message( self._d_to_m[device_topic] = cloud_topic target_topic = device_topic.topic_out + if self._request_observer is not None and not bool(getattr(message, "retain", False)): + try: + self._request_observer(cloud_topic.duid, bytes(message.payload)) + except Exception: # Observation must never prevent forwarding. + self._logger.warning("MQTT request observer failed for %s", src_topic, exc_info=True) await self._publish_mirror(client, message, target_topic) self._logger.info( "MQTT bridge m->d %s -> %s bytes=%d", diff --git a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py new file mode 100644 index 00000000..324e37dc --- /dev/null +++ b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py @@ -0,0 +1,268 @@ +"""Reconcile native schedule enablement independently of the client making a change.""" + +from __future__ import annotations + +import asyncio +from collections.abc import Callable +from enum import StrEnum +import logging +import time +from typing import Any + +from .inventory_io import load_inventory +from .schedule_store import SettingsError, list_jobs, native_timer_name, reconcile_native_enabled + +_LOGGER = logging.getLogger(__name__) + + +class TimerSyncError(RuntimeError): + def __init__(self, message: str, status: int = 503) -> None: + super().__init__(message) + self.status = status + + +class TimerCommand(StrEnum): + GET = "get_server_timer" + UPDATE = "upd_server_timer" + + +def _timer_state(timers: Any, name: str) -> str: + if not isinstance(timers, list): + raise TimerSyncError("The vacuum did not return a usable timer list. Refresh schedules and retry.") + matches = [entry for entry in timers if isinstance(entry, list) and entry and entry[0] == name] + if not matches: + raise TimerSyncError("The linked robot timer is missing. Check this schedule in the Roborock app before retrying.", 409) + if len(matches) != 1 or len(matches[0]) < 2 or matches[0][1] not in ("on", "off"): + raise TimerSyncError("The linked robot timer is ambiguous. Check this schedule in the Roborock app before retrying.", 409) + return matches[0][1] + + +async def sync_timer( + context: Any, duid: str, name: str, enabled: bool, *, check_current: Callable[[], None], +) -> str: + """Update only the named timer; a fresh read is the success criterion. + + The app sends upd_server_timer before its HTTP job update. A timed-out write + may still have reached the vacuum, so read it back without repeating the + write. Never create a timer or infer one from a cloud job's numeric ID. + """ + from shared.routine_runner import _RoutineMqttClient + + device = context.runtime_credentials.resolve_device(duid=duid) + if device is None or not device.get("localkey"): + raise TimerSyncError("Connect this vacuum to the local server before changing its schedule.") + desired = "on" if enabled else "off" + client = None + write_attempted = False + try: + async with asyncio.timeout(45): + client = _RoutineMqttClient(context, device, logging.getLogger(__name__)) + await client.connect() + current = _timer_state(await client.send_command(TimerCommand.GET, []), name) + # Recheck the job after the network read, before changing the robot. + check_current() + if current != desired: + write_attempted = True + try: + await client.send_command(TimerCommand.UPDATE, [[name, desired]]) + except Exception: + # A read-back can confirm a write whose acknowledgement was + # lost. It cannot confirm a refused or unapplied write. + pass + current = _timer_state(await client.send_command(TimerCommand.GET, []), name) + if current != desired: + raise TimerSyncError("The vacuum did not confirm the requested timer setting. Refresh schedules and retry.") + return current + except TimerSyncError: + raise + except Exception as exc: + message = ( + "The robot timer may have changed, but verification failed. Refresh schedules and retry." + if write_attempted else + "Could not read this vacuum's timer. Make sure it is connected locally, then retry." + ) + raise TimerSyncError(message) from exc + finally: + if client is not None: + try: + async with asyncio.timeout(5): + await client.close() + except Exception: + logging.getLogger(__name__).warning("Unable to close schedule timer connection") + + +def timer_states(timers: Any) -> dict[str, bool]: + """Ignore ambiguous/malformed entries; absence is not proof of disablement.""" + if not isinstance(timers, list): + raise TimerSyncError("The vacuum did not return a usable timer list") + states: dict[str, bool] = {} + seen: set[str] = set() + invalid: set[str] = set() + for entry in timers: + if not isinstance(entry, list) or not entry or not isinstance(entry[0], str): + continue + name = entry[0] + if name in seen or len(entry) < 2 or entry[1] not in ("on", "off"): + invalid.add(name) + else: + states[name] = entry[1] == "on" + seen.add(name) + return {name: enabled for name, enabled in states.items() if name not in invalid} + + +class NativeScheduleSync: + """One coordinator for API edits and read-only reconciliation from any client. + + Native RPCs still reach the robot unchanged. Observing a write only schedules + a fresh read; no optimistic request or generic 'ok' reply mutates inventory. + Periodic reads also catch LAN changes and recover after missed MQTT traffic. + """ + + INTERVAL = 30.0 + DEBOUNCE = 0.3 + + def __init__(self, context: Any) -> None: + self.context = context + self._locks: dict[str, asyncio.Lock] = {} + self._generation: dict[str, int] = {} + self._enable_intent: dict[str, dict[str, tuple[float, Any]]] = {} + self._dirty: set[str] = set() + self._wake = asyncio.Event() + self._slots = asyncio.Semaphore(2) + + def lock(self, duid: str) -> asyncio.Lock: + return self._locks.setdefault(duid, asyncio.Lock()) + + def changed(self, duid: str, *, enable_name: str | None = None) -> None: + self._generation[duid] = self._generation.get(duid, 0) + 1 + if enable_name is not None: + # Tie one-time re-enablement to the occurrence already consumed when + # the request was observed, never one consumed after this request. + claims = load_inventory(self.context).get("schedule_occurrences", {}).get(duid, {}) + for job in list_jobs(self.context, duid): + try: + matches = native_timer_name(job) == enable_name + except SettingsError: + continue + if matches: + self._enable_intent.setdefault(duid, {})[enable_name] = ( + time.monotonic() + 120, claims.get(str(job.get("id"))), + ) + self._dirty.add(duid) + self._wake.set() + + def observe_request(self, duid: str, payload: bytes) -> None: + from roborock.protocol import create_mqtt_decoder + from .timer_rpc import _request_body + + device = self.context.runtime_credentials.resolve_device(duid=duid) + if not device or not device.get("localkey"): + return + try: + messages = create_mqtt_decoder(device["localkey"])(payload) + for message in messages: + if int(getattr(message.protocol, "value", message.protocol)) != 101: + continue + request = _request_body(message) + if not request or request.get("method") not in { + "upd_server_timer", "set_server_timer", "del_server_timer", + }: + continue + enable_name = None + params = request.get("params") + if (request.get("method") == "upd_server_timer" and isinstance(params, list) + and len(params) == 1 and isinstance(params[0], list) + and len(params[0]) == 2 and isinstance(params[0][0], str)): + name, state = params[0] + # A later off must invalidate an earlier on intent, too. + self._enable_intent.setdefault(duid, {}).pop(name, None) + if state == "on": + enable_name = name + self.changed(duid, enable_name=enable_name) + except Exception: # Unrelated/unsupported frames must always pass through. + return + + async def refresh(self, duid: str) -> list[Any]: + from .routine_runner import _RoutineMqttClient + + async with self._slots, self.lock(duid): + snapshot = list_jobs(self.context, duid) + names = [] + for job in snapshot: + try: + name = native_timer_name(job) + except SettingsError: + continue + if name is not None: + names.append(name) + if not names: + return [] + device = self.context.runtime_credentials.resolve_device(duid=duid) + if not device or not device.get("localkey"): + return [] + generation = self._generation.get(duid, 0) + intents = {name: intent for name, intent in self._enable_intent.get(duid, {}).items() + if intent[0] > time.monotonic()} + self._enable_intent[duid] = intents + explicit_enable = {name: intent[1] for name, intent in intents.items()} + client = None + try: + async with asyncio.timeout(15): + client = _RoutineMqttClient(self.context, device, _LOGGER) + await client.connect() + states = timer_states(await client.send_command(TimerCommand.GET, [])) + if self._generation.get(duid, 0) != generation: + # A newer native write overlapped the read. A later pass + # will confirm it; do not apply this older snapshot. + self._dirty.add(duid) + self._wake.set() + return [] + changed = reconcile_native_enabled( + self.context, duid, snapshot, states, explicit_enable=explicit_enable, + ) + # A read can arrive before an outstanding command is applied. + # Keep a bounded enable intent until it is actually observed on. + self._enable_intent[duid] = {name: until for name, until in intents.items() + if states.get(name) is not True} + if changed: + _LOGGER.info("Synchronized native schedule enablement for %s: %d changed", duid, len(changed)) + return changed + except (OSError, ValueError): + _LOGGER.warning("Could not save native schedule reconciliation for %s", duid, exc_info=True) + return [] + except Exception: + # An offline/unsupported robot keeps its last saved state. + _LOGGER.debug("Native schedule read unavailable for %s", duid, exc_info=True) + return [] + finally: + if client is not None: + try: + async with asyncio.timeout(5): + await client.close() + except Exception: + _LOGGER.debug("Could not close native schedule read for %s", duid, exc_info=True) + + async def run(self, stop_event: asyncio.Event) -> None: + next_scan = 0.0 + while not stop_event.is_set(): + now = time.monotonic() + due = set(self._dirty) + self._dirty.clear() + self._wake.clear() + if now >= next_scan: + schedules = load_inventory(self.context).get("schedules", {}) + if isinstance(schedules, dict): + due.update(str(duid) for duid in schedules) + next_scan = now + self.INTERVAL + if due: + await asyncio.sleep(self.DEBOUNCE) + results = await asyncio.gather(*(self.refresh(duid) for duid in sorted(due)), return_exceptions=True) + for duid, result in zip(sorted(due), results): + if isinstance(result, Exception): + _LOGGER.warning("Native schedule reconciliation unavailable for %s: %s", duid, type(result).__name__) + if self._dirty: + continue + try: + await asyncio.wait_for(self._wake.wait(), timeout=max(0.1, next_scan - time.monotonic())) + except TimeoutError: + pass diff --git a/src/roborock_local_server/bundled_backend/shared/routine_runner.py b/src/roborock_local_server/bundled_backend/shared/routine_runner.py index ad408ca7..117164f4 100644 --- a/src/roborock_local_server/bundled_backend/shared/routine_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/routine_runner.py @@ -3,7 +3,9 @@ from __future__ import annotations import asyncio +import copy from dataclasses import dataclass, replace +from datetime import datetime, timezone from enum import StrEnum import json import logging @@ -33,6 +35,7 @@ def _ensure_local_python_roborock_on_path() -> None: from roborock.roborock_typing import RoborockCommand from .context import ServerContext +from .schedule_store import claimed_job_is_current __all__ = [ "RoutineCommand", @@ -68,6 +71,7 @@ class SchedulerCommand(StrEnum): # without the entry it answers -10005 "Invalid timer", and with a bare id "Param should be # name + fan + segment". SERVER_SCHEDULED_START = "server_scheduled_start" + GET_SERVER_TIMER = "get_server_timer" # A whole-home clean started by a schedule rather than a scene; see commands_for_step. @@ -92,7 +96,6 @@ class SchedulerCommand(StrEnum): # -10005 "Param error" to a value it does not support, such as a route its model lacks. _APP_START_KEYS = ("repeat", "water_box_mode", "segments", "fan_power", "mop_mode", "clean_mop", "map_index") _APP_START_DEFAULTS = {"repeat": 1, "segments": "0", "clean_mop": 1, "map_index": -1} -_SETTING_ENTRY_KEYS = ("repeat", "fan_power", "water_box_mode", "mop_mode", "mop_template_id") # app_start's "source" field is how the robot learns who started a clean, for its history. 3 is # python-roborock's RoborockStartType.schedule, and a robot recorded the clean as "Schedule" in the # official app's history; an app start is 2. "start_type", "startType", "start_from", "from" and @@ -415,8 +418,9 @@ def commands_for_step(step: RoutineStep) -> list[RoutineCommand]: ] if step.method == SCHEDULE_SERVER_START: entry = _single_start_entry(step) - body = {key: entry[key] for key in (*_APP_START_KEYS, "name") if key in entry} - return [RoutineCommand(SchedulerCommand.SERVER_SCHEDULED_START, [body])] + # Preserve the captured payload, including settings introduced by newer + # models. Filtering through app_start's keys loses schedule semantics. + return [RoutineCommand(SchedulerCommand.SERVER_SCHEDULED_START, [copy.deepcopy(entry)])] if step.method == SCHEDULE_APP_START: entry = _single_start_entry(step) body = {**_APP_START_DEFAULTS, **{key: entry[key] for key in _APP_START_KEYS if key in entry}} @@ -439,26 +443,11 @@ def commands_for_step(step: RoutineStep) -> list[RoutineCommand]: def fallback_commands_for_step(step: RoutineStep) -> list[RoutineCommand] | None: - """The longer way to run [step], for when the robot refuses the short one; None if there is none. + """Alternative settings transport for a legacy schedule, never a native timer. - A schedule is started by asking the robot to run it (server_scheduled_start), and failing that - by one app_start carrying its settings. A refused command changes nothing, so on "Param error" - or "Invalid timer" the next, plainer way is tried: a room schedule as a segment clean, a - whole-home one as separate setting commands, which is how a routine starts a whole-home clean. + A refused server_scheduled_start can mean the timer is disabled or gone. + An ordinary clean would bypass that decision, so native starts never fall back. """ - if step.method == SCHEDULE_SERVER_START: - entry = _single_start_entry(step) - rooms = [ - int(part) - for part in str(entry.get("segments") or "").replace(";", ",").split(",") - if part.strip().isdigit() and int(part) > 0 - ] - if rooms: - settings = {key: entry[key] for key in _SETTING_ENTRY_KEYS if key in entry} - return commands_for_step( - replace(step, method="do_scenes_segments", params={"data": [{**settings, "segs": rooms}]}) - ) - return commands_for_step(replace(step, method=SCHEDULE_APP_START)) if step.method != SCHEDULE_APP_START: return None return commands_for_step(replace(step, method="do_scenes_app_start")) @@ -893,6 +882,8 @@ def start_job( job_id: int, name: str, steps: list[RoutineStep], + expected_job: dict[str, Any] | None = None, + deadline: datetime | None = None, ) -> dict[str, Any]: """Run a scheduled job's steps; never interrupts a routine already running.""" existing = self._tasks_by_device.get(device_id) @@ -906,7 +897,8 @@ def start_job( } scene = {"id": job_id, "name": name, "device_id": device_id} task = asyncio.get_running_loop().create_task( - self._run_steps(scene=scene, steps=steps, sync_scene_tids=False), + self._run_steps(scene=scene, steps=steps, sync_scene_tids=False, + schedule_job=copy.deepcopy(expected_job), schedule_deadline=deadline), name=f"routine-job-{job_id}-{device_id}", ) self._tasks_by_device[device_id] = _ActiveRoutine(task=task, scene_id=job_id, scene_name=name) @@ -1059,6 +1051,7 @@ async def _send_step_command( except Exception as exc: # noqa: BLE001 if ( routine_command.command in _STEP_START_COMMANDS + and step.method != SCHEDULE_SERVER_START and attempts < _ACTION_LOCK_RETRY_ATTEMPTS and _is_retryable_action_locked_error(exc) ): @@ -1110,6 +1103,8 @@ async def _run_steps( scene: dict[str, Any], steps: list[RoutineStep], sync_scene_tids: bool, + schedule_job: dict[str, Any] | None = None, + schedule_deadline: datetime | None = None, ) -> None: device_id = scene_device_id(scene) device = self._device_record(device_id) @@ -1135,6 +1130,29 @@ async def _run_steps( if sync_scene_tids: await self._sync_scene_tids(client=client, scene=scene, device_id=device_id, logger=logger) for step_index, step in enumerate(steps): + if step.method == SCHEDULE_SERVER_START: + timer_name = _single_start_entry(step).get("name") + timers = await client.send_command(SchedulerCommand.GET_SERVER_TIMER, []) + # The cloud job's enabled field can remain true when the + # robot timer is off. A failed/malformed read must not start + # cleaning, nor enqueue a start for an offline robot. + if not isinstance(timers, list): + raise RoutineExecutionError("Cannot verify native schedule timer state") + matching = [entry for entry in timers if isinstance(entry, list) and len(entry) >= 2 + and entry[0] == timer_name] + if len(matching) != 1 or matching[0][1] != "on": + logger.info("Skipping schedule: robot timer is off, missing, or ambiguous") + continue + elif schedule_job is not None: + # Legacy jobs have no native flag to read, but still require + # a live robot before sending cleaning commands. + await client.get_status() + if schedule_job is not None and not claimed_job_is_current(self._context, device_id, schedule_job): + logger.info("Skipping schedule: job changed during dispatch") + return + if schedule_deadline is not None and datetime.now(timezone.utc) >= schedule_deadline: + logger.info("Skipping schedule: dispatch window expired") + return waits_for_step_complete = RoborockDataProtocol.TASK_COMPLETE.value in step.finish_dp_ids try: await self._send_step_commands( diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index 99259982..84c99440 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -8,6 +8,7 @@ from __future__ import annotations import asyncio +import copy from dataclasses import dataclass from datetime import date, datetime, timedelta, timezone, tzinfo import json @@ -25,7 +26,7 @@ RoutineStep, get_routine_runner, ) -from .schedule_store import set_enabled +from .schedule_store import claim_occurrence if TYPE_CHECKING: from .context import ServerContext @@ -33,6 +34,7 @@ _LOGGER = logging.getLogger(__name__) TICK_SECONDS = 20 +MAX_LATENESS_SECONDS = 60 _SUPPORTED_JOB_METHOD = "server_scheduled_start" _CARRIED_ENTRY_KEYS = ("repeat", "fan_power", "water_box_mode", "mop_mode", "mop_template_id") _START_ENTRY_KEYS = (*_CARRIED_ENTRY_KEYS, "segments", "clean_mop", "map_index") @@ -123,36 +125,36 @@ def parse_cron(expression: str) -> CronSpec: def next_fire(spec: CronSpec, after: datetime) -> datetime | None: - """First fire instant strictly after ``after`` (an aware datetime), in its timezone.""" + """Next wall-clock match; skip DST gaps and use only the first repeated hour. + + This explicit DST policy is provisional until compared with cloud behavior. + Compare UTC instants because local datetime arithmetic can hide DST folds. + """ zone = after.tzinfo if zone is None: raise ValueError("after must be timezone-aware") - start = after.replace(second=0, microsecond=0) + timedelta(minutes=1) - start_utc = start.astimezone(timezone.utc) - first_day = start.date() - for offset in range(0, 800): + after_utc = after.astimezone(timezone.utc) + first_day = after.date() + # Eight years also covers the leap-day gap across a non-leap century. + for offset in range(8 * 366): day = first_day + timedelta(days=offset) if not spec.day_matches(day): continue for hour in spec.hours: for minute in spec.minutes: candidate = datetime(day.year, day.month, day.day, hour, minute, tzinfo=zone) - if candidate.astimezone(timezone.utc) >= start_utc: + candidate_utc = candidate.astimezone(timezone.utc) + if candidate_utc.astimezone(zone).replace(tzinfo=None) != candidate.replace(tzinfo=None): + continue # Nonexistent wall-clock time during spring forward. + if candidate_utc > after_utc: return candidate return None def _zone(name: str, fallback: str) -> tzinfo: # DEFAULT_TIMEZONE is what /location tells the robot when none is configured. - for candidate in (name, fallback, DEFAULT_TIMEZONE): - candidate = str(candidate or "").strip() - if not candidate: - continue - try: - return ZoneInfo(candidate) - except (ZoneInfoNotFoundError, ValueError): - continue - return timezone.utc + candidate = str(name or fallback or DEFAULT_TIMEZONE).strip() + return ZoneInfo(candidate) def _segment_ids(value: Any) -> list[int]: @@ -193,6 +195,8 @@ def steps_for_job(job: dict[str, Any]) -> list[RoutineStep]: if method != _SUPPORTED_JOB_METHOD: raise RoutineExecutionError(f"unsupported schedule method {method!r}") params = param.get("params") + if isinstance(params, list) and len(params) != 1: + raise RoutineExecutionError("schedule params must contain exactly one settings object") entry = params[0] if isinstance(params, list) and params else params if not isinstance(entry, dict): raise RoutineExecutionError("schedule params must contain one settings object") @@ -203,6 +207,8 @@ def steps_for_job(job: dict[str, Any]) -> list[RoutineStep]: # The robot-side timer id the official app (or LocalRock) saved this schedule under. A schedule # that has one is run the way the cloud runs it: the robot is asked to start that timer. timer_id = entry.get("name") + if "name" in entry and (not isinstance(timer_id, str) or not timer_id.strip()): + raise RoutineExecutionError("native schedule timer name must be a nonempty string") name = str(entry.get("name") or job.get("name") or "").strip() if not name or name.isdigit(): name = f"Schedule {job.get('id', '')}".strip() @@ -212,7 +218,7 @@ def steps_for_job(job: dict[str, Any]) -> list[RoutineStep]: step_id=1, name=name, method=SCHEDULE_SERVER_START, - params=[{**start_settings, "name": timer_id.strip()}], + params=[copy.deepcopy(entry)], finish_dp_ids=(), ) ] @@ -243,9 +249,9 @@ def next_fire_time_utc(job: dict[str, Any], *, default_timezone: str = "", now: return None try: spec = parse_cron(str(job.get("cron") or "")) - except CronError: + zone = _zone(str(job.get("timeZoneId") or ""), default_timezone) + except (CronError, ZoneInfoNotFoundError, ValueError): return None - zone = _zone(str(job.get("timeZoneId") or ""), default_timezone) current = (now or datetime.now(timezone.utc)).astimezone(zone) upcoming = next_fire(spec, current) return upcoming.astimezone(timezone.utc) if upcoming is not None else None @@ -258,55 +264,65 @@ def __init__(self, context: ServerContext, *, default_timezone: str = "") -> Non self._last_tick: datetime | None = None self._logger = context.loggers.get("api") or context.loggers.get("unknown") or _LOGGER - def due_jobs(self, since: datetime, until: datetime) -> list[tuple[str, dict[str, Any]]]: + def due_jobs(self, since: datetime, until: datetime) -> list[tuple[str, dict[str, Any], datetime]]: inventory = load_inventory(self._context) schedules = inventory.get("schedules") if not isinstance(schedules, dict): return [] - due: list[tuple[str, dict[str, Any]]] = [] + due: list[tuple[str, dict[str, Any], datetime]] = [] for device_id, jobs in schedules.items(): if not isinstance(jobs, list): continue for job in jobs: if not isinstance(job, dict) or not as_bool(job.get("enabled", True), True): continue + if job.get("id") in (None, ""): + self._logger.warning("Skipping schedule without an id on %s", device_id) + continue try: spec = parse_cron(str(job.get("cron") or "")) - except CronError as exc: + zone = _zone(str(job.get("timeZoneId") or ""), self._default_timezone) + except (CronError, ZoneInfoNotFoundError, ValueError) as exc: self._logger.warning("Skipping schedule %s on %s: %s", job.get("id"), device_id, exc) continue - zone = _zone(str(job.get("timeZoneId") or ""), self._default_timezone) upcoming = next_fire(spec, since.astimezone(zone)) if upcoming is not None and upcoming.astimezone(timezone.utc) <= until: - due.append((str(device_id), job)) + due.append((str(device_id), job, upcoming)) return due def tick(self, now: datetime | None = None) -> list[dict[str, Any]]: """Fire anything that came due since the previous tick. Needs a running event loop.""" current = (now or datetime.now(timezone.utc)).astimezone(timezone.utc) previous = self._last_tick + if previous is not None and current <= previous: + return [] self._last_tick = current if previous is None: return [] results: list[dict[str, Any]] = [] runner = get_routine_runner(self._context) - for device_id, job in self.due_jobs(previous, current): + # Never drain a backlog after suspension or an event-loop stall. Startup + # establishes a fresh baseline; normal ticks get a bounded grace period. + previous = max(previous, current - timedelta(seconds=MAX_LATENESS_SECONDS)) + for device_id, job, due in self.due_jobs(previous, current): job_id = job.get("id") try: steps = steps_for_job(job) + if not claim_occurrence(self._context, device_id, job, due): + continue result = runner.start_job( device_id=device_id, job_id=int(job_id) if str(job_id).isdigit() else 0, name=steps[0].name, steps=steps, + expected_job=job, + deadline=due.astimezone(timezone.utc) + timedelta(seconds=MAX_LATENESS_SECONDS), ) - except (RoutineExecutionError, ValueError) as exc: + except (RoutineExecutionError, ValueError, OSError) as exc: self._logger.warning("Schedule %s on %s not started: %s", job_id, device_id, exc) result = {"accepted": False, "status": "error", "error": str(exc), "deviceId": device_id} else: self._logger.info("Schedule %s on %s fired: %s", job_id, device_id, result.get("status")) - if result.get("accepted") and not as_bool(job.get("repeated", True), True): - set_enabled(self._context, device_id, job_id, False) results.append({"jobId": job_id, **result}) return results diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_store.py b/src/roborock_local_server/bundled_backend/shared/schedule_store.py index 8426e54e..e59a09f6 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_store.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_store.py @@ -3,8 +3,12 @@ from __future__ import annotations import copy +from datetime import datetime +import json +import time from typing import TYPE_CHECKING, Any +from .data_helpers import as_bool from .inventory_io import inventory_mutation, load_inventory, write_inventory if TYPE_CHECKING: @@ -50,18 +54,103 @@ def _normalize_setting(key: str, value: Any) -> Any: raise SettingsError(f"{key} must be a number, got {value!r}") from exc +def _settings_entry(job: dict[str, Any]) -> dict[str, Any]: + param = job.get("param") + if isinstance(param, str): + try: + param = json.loads(param) + except json.JSONDecodeError: + return {} + params = param.get("params") if isinstance(param, dict) else None + return params[0] if isinstance(params, list) and params and isinstance(params[0], dict) else {} + + +def has_native_timer(job: dict[str, Any]) -> bool: + return "name" in _settings_entry(job) + + +def native_timer_name(job: dict[str, Any]) -> str | None: + """Return an exact native reference, refusing malformed or multiple references.""" + param = job.get("param") + if isinstance(param, str): + try: + param = json.loads(param) + except (ValueError, TypeError): + return None + if not isinstance(param, dict) or param.get("method") != "server_scheduled_start": + return None + params = param.get("params") + if isinstance(params, list): + if len(params) != 1: + if any(isinstance(entry, dict) and "name" in entry for entry in params): + raise SettingsError("Schedule has multiple or ambiguous native timer references") + return None + params = params[0] + if not isinstance(params, dict) or "name" not in params: + return None + name = params["name"] + if not isinstance(name, str) or not name.strip(): + raise SettingsError("Schedule has an invalid native timer reference") + return name + + +@inventory_mutation +def reconcile_native_enabled( + ctx: ServerContext, duid: str, snapshot: list[dict[str, Any]], states: dict[str, bool], + *, explicit_enable: dict[str, Any] | None = None, +) -> list[Any]: + """Merge a fresh robot read only into unchanged, uniquely linked jobs. + + Preserve complete records and occurrence claims. A still-on robot timer + must not resurrect a consumed one-time job after a restart or routine poll. + """ + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=False) or [] + by_name: dict[str, list[dict[str, Any]]] = {} + for job in jobs: + if not isinstance(job, dict): + continue + try: + name = native_timer_name(job) + except SettingsError: + continue + if name is not None: + by_name.setdefault(name, []).append(job) + changed = [] + claims = inventory.get("schedule_occurrences", {}).get(duid, {}) + for name, linked in by_name.items(): + if len(linked) != 1 or name not in states: + continue + job = linked[0] + before = [entry for entry in snapshot if _same_id(entry, job.get("id"))] + if before != [job] or sum(_same_id(entry, job.get("id")) for entry in jobs) != 1: + continue + enabled = states[name] + if as_bool(job.get("enabled", True), True) == enabled: + continue + if (enabled and not as_bool(job.get("repeated", True), True) + and str(job.get("id")) in claims + and (name not in (explicit_enable or {}) + or claims[str(job.get("id"))] != explicit_enable[name])): + continue + job["enabled"] = enabled + job.pop("nextFireTime", None) + changed.append(job["id"]) + if changed: + _persist(ctx, inventory) + return changed + + def job_settings(job: dict[str, Any]) -> dict[str, Any]: """The vac/mop settings a job carries, as a flat dict. ``name``, when the job has one, rides along too, outside ``JOB_SETTING_KEYS``: it is not a vac/mop setting a client edits, it is the official app's own link from this job to the robot's server_timer entry it saved alongside it (POST/PUT /user/devices/{duid}/jobs carries - it in exactly this spot, confirmed live). A client that reads the job back through get_timer - needs it to find the robot's entry to edit or delete. + it in exactly this spot, confirmed live). Native timer entries themselves are + read from the robot; this helper does not turn them into legacy timers. """ - param = job.get("param") - params = param.get("params") if isinstance(param, dict) else None - entry = params[0] if isinstance(params, list) and params and isinstance(params[0], dict) else {} + entry = _settings_entry(job) settings = {key: entry[key] for key in JOB_SETTING_KEYS if key in entry} name = entry.get("name") if isinstance(name, str) and name: @@ -120,6 +209,11 @@ def list_jobs(ctx: ServerContext, duid: str) -> list[dict[str, Any]]: return [copy.deepcopy(job) for job in jobs if isinstance(job, dict)] +def _persist(ctx: ServerContext, inventory: dict[str, Any]) -> None: + if not write_inventory(ctx, inventory): + raise OSError("Unable to persist schedule inventory") + + @inventory_mutation def upsert_job( ctx: ServerContext, @@ -132,6 +226,9 @@ def upsert_job( repeated: bool | None = None, param: dict[str, Any] | None = None, override_time_zone: bool = False, + extra_fields: dict[str, Any] | None = None, + create_only: bool = False, + expected_job: dict[str, Any] | None = None, ) -> dict[str, Any]: """Create the job, or update cron/enabled on an existing one while keeping its settings. @@ -140,7 +237,15 @@ def upsert_job( inventory = load_inventory(ctx) jobs = _jobs_for(inventory, duid, create=True) assert jobs is not None + if job_id is None: + job_id = time.time_ns() // 1_000_000 + while any(_same_id(job, job_id) for job in jobs): + job_id += 1 existing = next((job for job in jobs if _same_id(job, job_id)), None) + if create_only and existing is not None: + raise ValueError("schedule id already exists") + if expected_job is not None and existing != expected_job: + raise ValueError("schedule changed or was deleted; read it again before updating") if existing is None: existing = { "id": _coerce_id(job_id), @@ -160,7 +265,12 @@ def upsert_job( existing["timeZoneId"] = time_zone existing["cron"] = cron existing["enabled"] = enabled - write_inventory(ctx, inventory) + # Preserve model-specific job fields without allowing a body to replace the + # path ID, validated fields, or the server's calculated next-fire value. + reserved = {"id", "nextFireTime", "cron", "enabled", "timeZoneId", "repeated", "param"} + existing.update(copy.deepcopy({key: value for key, value in (extra_fields or {}).items() if key not in reserved})) + existing.pop("nextFireTime", None) + _persist(ctx, inventory) return copy.deepcopy(existing) @@ -174,7 +284,7 @@ def set_enabled(ctx: ServerContext, duid: str, job_id: Any, enabled: bool) -> bo job["enabled"] = enabled found = True if found: - write_inventory(ctx, inventory) + _persist(ctx, inventory) return found @@ -188,5 +298,40 @@ def delete_job(ctx: ServerContext, duid: str, job_id: Any) -> bool: if len(remaining) == len(jobs): return False jobs[:] = remaining - write_inventory(ctx, inventory) + occurrences = inventory.get("schedule_occurrences", {}).get(duid, {}) + occurrences.pop(str(job_id), None) + _persist(ctx, inventory) + return True + + +@inventory_mutation +def claim_occurrence(ctx: ServerContext, duid: str, job: dict[str, Any], due: datetime) -> bool: + """Persist an at-most-once decision before any robot command is sent. + + A crash after this write can skip a clean; replaying a clean after restart is + deliberately avoided. Compare the snapshot under the same lock as CRUD so a + disabled, edited, or deleted job cannot be claimed from a stale tick. + """ + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=False) or [] + current = next((item for item in jobs if _same_id(item, job.get("id"))), None) + if current != job or not as_bool(job.get("enabled", True), True): + return False + occurrences = inventory.setdefault("schedule_occurrences", {}).setdefault(duid, {}) + key = str(job["id"]) + instant = int(due.timestamp()) + if occurrences.get(key, -1) >= instant: + return False + occurrences[key] = instant + if not as_bool(job.get("repeated", True), True): + current["enabled"] = False + _persist(ctx, inventory) return True + + +def claimed_job_is_current(ctx: ServerContext, duid: str, job: dict[str, Any]) -> bool: + """Recheck after network awaits; a time edit must cancel stale queued work.""" + expected = copy.deepcopy(job) + if not as_bool(job.get("repeated", True), True): + expected["enabled"] = False # Consumed atomically by claim_occurrence. + return any(item == expected for item in list_jobs(ctx, duid)) diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index 3d93ef3c..5129dac2 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -34,6 +34,7 @@ JOB_SETTING_KEYS, SettingsError, delete_job, + has_native_timer, job_settings, list_jobs, merge_job_settings, @@ -72,6 +73,10 @@ def device_cron(cron: str) -> str | None: def _timer_entry(job: dict[str, Any]) -> list[Any] | None: + # Captured a15/a27 cloud jobs have native server timers while get_timer is + # empty. Do not fabricate a second legacy timer for the same native job. + if has_native_timer(job): + return None cron = device_cron(str(job.get("cron") or "")) if cron is None: return None diff --git a/src/roborock_local_server/server.py b/src/roborock_local_server/server.py index 20e1139f..f94b6eff 100644 --- a/src/roborock_local_server/server.py +++ b/src/roborock_local_server/server.py @@ -55,7 +55,9 @@ from shared.inventory_io import atomic_write_inventory, inventory_transaction from shared.protocol_auth import ProtocolAuthStore from shared.schedule_runner import ScheduleRunner +from shared.native_schedule_sync import NativeScheduleSync from shared.timer_rpc import TimerRpcInterceptor +from https_server.routes.user.devices import jobs as schedule_jobs from https_server.routes.user.scene import validity as scene_validity from https_server.routes.auth.service import ( build_login_data_response, @@ -365,6 +367,7 @@ def __init__( self._http_server: ManagedFastApiServer | None = None self._renew_task: asyncio.Task[None] | None = None self._schedule_task: asyncio.Task[None] | None = None + self._schedule_sync_task: asyncio.Task[None] | None = None self._stop_event = asyncio.Event() self.certificate_manager = CertificateManager(config=config, paths=paths) @@ -488,6 +491,7 @@ def __init__( turn_ttl=self.config.turn.ttl, ) self.endpoint_rules = default_endpoint_rules() + self.schedule_sync = NativeScheduleSync(self.context) self.app = self._create_app() def _init_zone_ranges_store(self) -> ZoneRangesStore: @@ -1355,14 +1359,20 @@ async def _handle_roborock_request(self, request: Request) -> Response: ) return response - route_name, response_payload = resolve_route( - rules=self.endpoint_rules, - context=self.context, - clean_path=clean_path, - query_params=query_params, - body_params=body_params, - method=request.method, - ) + if schedule_jobs.match_update(clean_path, request.method): + route_name = "update_schedule" + response_payload = await schedule_jobs.build_update_synced( + self.context, query_params, body_params, clean_path, self.schedule_sync, + ) + else: + route_name, response_payload = resolve_route( + rules=self.endpoint_rules, + context=self.context, + clean_path=clean_path, + query_params=query_params, + body_params=body_params, + method=request.method, + ) entry["route"] = route_name if route_name == "fw_createca": # Persist metadata, but retain the exact credential response sent to firmware. @@ -2011,6 +2021,7 @@ async def start(self) -> None: logger=self.loggers["mqtt"], runtime_state=self.runtime_state, inventory_path=self.paths.inventory_path, + request_observer=self.schedule_sync.observe_request, request_interceptor=TimerRpcInterceptor( self.context, default_timezone=self.config.network.timezone, @@ -2045,6 +2056,9 @@ async def start(self) -> None: self._renew_task = asyncio.create_task(self._renew_loop(), name="tls-renew-loop") schedule_runner = ScheduleRunner(self.context, default_timezone=self.config.network.timezone) + self._schedule_sync_task = asyncio.create_task( + self.schedule_sync.run(self._stop_event), name="native-schedule-sync", + ) self._schedule_task = asyncio.create_task( schedule_runner.run(self._stop_event), name="schedule-runner", @@ -2054,6 +2068,13 @@ async def stop(self) -> None: self._stop_event.set() self._turn_relay.stop() self._refresh_turn_health() + if self._schedule_sync_task is not None: + self._schedule_sync_task.cancel() + try: + await self._schedule_sync_task + except asyncio.CancelledError: + pass + self._schedule_sync_task = None if self._schedule_task is not None: self._schedule_task.cancel() try: diff --git a/tests/test_cloud.py b/tests/test_cloud.py index 98be0b26..dc2f9c56 100644 --- a/tests/test_cloud.py +++ b/tests/test_cloud.py @@ -182,6 +182,16 @@ def test_cloud_inventory_merge_preserves_local_rooms_and_scenes() -> None: assert merged["scene_order"] == [90, 10, 20] +def test_cloud_refresh_preserves_schedule_occurrence_claims(): + existing = {"schedule_occurrences": {"robot-test": {"42001": 1792789740}}} + cloud = {"schedules": {"robot-test": [{"id": 42001, "enabled": True}]}} + merged = _merge_existing_inventory_mutations(cloud, existing) + assert merged["schedule_occurrences"] == existing["schedule_occurrences"] + assert merged["schedules"] == cloud["schedules"] + merged["schedule_occurrences"]["robot-test"]["42001"] += 60 + assert existing["schedule_occurrences"]["robot-test"]["42001"] == 1792789740 + + def _make_manager(tmp_path: Path) -> CloudImportManager: return CloudImportManager( inventory_path=tmp_path / "inventory.json", diff --git a/tests/test_native_schedule_sync.py b/tests/test_native_schedule_sync.py new file mode 100644 index 00000000..3a7a53a2 --- /dev/null +++ b/tests/test_native_schedule_sync.py @@ -0,0 +1,235 @@ +"""Native state converges across clients without treating a sent command as success.""" + +import asyncio +import copy +import json +from pathlib import Path +import tempfile +from types import SimpleNamespace +import unittest +from unittest.mock import patch + +import roborock_local_server.backend # noqa: F401 (makes bundled shared modules importable) + +from shared import native_schedule_sync as sync +from shared import schedule_store as store + + +def job(enabled=True, *, job_id=701, name="native-1", repeated=True): + return { + "id": job_id, "enabled": enabled, "repeated": repeated, "cron": "0 9 * * *", + "timeZoneId": "America/Los_Angeles", "future": {"keep": [1, 2]}, + "param": {"method": "server_scheduled_start", "params": [{ + "name": name, "segments": "16,18", "map_index": 2, "future": {"keep": True}, + }]}, + } + + +class NativeStoreTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.path = Path(self.temp.name) / "web_api_inventory.json" + self.context = SimpleNamespace(http_jsonl=self.path.parent / "http.jsonl") + self.seed([job()]) + + def seed(self, jobs, claims=None): + self.path.write_text(json.dumps({ + "schedules": {"robot": jobs, "other": [job()]}, + "schedule_occurrences": {"robot": claims or {}}, "keep": {"metadata": 1}, + })) + + def test_reconciles_both_directions_and_preserves_identity_settings_and_other_devices(self): + for enabled in (False, True): + before = json.loads(self.path.read_text()) + snapshot = store.list_jobs(self.context, "robot") + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", snapshot, {"native-1": enabled}), [701]) + before["schedules"]["robot"][0]["enabled"] = enabled + self.assertEqual(json.loads(self.path.read_text()), before) + + def test_noop_does_not_write_inventory(self): + with patch.object(store, "write_inventory", side_effect=AssertionError("unnecessary write")): + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [job()], {"native-1": True}), []) + + def test_stale_read_cannot_overwrite_job_edited_while_reading_robot(self): + snapshot = store.list_jobs(self.context, "robot") + changed = job() + changed["cron"] = "30 9 * * *" + self.seed([changed]) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", snapshot, {"native-1": False}), []) + self.assertEqual(store.list_jobs(self.context, "robot"), [changed]) + + def test_deleted_job_is_not_recreated(self): + snapshot = store.list_jobs(self.context, "robot") + self.seed([]) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", snapshot, {"native-1": False}), []) + self.assertEqual(store.list_jobs(self.context, "robot"), []) + + def test_duplicate_timer_or_job_ids_are_never_guessed(self): + for jobs in ([job(), job(job_id=702)], [job(), job(name="native-2")]): + self.seed(jobs) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", jobs, {"native-1": False}), []) + self.assertEqual(store.list_jobs(self.context, "robot"), jobs) + + def test_missing_and_malformed_timer_entries_do_not_disable_jobs(self): + for timers in ([], [["native-1", "bad"]], [["native-1", "off"], ["native-1", "on"]], [["native-1"]]): + states = sync.timer_states(timers) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [job()], states), []) + + def test_serialized_and_object_params_keep_their_original_form(self): + for serialized in (False, True): + original = job() + if serialized: + original["param"] = json.dumps(original["param"]) + else: + original["param"]["params"] = original["param"]["params"][0] + self.seed([original]) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [original], {"native-1": False}), [701]) + self.assertEqual(store.list_jobs(self.context, "robot")[0]["param"], original["param"]) + + def test_consumed_one_time_job_is_not_resurrected_by_periodic_read(self): + original = job(False, repeated=False) + self.seed([original], {"701": 12345}) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [original], {"native-1": True}), []) + self.assertEqual(store.reconcile_native_enabled( + self.context, "robot", [original], {"native-1": True}, explicit_enable={"native-1": 12345}, + ), [701]) + self.assertEqual(json.loads(self.path.read_text())["schedule_occurrences"], {"robot": {"701": 12345}}) + + def test_earlier_enable_intent_does_not_resurrect_a_newly_consumed_occurrence(self): + original = job(False, repeated=False) + self.seed([original], {"701": 23456}) + self.assertEqual(store.reconcile_native_enabled( + self.context, "robot", [original], {"native-1": True}, explicit_enable={"native-1": 12345}, + ), []) + + def test_unlinked_server_only_jobs_are_unchanged(self): + original = job() + del original["param"]["params"][0]["name"] + self.seed([original]) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [original], {"701": False}), []) + + def test_persistence_failure_does_not_lose_inventory(self): + before = self.path.read_bytes() + with patch.object(store, "write_inventory", return_value=False): + with self.assertRaises(OSError): + store.reconcile_native_enabled(self.context, "robot", [job()], {"native-1": False}) + self.assertEqual(self.path.read_bytes(), before) + + +class NativeCoordinatorTests(unittest.IsolatedAsyncioTestCase): + async def asyncSetUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.path = Path(self.temp.name) / "web_api_inventory.json" + self.path.write_text(json.dumps({"schedules": {"robot": [job()]}})) + self.context = SimpleNamespace( + http_jsonl=self.path.parent / "http.jsonl", + runtime_credentials=SimpleNamespace(resolve_device=lambda **kw: {"duid": "robot", "localkey": "test"}), + ) + self.coordinator = sync.NativeScheduleSync(self.context) + self.robot = SimpleNamespace(timers=[["native-1", "off"]], calls=[], after_read=None, write_error=None, apply=True) + robot = self.robot + + class Client: + def __init__(self, *args): + pass + + async def connect(self): + robot.calls.append("connect") + + async def send_command(self, command, params): + robot.calls.append(str(command)) + if command == "get_server_timer": + result = copy.deepcopy(robot.timers) + if robot.after_read: + await robot.after_read() + return result + if robot.apply: + robot.timers[0][1] = params[0][1] + if robot.write_error: + raise robot.write_error + return ["ok"] + + async def close(self): + robot.calls.append("close") + + self.network = patch.dict("sys.modules", {"shared.routine_runner": SimpleNamespace(_RoutineMqttClient=Client)}) + self.network.start() + self.addCleanup(self.network.stop) + + async def test_external_toggle_reconciliation_reads_only_and_closes_connection(self): + self.assertEqual(await self.coordinator.refresh("robot"), [701]) + self.assertFalse(store.list_jobs(self.context, "robot")[0]["enabled"]) + self.assertEqual(self.robot.calls, ["connect", "get_server_timer", "close"]) + + async def test_later_native_write_invalidates_an_in_flight_read(self): + async def later(): + self.coordinator.changed("robot") + self.robot.timers[0][1] = "on" + self.robot.after_read = later + self.assertEqual(await self.coordinator.refresh("robot"), []) + self.assertTrue(store.list_jobs(self.context, "robot")[0]["enabled"]) + self.robot.after_read = None + self.assertEqual(await self.coordinator.refresh("robot"), []) + + async def test_dashboard_or_http_edit_lock_prevents_background_race(self): + async with self.coordinator.lock("robot"): + pending = asyncio.create_task(self.coordinator.refresh("robot")) + await asyncio.sleep(0) + self.assertEqual(self.robot.calls, []) + self.assertFalse(pending.done()) + self.assertEqual(await pending, [701]) + + async def test_lost_write_ack_requires_a_fresh_matching_read(self): + self.robot.write_error = TimeoutError() + self.assertEqual(await sync.sync_timer(self.context, "robot", "native-1", True, check_current=lambda: None), "on") + self.assertEqual(self.robot.calls.count("upd_server_timer"), 1) + self.assertEqual(self.robot.calls.count("get_server_timer"), 2) + + async def test_unapplied_write_is_not_success(self): + self.robot.apply = False + with self.assertRaises(sync.TimerSyncError): + await sync.sync_timer(self.context, "robot", "native-1", True, check_current=lambda: None) + self.assertEqual(self.robot.calls.count("upd_server_timer"), 1) + + async def test_matching_timer_needs_no_second_write(self): + self.assertEqual(await sync.sync_timer(self.context, "robot", "native-1", False, check_current=lambda: None), "off") + self.assertNotIn("upd_server_timer", self.robot.calls) + + async def test_startup_and_periodic_reads_recover_changes_without_observed_requests(self): + self.coordinator.INTERVAL = 0.02 + self.coordinator.DEBOUNCE = 0 + stop = asyncio.Event() + task = asyncio.create_task(self.coordinator.run(stop)) + try: + for enabled in (False, True): + self.robot.timers[0][1] = "on" if enabled else "off" + async with asyncio.timeout(1): + while store.list_jobs(self.context, "robot")[0]["enabled"] != enabled: + await asyncio.sleep(0.001) + finally: + stop.set() + task.cancel() + try: + await task + except asyncio.CancelledError: + pass + self.assertNotIn("upd_server_timer", self.robot.calls) + + async def test_cancellation_closes_an_in_flight_read(self): + waiting = asyncio.Event() + async def pause(): + waiting.set() + await asyncio.Event().wait() + self.robot.after_read = pause + task = asyncio.create_task(self.coordinator.refresh("robot")) + await waiting.wait() + task.cancel() + with self.assertRaises(asyncio.CancelledError): + await task + self.assertEqual(self.robot.calls[-1], "close") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_routine_runner.py b/tests/test_routine_runner.py index 01ca4da9..0add6380 100644 --- a/tests/test_routine_runner.py +++ b/tests/test_routine_runner.py @@ -14,6 +14,7 @@ SCHEDULE_SERVER_START, RoutineRunner, RoutineStep, + RoutineExecutionError, parse_scene_steps, ) from roborock.roborock_typing import RoborockCommand @@ -905,6 +906,8 @@ async def close(self) -> None: async def send_command(self, command, params=None): sent_commands.append((command, params)) + if command == routine_runner_module.SchedulerCommand.GET_SERVER_TIMER: + return [[step.params[0]["name"], "on", 1]] if refuse_single_start_with is not None and command == RoborockCommand.APP_START and params is not None: raise RoborockException(refuse_single_start_with) return ["ok"] @@ -980,6 +983,8 @@ async def close(self) -> None: async def send_command(self, command, params=None): sent_commands.append((command, params)) + if command == routine_runner_module.SchedulerCommand.GET_SERVER_TIMER: + return [[step.params[0]["name"], "on", 1]] if command == routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START: raise RoborockException(error) return ["ok"] @@ -990,7 +995,9 @@ async def wait_for_step_complete(self) -> None: monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", FakeRoutineClient) scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") runner = RoutineRunner(_test_context(tmp_path)) - return sent_commands, asyncio.run(runner._run_steps(scene=scene, steps=[step], sync_scene_tids=False)) + with pytest.raises(RoborockException): + asyncio.run(runner._run_steps(scene=scene, steps=[step], sync_scene_tids=False)) + return sent_commands def _server_start_step(**entry) -> RoutineStep: @@ -1002,38 +1009,100 @@ def test_a_server_start_asks_the_robot_to_run_the_schedule_with_its_whole_entry( # confirmed live to start a clean the robot records as "Schedules | Rooms". step = _server_start_step( repeat=1, water_box_mode=202, segments="16,17", fan_power=102, mop_mode=300, - clean_mop=1, map_index=0, name="1792336598179", + clean_mop=1, map_index=0, name="1792336598179", clean_order_mode=0, + mop_template_id=42, future_model_option={"mode": 7}, ) sent, _ = _run_schedule_step(tmp_path, monkeypatch, step, refuse_single_start_with=None) - assert [command for command, _ in sent] == [routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START] - assert sent[0][1] == [ + assert [command for command, _ in sent] == [ + routine_runner_module.SchedulerCommand.GET_SERVER_TIMER, + routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START, + ] + assert sent[1][1] == [ {"repeat": 1, "water_box_mode": 202, "segments": "16,17", "fan_power": 102, "mop_mode": 300, - "clean_mop": 1, "map_index": 0, "name": "1792336598179"} + "clean_mop": 1, "map_index": 0, "name": "1792336598179", "clean_order_mode": 0, + "mop_template_id": 42, "future_model_option": {"mode": 7}} ] -def test_a_refused_server_start_falls_back_to_a_segment_clean_for_a_room_schedule(tmp_path: Path, monkeypatch) -> None: +@pytest.mark.parametrize("segments", ["16,17", "0"]) +@pytest.mark.parametrize("code", [-10005, -10003, -10007]) +def test_a_refused_server_start_never_falls_back_or_retries(tmp_path: Path, monkeypatch, segments, code) -> None: # "Invalid timer" (the robot no longer holds that timer) is a -10005 like any bad parameter. - step = _server_start_step(fan_power=102, water_box_mode=202, segments="16,17", name="gone") - sent, _ = _run_schedule_step_refusing(tmp_path, monkeypatch, step, {"code": -10005, "message": "Invalid timer"}) + step = _server_start_step(fan_power=102, water_box_mode=202, segments=segments, name="gone") + sent = _run_schedule_step_refusing(tmp_path, monkeypatch, step, {"code": code, "message": "Refused"}) commands = [command for command, _ in sent] - assert commands[0] == routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START - assert commands[-1] == RoborockCommand.APP_SEGMENT_CLEAN - assert sent[-1][1] == [{"segments": [16, 17], "repeat": 1}] + assert commands == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER, + routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START] -def test_a_refused_server_start_falls_back_to_the_single_app_start_for_a_whole_home_schedule( - tmp_path: Path, monkeypatch +@pytest.mark.parametrize("timers", [[], [["timer", "off", 0]], [["different", "on", 1]], + [["timer", "on", 1], ["timer", "off", 0]]]) +def test_a_native_schedule_requires_one_enabled_robot_timer( + tmp_path: Path, monkeypatch, timers ) -> None: - step = _server_start_step(fan_power=102, water_box_mode=202, segments="0", name="gone") - sent, _ = _run_schedule_step_refusing(tmp_path, monkeypatch, step, {"code": -10005, "message": "Invalid timer"}) + sent = [] + class Client: + def __init__(self, *args): pass + async def connect(self): pass + async def close(self): pass + async def send_command(self, command, params=None): + sent.append(command) + return timers + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", Client) + runner = RoutineRunner(_test_context(tmp_path)) + scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") + asyncio.run(runner._run_steps(scene=scene, steps=[_server_start_step(name="timer")], sync_scene_tids=False)) + assert sent == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER] - assert sent[0][0] == routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START - assert sent[1] == ( - RoborockCommand.APP_START, - [{"repeat": 1, "segments": "0", "clean_mop": 1, "map_index": -1, "fan_power": 102, - "water_box_mode": 202, "source": 3}], - ) + +def test_offline_timer_read_never_publishes_a_start(tmp_path, monkeypatch): + sent = [] + closed = [] + class Client: + def __init__(self, *args): pass + async def connect(self): pass + async def close(self): closed.append(True) + async def send_command(self, command, params=None): + sent.append(command) + raise RoutineExecutionError("timed out") + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", Client) + runner = RoutineRunner(_test_context(tmp_path)) + scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") + with pytest.raises(RoutineExecutionError, match="timed out"): + asyncio.run(runner._run_steps(scene=scene, steps=[_server_start_step(name="timer")], sync_scene_tids=False)) + assert sent == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER] + assert closed == [True] + + +@pytest.mark.parametrize("change", ["edit", "disable", "delete", "expire"]) +def test_dispatch_rechecks_job_and_deadline_after_waiting_for_the_robot(tmp_path, monkeypatch, change): + from datetime import datetime, timedelta, timezone + from shared import schedule_store + ctx = _test_context(tmp_path) + duid = "6HL2zfniaoYYV01CkVuhkO" + job = schedule_store.upsert_job(ctx, duid, job_id=1, cron="0 9 * * *", enabled=True, + param={"method": "server_scheduled_start", "params": [{"name": "timer"}]}) + sent = [] + class Client: + def __init__(self, *args): pass + async def connect(self): pass + async def close(self): pass + async def send_command(self, command, params=None): + sent.append(command) + if change == "edit": + schedule_store.upsert_job(ctx, duid, job_id=1, cron="0 10 * * *", enabled=True) + elif change == "disable": + schedule_store.set_enabled(ctx, duid, 1, False) + elif change == "delete": + schedule_store.delete_job(ctx, duid, 1) + return [["timer", "on", 1]] + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", Client) + deadline = datetime.now(timezone.utc) + timedelta(seconds=-1 if change == "expire" else 60) + runner = RoutineRunner(ctx) + asyncio.run(runner._run_steps(scene=_scene(scene_id=1, device_id=duid, name="Schedule 1"), + steps=[_server_start_step(name="timer")], sync_scene_tids=False, + schedule_job=job, schedule_deadline=deadline)) + assert sent == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER] diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py index 2ee5f098..a9331056 100644 --- a/tests/test_schedule_runner.py +++ b/tests/test_schedule_runner.py @@ -184,3 +184,104 @@ def test_next_fire_time_utc_is_none_when_disabled(): now = _utc(2026, 9, 28, 12, 0) assert sr.next_fire_time_utc(_job(), now=now) == _utc(2026, 9, 28, 15, 0) assert sr.next_fire_time_utc(_job(enabled=False), now=now) is None + + +def test_native_settings_survive_dispatch_preparation_without_filtering_or_mutation(): + job = _job() + entry = job["param"]["params"][0] + entry.update(name="timer-42", clean_order_mode=0, mop_template_id=7, future_option={"x": [1, 2]}) + step = sr.steps_for_job(job)[0] + assert step.params == [entry] + step.params[0]["future_option"]["x"].append(3) + assert entry["future_option"] == {"x": [1, 2]} + + +@pytest.mark.parametrize("timer_name", [None, "", " ", 123]) +def test_invalid_native_timer_name_cannot_turn_into_an_ordinary_start(timer_name): + job = _job() + job["param"]["params"][0]["name"] = timer_name + with pytest.raises(RoutineExecutionError, match="timer name"): + sr.steps_for_job(job) + + +def test_observed_weekday_next_fire_advances_to_monday_after_offline_occurrence(): + job = _job(cron="09 14 ? * 1,2,3,4,5", timeZoneId="America/Los_Angeles") + assert sr.next_fire_time_utc(job, now=_utc(2026, 10, 23, 21, 7)) == _utc(2026, 10, 23, 21, 9) + assert sr.next_fire_time_utc(job, now=_utc(2026, 10, 23, 21, 10)) == _utc(2026, 10, 26, 21, 9) + + +@pytest.mark.parametrize("cron,after,expected", [ + ("30 2 * * *", _utc(2026, 3, 8, 9, 59), _utc(2026, 3, 9, 9, 30)), + ("30 1 * * *", _utc(2026, 11, 1, 8, 29), _utc(2026, 11, 1, 8, 30)), + ("30 1 * * *", _utc(2026, 11, 1, 8, 30), _utc(2026, 11, 2, 9, 30)), + ("30 1 * * *", _utc(2026, 11, 1, 9, 20), _utc(2026, 11, 2, 9, 30)), + ("0 0 * * 0", _utc(2026, 10, 25, 6, 59), _utc(2026, 10, 25, 7, 0)), + ("0 0 29 2 ?", _utc(2024, 3, 1), _utc(2028, 2, 29, 8, 0)), +]) +def test_timezone_and_provisional_dst_policy(cron, after, expected): + job = _job(cron=cron, timeZoneId="America/Los_Angeles") + assert sr.next_fire_time_utc(job, now=after) == expected + + +def test_invalid_explicit_timezone_never_runs_in_a_fallback_zone(tmp_path, monkeypatch): + job = _job(timeZoneId="Invalid/Zone") + assert sr.next_fire_time_utc(job, default_timezone="UTC", now=_utc(2026, 9, 28, 14)) is None + runner, _, fake = _runner_with_inventory(tmp_path, monkeypatch, [job]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert fake.started == [] + + +def test_missed_occurrence_is_skipped_and_future_schedule_stays_enabled(tmp_path, monkeypatch): + runner, ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + assert runner.tick(_utc(2026, 9, 28, 15, 2)) == [] + restarted = sr.ScheduleRunner(ctx) + assert restarted.tick(_utc(2026, 9, 28, 15, 3)) == [] + assert fake.started == [] + runner.tick(_utc(2026, 9, 30, 14, 59, 40)) + runner.tick(_utc(2026, 9, 30, 15, 0, 5)) + assert len(fake.started) == 1 + assert sr.load_inventory(ctx)["schedules"]["duid-1"][0]["enabled"] is True + + +def test_persisted_claim_prevents_replay_after_restart_and_clock_rollback(tmp_path, monkeypatch): + runner, ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 10)) + restarted = sr.ScheduleRunner(ctx) + restarted.tick(_utc(2026, 9, 28, 14, 59, 40)) + restarted.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert len(fake.started) == 1 + + +def test_failed_persistence_prevents_robot_dispatch(tmp_path, monkeypatch): + from shared import schedule_store + runner, _, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + monkeypatch.setattr(schedule_store, "write_inventory", lambda *args: False) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + result = runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert result[0]["accepted"] is False + assert fake.started == [] + + +def test_job_edit_between_tick_and_claim_cancels_stale_occurrence(tmp_path, monkeypatch): + from shared import schedule_store + runner, ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + original_steps = sr.steps_for_job + def edit_then_prepare(job): + schedule_store.set_enabled(ctx, "duid-1", job["id"], False) + return original_steps(job) + monkeypatch.setattr(sr, "steps_for_job", edit_then_prepare) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert fake.started == [] + + +def test_malformed_import_does_not_block_other_jobs(tmp_path, monkeypatch): + runner, _, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job(id=None), _job()]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert len(fake.started) == 1 diff --git a/tests/test_schedules.py b/tests/test_schedules.py index f362a6ab..90c27ed3 100644 --- a/tests/test_schedules.py +++ b/tests/test_schedules.py @@ -1,5 +1,10 @@ import json +import copy from pathlib import Path +from types import SimpleNamespace +from uuid import uuid4 + +import pytest from fastapi.testclient import TestClient from roborock.data import HomeData, HomeDataSchedule @@ -193,7 +198,7 @@ def _signed_json(paths, path: str, body: dict[str, object]) -> tuple[bytes, dict headers = { # Hawk rejects replayed nonces, and _hawk_headers() uses nonce- for plain requests. "Authorization": build_hawk_authorization( - user=user, path=path, json_body=raw, nonce=f"write-{path.replace('/', '-')}-{len(raw)}" + user=user, path=path, json_body=raw, nonce=f"write-{uuid4().hex}" ), "Content-Type": "application/json", } @@ -253,3 +258,175 @@ def test_creating_a_job_with_a_bad_cron_is_rejected(tmp_path: Path) -> None: response = client.post(collection, content=body, headers=headers) assert response.json()["success"] is False assert response.json()["code"] == 400 + + +def _native_job(): + # Synthetic IDs/settings based on the a15/a27 protocol shape, not a raw capture. + return { + "id": 42001, "cron": "09 14 ? * 1,2,3,4,5", "timeZoneId": "America/Los_Angeles", + "repeated": True, "enabled": True, "type": "clean", "modelOption": {"mode": 1}, + "param": {"id": 1, "method": "server_scheduled_start", "params": [{ + "name": "1800000000001", "repeat": 1, "water_box_mode": 204, "segments": "16,17", + "fan_power": 106, "mop_mode": 302, "clean_mop": 1, "clean_order_mode": 0, + "map_index": 1, "mop_template_id": 7, "futureOption": {"mode": 2}, + }]}, + } + + +@pytest.fixture +def native_robot(monkeypatch): + from shared import routine_runner + robot = SimpleNamespace(timers=[["1800000000001", "on"]], calls=[], apply=True, + error=None, after_read=None) + + class Client: + def __init__(self, context, device, logger): + assert device["duid"] == "robot-test" + + async def connect(self): + pass + + async def send_command(self, command, params): + robot.calls.append((str(command), copy.deepcopy(params))) + if str(command) == "get_server_timer": + if robot.after_read: + robot.after_read() + return copy.deepcopy(robot.timers) + assert str(command) == "upd_server_timer" + if robot.apply: + robot.timers[0][1] = params[0][1] + if robot.error: + raise robot.error + return ["ok"] + + async def close(self): + pass + + monkeypatch.setattr(routine_runner, "_RoutineMqttClient", Client) + return robot + + +def test_homebridge_delay_and_restore_put_round_trip_preserves_native_job(tmp_path, native_robot): + original = _native_job() + client, paths = _jobs_client(tmp_path, "robot-test", [original]) + collection = "/user/devices/robot-test/jobs" + item = f"{collection}/{original['id']}" + for cron in ("39 14 ? * 1,2,3,4,5", original["cron"]): + body = {key: value for key, value in original.items() if key not in ("id", "nextFireTime")} + body["cron"] = cron + raw, headers = _signed_json(paths, item, body) + response = client.put(item, content=raw, headers=headers) + assert response.json()["success"] is True + saved = json.loads(paths.inventory_path.read_text())["schedules"]["robot-test"][0] + assert saved == {**original, "cron": cron} + listed = client.get(collection, headers=_hawk_headers(paths.cloud_snapshot_path, collection)).json()["result"] + assert {k: v for k, v in listed[0].items() if k != "nextFireTime"} == original + assert listed[0]["param"]["params"][0]["name"] != str(listed[0]["id"]) + assert all(method == "get_server_timer" for method, _ in native_robot.calls) + + +def test_direct_http_enablement_updates_robot_then_store_for_any_client(tmp_path, native_robot): + original = _native_job() + client, paths = _jobs_client(tmp_path, "robot-test", [original]) + item = "/user/devices/robot-test/jobs/42001" + for enabled in (False, True): + raw, headers = _signed_json(paths, item, {"enabled": enabled}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is True + assert result["result"] == {**original, "enabled": enabled} + assert native_robot.timers[0][1] == ("on" if enabled else "off") + assert native_robot.calls[-2:] == [ + ("upd_server_timer", [["1800000000001", "on" if enabled else "off"]]), + ("get_server_timer", []), + ] + + +def test_oem_timer_then_job_put_does_not_write_native_timer_again(tmp_path, native_robot): + native_robot.timers[0][1] = "off" + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"enabled": False}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is True + assert native_robot.calls == [("get_server_timer", [])] + + +@pytest.mark.parametrize("lost_ack", [False, True]) +def test_http_requires_actual_confirmation_and_does_not_retry_write(tmp_path, native_robot, lost_ack): + native_robot.error = TimeoutError("lost ack") if lost_ack else None + native_robot.apply = lost_ack + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + before = paths.inventory_path.read_bytes() + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"enabled": False}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is lost_ack + assert sum(method == "upd_server_timer" for method, _ in native_robot.calls) == 1 + if not lost_ack: + assert paths.inventory_path.read_bytes() == before + + +def test_http_change_during_timer_read_refuses_stale_native_write(tmp_path, native_robot): + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + def change(): + data = json.loads(paths.inventory_path.read_text()) + data["schedules"]["robot-test"][0]["cron"] = "0 10 * * *" + paths.inventory_path.write_text(json.dumps(data)) + native_robot.after_read = change + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"enabled": False}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is False and result["code"] == 409 + assert native_robot.calls == [("get_server_timer", [])] + + +def test_put_replaces_param_exactly_when_app_removes_optional_fields(tmp_path): + original = _native_job() + client, paths = _jobs_client(tmp_path, "robot-test", [original]) + param = json.loads(json.dumps(original["param"])) + del param["params"][0]["mop_template_id"] + param["params"][0]["segments"] = "0" + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"param": param, "modelOption": {"mode": 5}}) + result = client.put(item, content=raw, headers=headers).json()["result"] + assert result["param"] == param + assert result["modelOption"] == {"mode": 5} + assert result["cron"] == original["cron"] + + +@pytest.mark.parametrize("body", [{"cron": ""}, {"timeZoneId": "Invalid/Zone"}, {"param": []}]) +def test_invalid_update_does_not_mutate_inventory(tmp_path, body): + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + before = paths.inventory_path.read_bytes() + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, body) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is False and result["code"] == 400 + assert paths.inventory_path.read_bytes() == before + + +def test_creates_in_same_millisecond_get_distinct_ids_and_explicit_collision_is_rejected(tmp_path, monkeypatch): + from shared import schedule_store + monkeypatch.setattr(schedule_store.time, "time_ns", lambda: 1800000000001000000) + client, paths = _jobs_client(tmp_path, "robot-test", []) + collection = "/user/devices/robot-test/jobs" + ids = [] + for _ in range(2): + raw, headers = _signed_json(paths, collection, {"cron": "0 9 * * *"}) + ids.append(client.post(collection, content=raw, headers=headers).json()["result"]["id"]) + assert len(set(ids)) == 2 + raw, headers = _signed_json(paths, collection, {"id": ids[0], "cron": "0 10 * * *"}) + result = client.post(collection, content=raw, headers=headers).json() + assert result["success"] is False and result["code"] == 409 + + +def test_http_schedule_write_failure_is_not_acknowledged_as_success(tmp_path, monkeypatch): + from shared import schedule_store + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + before = paths.inventory_path.read_bytes() + monkeypatch.setattr(schedule_store, "write_inventory", lambda *args: False) + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"cron": "0 10 * * *"}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is False and result["code"] == 500 + assert paths.inventory_path.read_bytes() == before diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py index 2dcab6fe..bbbbb312 100644 --- a/tests/test_timer_rpc.py +++ b/tests/test_timer_rpc.py @@ -66,6 +66,23 @@ def test_get_timer_lists_cloud_schedules_in_the_shape_localrock_parses(ctx): ] +@pytest.mark.parametrize("serialized", [False, True]) +def test_native_jobs_are_not_fabricated_as_legacy_timers(ctx, serialized): + for job in schedule_store.list_jobs(ctx, DUID): + param = job["param"] + param["params"][0]["name"] = "native-" + str(job["id"]) + schedule_store.upsert_job(ctx, DUID, job_id=job["id"], cron=job["cron"], + enabled=job["enabled"], param=param) + if serialized: + path = ctx.http_jsonl.parent / "web_api_inventory.json" + inventory = json.loads(path.read_text()) + for job in inventory["schedules"][DUID]: + job["param"] = json.dumps(job["param"]) + path.write_text(json.dumps(inventory)) + assert timer_rpc.handle_timer_request(ctx, DUID, "get_timer", []) == [] + assert len(schedule_store.list_jobs(ctx, DUID)) == 3 + + @pytest.mark.parametrize( ("cron", "expected"), [ @@ -161,6 +178,54 @@ def test_interceptor_leaves_server_timer_to_the_robot(ctx, method): assert interceptor(DUID, _encode_request(method, [])) is None +def test_native_write_observation_preserves_wire_payload_and_waits_for_confirmation(ctx): + from shared.native_schedule_sync import NativeScheduleSync + from roborock_local_server.bundled_backend.mqtt_broker_server.topic_bridge import DeviceTopicKey + + coordinator = NativeScheduleSync(ctx) + bridge = MqttTopicBridge( + host="127.0.0.1", port=1883, logger=logging.getLogger("native-sync-test"), + request_interceptor=timer_rpc.TimerRpcInterceptor(ctx), + request_observer=coordinator.observe_request, + ) + device = DeviceTopicKey(did="robot-did", mqtt_usr="robot-user") + bridge._remember_device_seen(device) + before = schedule_store.list_jobs(ctx, DUID) + sent = [] + + class Client: + async def publish(self, topic, payload, **kwargs): + sent.append((topic, payload)) + + async def exercise(): + for session, enabled in (("homebridge", False), ("oem", True), ("localrock", False)): + cloud = CloudTopicKey(rriot_u=session, mqtt_username=session, duid=DUID) + payload = _encode_request("upd_server_timer", [["native-timer", "on" if enabled else "off"]]) + message = SimpleNamespace(payload=payload, qos=0, retain=False) + await bridge._handle_cloud_message(Client(), message, cloud, cloud.topic_in) + assert sent[-1] == (device.topic_out, payload) + assert schedule_store.list_jobs(ctx, DUID) == before + assert coordinator._generation[DUID] == 3 + + asyncio.run(exercise()) + + +def test_retained_native_write_is_not_observed_as_a_new_change(ctx): + from shared.native_schedule_sync import NativeScheduleSync + from roborock_local_server.bundled_backend.mqtt_broker_server.topic_bridge import DeviceTopicKey + coordinator = NativeScheduleSync(ctx) + bridge = MqttTopicBridge(host="127.0.0.1", port=1883, logger=logging.getLogger("native-sync-test"), + request_observer=coordinator.observe_request) + bridge._remember_device_seen(DeviceTopicKey(did="robot-did", mqtt_usr="robot-user")) + cloud = CloudTopicKey(rriot_u="app", mqtt_username="app", duid=DUID) + class Client: + async def publish(self, *args, **kwargs): + pass + message = SimpleNamespace(payload=_encode_request("upd_server_timer", [["native-timer", "on"]]), qos=0, retain=True) + asyncio.run(bridge._handle_cloud_message(Client(), message, cloud, cloud.topic_in)) + assert not coordinator._dirty + + def test_bridge_answers_timer_requests_locally_and_does_not_forward_them(ctx): class Client: def __init__(self): From 0bbae6fefce43ee7c5d605a34368690d1d15330c Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:42:37 -0500 Subject: [PATCH 09/27] Keep a native schedule read that cannot reach the robot quiet asyncio.timeout raises TimeoutError, which is an OSError, so a read that timed out because the robot had not reconnected after a restart was logged as 'Could not save native schedule reconciliation' with a traceback. The read now has its own handler that logs at debug and returns, and only a failure to save the result still warns. Co-Authored-By: Claude Sonnet 5.5 --- .../shared/native_schedule_sync.py | 14 ++++++++++---- tests/test_native_schedule_sync.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py index 324e37dc..01e51c50 100644 --- a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py +++ b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py @@ -207,10 +207,16 @@ async def refresh(self, duid: str) -> list[Any]: explicit_enable = {name: intent[1] for name, intent in intents.items()} client = None try: - async with asyncio.timeout(15): - client = _RoutineMqttClient(self.context, device, _LOGGER) - await client.connect() - states = timer_states(await client.send_command(TimerCommand.GET, [])) + try: + async with asyncio.timeout(15): + client = _RoutineMqttClient(self.context, device, _LOGGER) + await client.connect() + states = timer_states(await client.send_command(TimerCommand.GET, [])) + except Exception: + # A robot that is offline or has not reconnected yet keeps its last saved state. + # TimeoutError is an OSError, so it must not reach the save-failure warning below. + _LOGGER.debug("Native schedule read unavailable for %s", duid, exc_info=True) + return [] if self._generation.get(duid, 0) != generation: # A newer native write overlapped the read. A later pass # will confirm it; do not apply this older snapshot. diff --git a/tests/test_native_schedule_sync.py b/tests/test_native_schedule_sync.py index 3a7a53a2..62c56563 100644 --- a/tests/test_native_schedule_sync.py +++ b/tests/test_native_schedule_sync.py @@ -163,6 +163,22 @@ async def test_external_toggle_reconciliation_reads_only_and_closes_connection(s self.assertFalse(store.list_jobs(self.context, "robot")[0]["enabled"]) self.assertEqual(self.robot.calls, ["connect", "get_server_timer", "close"]) + async def test_a_read_that_times_out_is_quiet(self): + async def never_answers(): + raise TimeoutError + + self.robot.after_read = never_answers + with self.assertNoLogs(sync.__name__, level="WARNING"): + self.assertEqual(await self.coordinator.refresh("robot"), []) + self.assertEqual(self.robot.calls[-1], "close") + self.assertTrue(store.list_jobs(self.context, "robot")[0]["enabled"]) + + async def test_a_failure_saving_the_reconciliation_still_warns(self): + with patch.object(sync, "reconcile_native_enabled", side_effect=OSError("disk full")): + with self.assertLogs(sync.__name__, level="WARNING") as logs: + self.assertEqual(await self.coordinator.refresh("robot"), []) + self.assertIn("Could not save native schedule reconciliation", logs.output[0]) + async def test_later_native_write_invalidates_an_in_flight_read(self): async def later(): self.coordinator.changed("robot") From b437ef13e4386b03588f0001381a43e0c0730e39 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:20:11 -0500 Subject: [PATCH 10/27] Accept the official app's set_timer layout and keep the robot's own timers visible The official app writes set_timer as [[id, [cron, [action, {...}]]]] with no status element. The parser read the cron as the status and rejected the request with error -1. A timer written without a status now starts on, as the official app's own timers do. Answering every get/set/upd/del_timer locally also hid the robot's own legacy timers. Now: - get_timer goes to the robot and the store's timers are added to its reply (the bridge gets a response_rewriter hook for this), so both sets are listed. - upd_timer and del_timer for an id the store does not hold, and patrol timers (app_start_patrol), go to the robot, which owns them. - A set_timer or upd_timer whose save fails is answered with an error. It is no longer forwarded to the robot as if nothing happened. Co-Authored-By: Claude Sonnet 5.5 --- .../mqtt_broker_server/topic_bridge.py | 17 +- .../bundled_backend/shared/timer_rpc.py | 152 +++++++++++++----- src/roborock_local_server/server.py | 7 +- tests/test_timer_rpc.py | 126 ++++++++++++++- 4 files changed, 255 insertions(+), 47 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py index a21b890f..69c80ffd 100644 --- a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py +++ b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py @@ -75,11 +75,14 @@ def __init__( inventory_path: Path | None = None, request_interceptor: Callable[[str, bytes], bytes | None] | None = None, request_observer: Callable[[str, bytes], None] | None = None, + response_rewriter: Callable[[str, bytes], bytes | None] | None = None, ) -> None: # Given (duid, payload) for each rr/m request; returns an encoded reply to # publish back to the requester instead of forwarding, or None to forward. self._request_interceptor = request_interceptor self._request_observer = request_observer + # Given (duid, payload) for each rr/d reply; returns a replacement payload, or None to keep it. + self._response_rewriter = response_rewriter self._host = host self._port = port self._logger = logger @@ -383,10 +386,12 @@ def add_matches(matches: list[CloudTopicKey]) -> None: add_matches([next(iter(self._m_to_d))]) return targets - async def _publish_mirror(self, client: aiomqtt.Client, message: aiomqtt.Message, target_topic: str) -> None: + async def _publish_mirror( + self, client: aiomqtt.Client, message: aiomqtt.Message, target_topic: str, payload: bytes | None = None, + ) -> None: qos = _extract_qos(message) retain = bool(getattr(message, "retain", False)) - await client.publish(target_topic, message.payload, qos=qos, retain=retain) + await client.publish(target_topic, message.payload if payload is None else payload, qos=qos, retain=retain) async def _handle_cloud_message( self, @@ -475,7 +480,13 @@ async def _handle_device_message( self._m_to_d[cloud_topic] = device_topic target_topic = cloud_topic.topic_out - await self._publish_mirror(client, message, target_topic) + rewritten = None + if self._response_rewriter is not None and not bool(getattr(message, "retain", False)): + try: + rewritten = self._response_rewriter(cloud_topic.duid, bytes(message.payload)) + except Exception: # noqa: BLE001 + self._logger.warning("MQTT response rewriter failed for %s", src_topic, exc_info=True) + await self._publish_mirror(client, message, target_topic, rewritten) self._logger.info( "MQTT bridge d->m %s -> %s bytes=%d", src_topic, diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index 5129dac2..760180ea 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -3,7 +3,9 @@ On cloud-managed robots the schedules live server-side, not on the robot, so clients such as LocalRock that ask the robot for its timers would otherwise see nothing and could never change the schedules the cloud imported. The topic -bridge hands matching requests here instead of forwarding them to the robot. +bridge hands matching requests here instead of forwarding them to the robot. Requests about a +timer the store does not own (a robot's own legacy timer, a patrol timer) still go to the robot, +and get_timer replies from the robot are merged with the store's entries. Deliberately not handled here: get/set/upd/del_server_timer, the newer sibling protocol some firmware speaks instead of (or alongside) this one. The robot @@ -23,7 +25,7 @@ import json import logging import time -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, NamedTuple from roborock.protocol import create_mqtt_decoder, create_mqtt_encoder from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol @@ -55,6 +57,24 @@ class TimerRequestError(ValueError): """The request used a shape this handler cannot interpret.""" +class ForwardToRobot(Exception): + """The request is about a timer the store does not own; the robot must answer it.""" + + +# Timers the robot runs itself; the store cannot start a patrol, so these always go to the robot. +_PATROL_ACTIONS = frozenset({"app_start_patrol", "start_patrol"}) +_STATUS_WORDS = frozenset({"on", "off", "disable"}) +_PENDING_SECONDS = 30.0 + + +class _SetTimer(NamedTuple): + job_id: str + enabled: bool + cron: str + settings: dict[str, Any] + action: str + + def device_cron(cron: str) -> str | None: """Cron in the robot's own 'm h dom month dow' form, with '?' written as '*'. @@ -85,23 +105,31 @@ def _timer_entry(job: dict[str, Any]) -> list[Any] | None: return [str(job.get("id")), "on" if on else "off", [cron, ["start_clean", job_settings(job)]]] -def _parse_set_timer(params: Any) -> tuple[str, bool, str, dict[str, Any]]: +def _parse_set_timer(params: Any) -> _SetTimer: entry = params[0] if isinstance(params, list) and params and isinstance(params[0], list) else params if not isinstance(entry, list) or len(entry) < 2: raise TimerRequestError("set_timer needs [id, [status, [cron, action]]]") job_id = str(entry[0]) + status: Any = None if len(entry) >= 3 and isinstance(entry[1], str): status, detail = entry[1], entry[2] else: spec = entry[1] if not isinstance(spec, list) or len(spec) < 2: - raise TimerRequestError("set_timer spec must be [status, [cron, action]]") - status, detail = spec[0], spec[1] + raise TimerRequestError("set_timer spec must be [status, [cron, action]] or [cron, action]") + if isinstance(spec[0], str) and spec[0].lower() in _STATUS_WORDS and isinstance(spec[1], list): + status, detail = spec[0], spec[1] + else: + # The official app's layout has no status element: [id, [cron, [action, {...}]]]. + detail = spec if not isinstance(detail, list) or not detail: raise TimerRequestError("set_timer is missing its cron") action = detail[1] if len(detail) > 1 else None + action_name = str(action[0]) if isinstance(action, list) and action else "" settings = action[1] if isinstance(action, list) and len(action) > 1 and isinstance(action[1], dict) else {} - return job_id, str(status).lower() == "on", str(detail[0]), settings + # A timer written without a status starts on, as the official app's own timers do. + enabled = True if status is None else str(status).lower() == "on" + return _SetTimer(job_id, enabled, str(detail[0]), settings, action_name) def _id_and_status(params: Any) -> tuple[str, str]: @@ -134,7 +162,9 @@ def handle_timer_request( return [entry for entry in entries if entry is not None] if method == "set_timer": - job_id, enabled, cron, action_settings = _parse_set_timer(params) + job_id, enabled, cron, action_settings, action_name = _parse_set_timer(params) + if action_name in _PATROL_ACTIONS: + raise ForwardToRobot(f"{action_name} timers are run by the robot") try: parse_cron(cron) except CronError as exc: @@ -157,11 +187,13 @@ def handle_timer_request( job_id, status = _id_and_status(params) if status not in ("on", "off"): raise TimerRequestError(f"unknown timer status {status!r}") - set_enabled(ctx, duid, job_id, status == "on") + if not set_enabled(ctx, duid, job_id, status == "on"): + raise ForwardToRobot(f"timer {job_id} is not in the schedule store") return list(_OK) if method == "del_timer": - delete_job(ctx, duid, _single_id(params)) + if not delete_job(ctx, duid, _single_id(params)): + raise ForwardToRobot("timer is not in the schedule store") return list(_OK) raise TimerRequestError(f"not a timer method: {method!r}") @@ -173,6 +205,8 @@ class TimerRpcInterceptor: def __init__(self, context: ServerContext, *, default_timezone: str = "") -> None: self._context = context self._default_timezone = default_timezone + # get_timer requests forwarded to the robot whose reply needs the store's entries added. + self._pending: dict[tuple[str, Any], tuple[float, list[Any]]] = {} def _localkey(self, duid: str) -> str: creds = self._context.runtime_credentials @@ -194,45 +228,91 @@ def __call__(self, duid: str, payload: bytes) -> bytes | None: protocol = int(getattr(message.protocol, "value", message.protocol)) if protocol != RoborockMessageProtocol.RPC_REQUEST.value or not message.payload: return None - request = _request_body(message) + request = _dps_body(message, RoborockMessageProtocol.RPC_REQUEST) if request is None or request.get("method") not in TIMER_METHODS: return None request_id = request.get("id") + method = str(request["method"]) try: - body: dict[str, Any] = { - "id": request_id, - "result": handle_timer_request( - self._context, - duid, - str(request["method"]), - request.get("params"), - default_timezone=self._default_timezone, - ), - } + result = handle_timer_request( + self._context, duid, method, request.get("params"), default_timezone=self._default_timezone, + ) + body: dict[str, Any] = {"id": request_id, "result": result} + except ForwardToRobot: + return None except TimerRequestError as exc: - _LOGGER.warning("Rejected %s for %s: %s", request.get("method"), duid, exc) + _LOGGER.warning("Rejected %s for %s: %s", method, duid, exc) body = {"id": request_id, "error": {"code": -1, "message": str(exc)}} - reply = RoborockMessage( - protocol=RoborockMessageProtocol.RPC_RESPONSE, - payload=json.dumps( - { - "t": int(time.time()), - "dps": {str(RoborockMessageProtocol.RPC_RESPONSE.value): json.dumps(body)}, - }, - separators=(",", ":"), - ).encode(), - seq=message.seq, - version=message.version, - ) - return create_mqtt_encoder(localkey)(reply) + except OSError as exc: + # A save that failed must be reported, not passed on to the robot as if it were fine. + _LOGGER.warning("Could not save %s for %s: %s", method, duid, exc) + body = {"id": request_id, "error": {"code": -1, "message": "unable to save the schedule"}} + if method == "get_timer" and "result" in body: + # The robot may hold legacy timers of its own: let it answer and add the store's entries. + if body["result"]: + self._remember_pending(duid, request_id, body["result"]) + return None + return _encode_reply(localkey, message, body) + def _remember_pending(self, duid: str, request_id: Any, entries: list[Any]) -> None: + now = time.monotonic() + self._pending = {key: value for key, value in self._pending.items() if value[0] > now} + self._pending[(duid, request_id)] = (now + _PENDING_SECONDS, entries) -def _request_body(message: RoborockMessage) -> dict[str, Any] | None: + def merge_response(self, duid: str, payload: bytes) -> bytes | None: + """Add the store's timers to the robot's get_timer reply, or None to pass it on unchanged.""" + if not self._pending: + return None + localkey = self._localkey(duid) + if not localkey: + return None + try: + messages = create_mqtt_decoder(localkey)(payload) + except Exception: # noqa: BLE001 + return None + if len(messages) != 1: + return None + message = messages[0] + if int(getattr(message.protocol, "value", message.protocol)) != RoborockMessageProtocol.RPC_RESPONSE.value: + return None + body = _dps_body(message, RoborockMessageProtocol.RPC_RESPONSE) + if body is None: + return None + pending = self._pending.get((duid, body.get("id"))) + if pending is None or pending[0] <= time.monotonic(): + return None + theirs = body.get("result") if isinstance(body.get("result"), list) else [] + known = {str(entry[0]) for entry in theirs if isinstance(entry, list) and entry} + merged = [*theirs, *(entry for entry in pending[1] if str(entry[0]) not in known)] + return _encode_reply(localkey, message, {"id": body.get("id"), "result": merged}) + + +def _encode_reply(localkey: str, request: RoborockMessage, body: dict[str, Any]) -> bytes: + reply = RoborockMessage( + protocol=RoborockMessageProtocol.RPC_RESPONSE, + payload=json.dumps( + { + "t": int(time.time()), + "dps": {str(RoborockMessageProtocol.RPC_RESPONSE.value): json.dumps(body)}, + }, + separators=(",", ":"), + ).encode(), + seq=request.seq, + version=request.version, + ) + return create_mqtt_encoder(localkey)(reply) + + +def _dps_body(message: RoborockMessage, protocol: RoborockMessageProtocol) -> dict[str, Any] | None: try: outer = json.loads(message.payload) - inner = outer["dps"][str(RoborockMessageProtocol.RPC_REQUEST.value)] + inner = outer["dps"][str(protocol.value)] body = json.loads(inner) if isinstance(inner, str) else inner except (ValueError, KeyError, TypeError): return None return body if isinstance(body, dict) else None + + +def _request_body(message: RoborockMessage) -> dict[str, Any] | None: + return _dps_body(message, RoborockMessageProtocol.RPC_REQUEST) diff --git a/src/roborock_local_server/server.py b/src/roborock_local_server/server.py index f94b6eff..517041a1 100644 --- a/src/roborock_local_server/server.py +++ b/src/roborock_local_server/server.py @@ -2015,6 +2015,7 @@ async def start(self) -> None: self.runtime_state.set_service("mqtt_backend_broker", running=True, required=True, enabled=True) if self.config.broker.enable_topic_bridge: + timer_rpc = TimerRpcInterceptor(self.context, default_timezone=self.config.network.timezone) self._topic_bridge = MqttTopicBridge( host=self.config.broker.host, port=self.config.broker.port, @@ -2022,10 +2023,8 @@ async def start(self) -> None: runtime_state=self.runtime_state, inventory_path=self.paths.inventory_path, request_observer=self.schedule_sync.observe_request, - request_interceptor=TimerRpcInterceptor( - self.context, - default_timezone=self.config.network.timezone, - ), + request_interceptor=timer_rpc, + response_rewriter=timer_rpc.merge_response, ) await self._topic_bridge.start() self.runtime_state.set_service("mqtt_topic_bridge", running=True, required=False, enabled=True) diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py index bbbbb312..b17b40d4 100644 --- a/tests/test_timer_rpc.py +++ b/tests/test_timer_rpc.py @@ -11,6 +11,7 @@ from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol from roborock_local_server.bundled_backend.mqtt_broker_server.topic_bridge import ( CloudTopicKey, + DeviceTopicKey, MqttTopicBridge, ) from shared import schedule_store, timer_rpc @@ -152,12 +153,12 @@ def _decode_reply(reply): return decode_rpc_response(message) -def test_interceptor_answers_get_timer_with_an_encrypted_rpc_response(ctx): - reply = timer_rpc.TimerRpcInterceptor(ctx)(DUID, _encode_request("get_timer", [])) +def test_interceptor_answers_upd_timer_with_an_encrypted_rpc_response(ctx): + reply = timer_rpc.TimerRpcInterceptor(ctx)(DUID, _encode_request("upd_timer", [["3549431", "off"]])) assert reply is not None response = _decode_reply(reply) assert response.request_id == 4242 - assert [entry[0] for entry in response.data] == ["3549431", "3549434", "3878757"] + assert response.data == ["ok"] def test_interceptor_forwards_everything_that_is_not_a_timer_request(ctx): @@ -241,7 +242,7 @@ async def publish(self, topic, payload, *, qos=0, retain=False): request_interceptor=timer_rpc.TimerRpcInterceptor(ctx), ) cloud = CloudTopicKey(rriot_u="u", mqtt_username="m", duid=DUID) - message = SimpleNamespace(payload=_encode_request("get_timer", []), qos=0, retain=False) + message = SimpleNamespace(payload=_encode_request("upd_timer", [["3549431", "off"]]), qos=0, retain=False) client = Client() asyncio.run(bridge._handle_cloud_message(client, message, cloud, cloud.topic_in)) @@ -312,3 +313,120 @@ def test_non_numeric_setting_is_rejected_and_nothing_is_saved(ctx): with pytest.raises(timer_rpc.TimerRequestError): timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["9", ["on", ["0 9 * * *", action]]]]) assert schedule_store.list_jobs(ctx, DUID) == before + + +# What the official app sends (tanos setTimerOnRobot): no status element, the action carries the settings. +OFFICIAL_SET_TIMER = [ + ["1765735413736", ["30 7 * * 1,2", ["start_clean", {"segments": "", "fan_power": 102, "water_box_mode": 202, "repeat": 1}]]] +] +OFFICIAL_PATROL_TIMER = [["1765735413800", ["0 9 * * *", ["app_start_patrol", {"repeat": 1, "source": 3}]]]] + + +def _encode_robot_reply(request_id, result): + body = json.dumps({"id": request_id, "result": result}) + payload = json.dumps({"t": 1790000000, "dps": {"102": body}}).encode() + return create_mqtt_encoder(LOCALKEY)(RoborockMessage(protocol=RoborockMessageProtocol.RPC_RESPONSE, payload=payload)) + + +def test_official_app_set_timer_layout_is_stored_enabled_with_its_settings(ctx): + assert timer_rpc.handle_timer_request(ctx, DUID, "set_timer", OFFICIAL_SET_TIMER) == ["ok"] + + job = _job(ctx, "1765735413736") + assert job["cron"] == "30 7 * * 1,2" + assert job["enabled"] is True + entry = job["param"]["params"][0] + assert (entry["fan_power"], entry["water_box_mode"], entry["repeat"], entry["segments"]) == (102, 202, 1, "0") + + +def test_interceptor_answers_the_official_set_timer_layout_with_ok(ctx): + reply = timer_rpc.TimerRpcInterceptor(ctx)(DUID, _encode_request("set_timer", OFFICIAL_SET_TIMER)) + response = _decode_reply(reply) + assert response.api_error is None + assert response.data == ["ok"] + + +def test_set_timer_layouts_with_a_status_still_work(ctx): + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["a1", ["off", ["0 9 * * *", ["start_clean", {}]]]]]) + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["a2", "off", ["0 9 * * *", ["start_clean", {}]]]]) + assert _job(ctx, "a1")["enabled"] is False + assert _job(ctx, "a2")["enabled"] is False + + +def test_patrol_timers_go_to_the_robot_instead_of_being_stored_as_cleans(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + assert interceptor(DUID, _encode_request("set_timer", OFFICIAL_PATROL_TIMER)) is None + assert all(str(j["id"]) != "1765735413800" for j in schedule_store.list_jobs(ctx, DUID)) + + +def test_upd_and_del_of_a_timer_the_store_does_not_own_go_to_the_robot(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + assert interceptor(DUID, _encode_request("upd_timer", [["robot-own", "off"]])) is None + assert interceptor(DUID, _encode_request("del_timer", ["robot-own"])) is None + assert interceptor(DUID, _encode_request("del_timer", ["3549431"])) is not None + + +def test_get_timer_is_forwarded_and_the_robots_own_timers_are_kept_in_the_reply(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + assert interceptor(DUID, _encode_request("get_timer", [], request_id=77)) is None + + own = ["robot-own", "on", ["0 6 * * *", ["start_clean", {"repeat": 1}]]] + merged = _decode_reply(interceptor.merge_response(DUID, _encode_robot_reply(77, [own]))) + assert [entry[0] for entry in merged.data] == ["robot-own", "3549431", "3549434", "3878757"] + # The robot's reply to some other request is left alone. + assert interceptor.merge_response(DUID, _encode_robot_reply(78, [own])) is None + + +def test_get_timer_reply_without_a_list_still_lists_the_stores_timers(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + interceptor(DUID, _encode_request("get_timer", [], request_id=5)) + merged = _decode_reply(interceptor.merge_response(DUID, _encode_robot_reply(5, None))) + assert [entry[0] for entry in merged.data] == ["3549431", "3549434", "3878757"] + + +def test_get_timer_with_nothing_in_the_store_is_plain_forwarding(tmp_path): + (tmp_path / "web_api_inventory.json").write_text("{}", encoding="utf-8") + empty = SimpleNamespace( + http_jsonl=tmp_path / "http.jsonl", loggers={}, timezone="America/New_York", + runtime_credentials=SimpleNamespace(resolve_device=lambda duid="": {"localkey": LOCALKEY}), localkey="", + ) + interceptor = timer_rpc.TimerRpcInterceptor(empty) + assert interceptor(DUID, _encode_request("get_timer", [], request_id=9)) is None + assert interceptor.merge_response(DUID, _encode_robot_reply(9, [])) is None + + +def test_a_failed_save_is_reported_and_not_forwarded_to_the_robot(ctx, monkeypatch): + def fail(*args, **kwargs): + raise OSError("disk full") + + monkeypatch.setattr(timer_rpc, "upsert_job", fail) + reply = timer_rpc.TimerRpcInterceptor(ctx)(DUID, _encode_request("set_timer", OFFICIAL_SET_TIMER)) + assert reply is not None + assert _decode_reply(reply).api_error is not None + + +def test_bridge_merges_the_robots_get_timer_reply_for_the_client(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + bridge = MqttTopicBridge( + host="127.0.0.1", port=1883, logger=logging.getLogger("test.timer_merge"), + request_interceptor=interceptor, response_rewriter=interceptor.merge_response, + ) + device = DeviceTopicKey(did="robot-did", mqtt_usr="robot-user") + bridge._remember_device_seen(device) + cloud = CloudTopicKey(rriot_u="u", mqtt_username="m", duid=DUID) + sent = [] + + class Client: + async def publish(self, topic, payload, **kwargs): + sent.append((topic, payload)) + + async def exercise(): + request = SimpleNamespace(payload=_encode_request("get_timer", [], request_id=31), qos=0, retain=False) + await bridge._handle_cloud_message(Client(), request, cloud, cloud.topic_in) + assert sent[-1][0] == device.topic_out + reply = SimpleNamespace(payload=_encode_robot_reply(31, []), qos=0, retain=False) + await bridge._handle_device_message(Client(), reply, device, device.topic_in) + + asyncio.run(exercise()) + topic, payload = sent[-1] + assert topic == cloud.topic_out + assert [entry[0] for entry in _decode_reply(payload).data] == ["3549431", "3549434", "3878757"] From ad0da1b6b45f57494ed12707f28018f966c64b2e Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:20:52 -0500 Subject: [PATCH 11/27] Answer job writes with a plain string result IotApi declares POST, PUT and DELETE on /user/devices/{duid}/jobs as ApiReturn, and only GET returns the list. Returning the job object in result would fail Gson parsing in the app, so it would report a failure for a job that was saved. POST now returns the new job's id as a string, PUT and DELETE return "done" (python-roborock's own job tests use "done" for the same calls). The exact cloud strings are not captured anywhere we have, so the shape is unverified against the real service. Co-Authored-By: Claude Sonnet 5.5 --- .../https_server/routes/user/devices/jobs.py | 9 ++-- tests/test_schedules.py | 41 ++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py index 20291c82..5a983c30 100644 --- a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py +++ b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py @@ -15,6 +15,8 @@ from .service import device_jobs_payload +# IotApi declares POST/PUT/DELETE as ApiReturn: a JSON object in `result` fails to parse in the app. +WRITE_DONE = "done" _COLLECTION_RE = re.compile(r"/user/devices/([^/]+)/jobs") _ITEM_RE = re.compile(r"/user/devices/([^/]+)/jobs/([^/]+)") @@ -99,7 +101,7 @@ def build_create( return _error(409, str(exc)) except OSError: return _error(500, "unable to persist schedule") - return wrap_response(created) + return wrap_response(str(created["id"])) def match_update(path: str, method: str = "GET") -> bool: @@ -141,7 +143,8 @@ def _prepare_update(ctx, duid, job_id, body_params): def _save_update(ctx, duid, values): try: - return wrap_response(upsert_job(ctx, duid, **values)) + upsert_job(ctx, duid, **values) + return wrap_response(WRITE_DONE) except ValueError as exc: return _error(409, str(exc)) except OSError: @@ -211,4 +214,4 @@ def build_delete( return _error(404, "schedule not found") except OSError: return _error(500, "unable to persist schedule") - return wrap_response({"id": job_id}) + return wrap_response(WRITE_DONE) diff --git a/tests/test_schedules.py b/tests/test_schedules.py index 90c27ed3..ab71ef08 100644 --- a/tests/test_schedules.py +++ b/tests/test_schedules.py @@ -191,6 +191,11 @@ def _jobs_client(tmp_path: Path, device_id: str, schedules: list[dict[str, objec return TestClient(supervisor.app), paths +def _stored_job(paths, device_id: str, job_id) -> dict[str, object]: + jobs = json.loads(paths.inventory_path.read_text(encoding="utf-8"))["schedules"][device_id] + return next(job for job in jobs if str(job["id"]) == str(job_id)) + + def _signed_json(paths, path: str, body: dict[str, object]) -> tuple[bytes, dict[str, str]]: raw = json.dumps(body, separators=(",", ":")).encode() user = ProtocolAuthStore(paths.cloud_snapshot_path).availability().user @@ -226,7 +231,9 @@ def test_jobs_can_be_created_updated_and_deleted_over_http(tmp_path: Path) -> No created = client.post(collection, content=body, headers=headers) assert created.status_code == 200 assert created.json()["success"] is True - new_id = created.json()["result"]["id"] + # IotApi declares the write calls as ApiReturn, so the result is a plain string. + assert isinstance(created.json()["result"], str) + new_id = int(created.json()["result"]) listed = client.get(collection, headers=_hawk_headers(paths.cloud_snapshot_path, collection)).json()["result"] assert {job["id"] for job in listed} == {3549431, new_id} @@ -236,13 +243,15 @@ def test_jobs_can_be_created_updated_and_deleted_over_http(tmp_path: Path) -> No body, headers = _signed_json(paths, item, {"cron": "15 9 ? * 1,3,5", "enabled": False}) updated = client.put(item, content=body, headers=headers) assert updated.status_code == 200 - result = updated.json()["result"] + assert updated.json()["result"] == "done" + result = _stored_job(paths, device_id, 3549431) assert (result["cron"], result["enabled"]) == ("15 9 ? * 1,3,5", False) # Vac/mop settings are untouched by a cron/enabled edit. assert result["param"]["params"][0]["mop_mode"] == 300 deleted = client.delete(item, headers=_hawk_headers(paths.cloud_snapshot_path, item)) assert deleted.status_code == 200 and deleted.json()["success"] is True + assert deleted.json()["result"] == "done" user = ProtocolAuthStore(paths.cloud_snapshot_path).availability().user assert user is not None second_list = {"Authorization": build_hawk_authorization(user=user, path=collection, nonce="second-list")} @@ -333,7 +342,8 @@ def test_direct_http_enablement_updates_robot_then_store_for_any_client(tmp_path raw, headers = _signed_json(paths, item, {"enabled": enabled}) result = client.put(item, content=raw, headers=headers).json() assert result["success"] is True - assert result["result"] == {**original, "enabled": enabled} + assert result["result"] == "done" + assert _stored_job(paths, "robot-test", 42001) == {**original, "enabled": enabled} assert native_robot.timers[0][1] == ("on" if enabled else "off") assert native_robot.calls[-2:] == [ ("upd_server_timer", [["1800000000001", "on" if enabled else "off"]]), @@ -388,7 +398,8 @@ def test_put_replaces_param_exactly_when_app_removes_optional_fields(tmp_path): param["params"][0]["segments"] = "0" item = "/user/devices/robot-test/jobs/42001" raw, headers = _signed_json(paths, item, {"param": param, "modelOption": {"mode": 5}}) - result = client.put(item, content=raw, headers=headers).json()["result"] + assert client.put(item, content=raw, headers=headers).json()["result"] == "done" + result = _stored_job(paths, "robot-test", 42001) assert result["param"] == param assert result["modelOption"] == {"mode": 5} assert result["cron"] == original["cron"] @@ -413,7 +424,7 @@ def test_creates_in_same_millisecond_get_distinct_ids_and_explicit_collision_is_ ids = [] for _ in range(2): raw, headers = _signed_json(paths, collection, {"cron": "0 9 * * *"}) - ids.append(client.post(collection, content=raw, headers=headers).json()["result"]["id"]) + ids.append(int(client.post(collection, content=raw, headers=headers).json()["result"])) assert len(set(ids)) == 2 raw, headers = _signed_json(paths, collection, {"id": ids[0], "cron": "0 10 * * *"}) result = client.post(collection, content=raw, headers=headers).json() @@ -430,3 +441,23 @@ def test_http_schedule_write_failure_is_not_acknowledged_as_success(tmp_path, mo result = client.put(item, content=raw, headers=headers).json() assert result["success"] is False and result["code"] == 500 assert paths.inventory_path.read_bytes() == before + + +def test_write_calls_answer_with_a_string_result_like_iotapi_declares(tmp_path: Path) -> None: + device_id = "6HL2zfniaoYYV01CkVuhkO" + client, paths = _jobs_client(tmp_path, device_id, []) + collection = f"/user/devices/{device_id}/jobs" + body, headers = _signed_json(paths, collection, {"cron": "30 7 * * 1,2", "enabled": True}) + created = client.post(collection, content=body, headers=headers).json() + assert isinstance(created["result"], str) and created["result"].isdigit() + + item = f"{collection}/{created['result']}" + body, headers = _signed_json(paths, item, {"enabled": False}) + updated = client.put(item, content=body, headers=headers).json() + deleted = client.delete(item, headers=_hawk_headers(paths.cloud_snapshot_path, item)).json() + assert updated["success"] is True and updated["result"] == "done" + assert deleted["success"] is True and deleted["result"] == "done" + + body, headers = _signed_json(paths, collection, {"cron": "not a cron"}) + refused = client.post(collection, content=body, headers=headers).json() + assert refused["success"] is False and refused["result"] is None From 518e2dfcd7334a3b2fc981f56b677f46be8fc2e3 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:21:58 -0500 Subject: [PATCH 12/27] Match native schedule timers on firmware that lists them as one string An S6 MaxV (a10, 02.62.46) answers get_server_timer with ["Y 1 1 app_start", 0] for an on timer and ["Y ...", "off", 0] for an off one, and upd_server_timer only accepts the whole entry string. The runner and the enablement sync looked for [name, "on"|"off"], so on that firmware native-linked jobs never matched: they were never started and a toggle never synced. A small parser (native_timers.py) reads both layouts. The dispatch check, the periodic read, the HTTP enable path and the observed upd_server_timer use it, and upd_server_timer is sent the entry string the robot listed. Tests use the S6 reply as reported. Co-Authored-By: Claude Sonnet 5.5 --- .../shared/native_schedule_sync.py | 34 ++++++----- .../bundled_backend/shared/native_timers.py | 42 ++++++++++++++ .../bundled_backend/shared/routine_runner.py | 6 +- tests/test_native_schedule_sync.py | 57 ++++++++++++++++++- tests/test_routine_runner.py | 37 ++++++++++++ 5 files changed, 156 insertions(+), 20 deletions(-) create mode 100644 src/roborock_local_server/bundled_backend/shared/native_timers.py diff --git a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py index 01e51c50..3925ae33 100644 --- a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py +++ b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py @@ -10,6 +10,7 @@ from typing import Any from .inventory_io import load_inventory +from .native_timers import NativeTimer, native_timer_name as entry_timer_name, parse_native_timers from .schedule_store import SettingsError, list_jobs, native_timer_name, reconcile_native_enabled _LOGGER = logging.getLogger(__name__) @@ -26,15 +27,19 @@ class TimerCommand(StrEnum): UPDATE = "upd_server_timer" -def _timer_state(timers: Any, name: str) -> str: +def _timer_entry(timers: Any, name: str) -> NativeTimer: if not isinstance(timers, list): raise TimerSyncError("The vacuum did not return a usable timer list. Refresh schedules and retry.") - matches = [entry for entry in timers if isinstance(entry, list) and entry and entry[0] == name] + matches = [timer for timer in parse_native_timers(timers) if timer.name == name] if not matches: raise TimerSyncError("The linked robot timer is missing. Check this schedule in the Roborock app before retrying.", 409) - if len(matches) != 1 or len(matches[0]) < 2 or matches[0][1] not in ("on", "off"): + if len(matches) != 1 or matches[0].enabled is None: raise TimerSyncError("The linked robot timer is ambiguous. Check this schedule in the Roborock app before retrying.", 409) - return matches[0][1] + return matches[0] + + +def _timer_state(timers: Any, name: str) -> str: + return "on" if _timer_entry(timers, name).enabled else "off" async def sync_timer( @@ -58,13 +63,15 @@ async def sync_timer( async with asyncio.timeout(45): client = _RoutineMqttClient(context, device, logging.getLogger(__name__)) await client.connect() - current = _timer_state(await client.send_command(TimerCommand.GET, []), name) + timer = _timer_entry(await client.send_command(TimerCommand.GET, []), name) + current = "on" if timer.enabled else "off" # Recheck the job after the network read, before changing the robot. check_current() if current != desired: write_attempted = True try: - await client.send_command(TimerCommand.UPDATE, [[name, desired]]) + # Some firmware only accepts the full entry string it listed, not the bare name. + await client.send_command(TimerCommand.UPDATE, [[timer.entry_id, desired]]) except Exception: # A read-back can confirm a write whose acknowledgement was # lost. It cannot confirm a refused or unapplied write. @@ -98,15 +105,12 @@ def timer_states(timers: Any) -> dict[str, bool]: states: dict[str, bool] = {} seen: set[str] = set() invalid: set[str] = set() - for entry in timers: - if not isinstance(entry, list) or not entry or not isinstance(entry[0], str): - continue - name = entry[0] - if name in seen or len(entry) < 2 or entry[1] not in ("on", "off"): - invalid.add(name) + for timer in parse_native_timers(timers): + if timer.name in seen or timer.enabled is None: + invalid.add(timer.name) else: - states[name] = entry[1] == "on" - seen.add(name) + states[timer.name] = timer.enabled + seen.add(timer.name) return {name: enabled for name, enabled in states.items() if name not in invalid} @@ -173,7 +177,7 @@ def observe_request(self, duid: str, payload: bytes) -> None: if (request.get("method") == "upd_server_timer" and isinstance(params, list) and len(params) == 1 and isinstance(params[0], list) and len(params[0]) == 2 and isinstance(params[0][0], str)): - name, state = params[0] + name, state = entry_timer_name(params[0][0]), params[0][1] # A later off must invalidate an earlier on intent, too. self._enable_intent.setdefault(duid, {}).pop(name, None) if state == "on": diff --git a/src/roborock_local_server/bundled_backend/shared/native_timers.py b/src/roborock_local_server/bundled_backend/shared/native_timers.py new file mode 100644 index 00000000..5512af95 --- /dev/null +++ b/src/roborock_local_server/bundled_backend/shared/native_timers.py @@ -0,0 +1,42 @@ +"""Read the robot's get_server_timer entries, whichever layout the firmware answers with.""" + +from __future__ import annotations + +from dataclasses import dataclass +from typing import Any + + +@dataclass(frozen=True) +class NativeTimer: + name: str + # What upd_server_timer and del_server_timer must be given: the name, or the whole entry string. + entry_id: str + # None when the entry does not say clearly. + enabled: bool | None + + +def native_timer_name(entry_id: str) -> str: + """The timer name inside an entry id such as 'Y 1 1 app_start', else the id itself.""" + tokens = entry_id.split() + return tokens[1] if len(tokens) >= 3 else entry_id + + +def parse_native_timers(timers: Any) -> list[NativeTimer]: + """Most robots answer [name, "on"|"off", ...]. An S6 MaxV (a10, 02.62.46) answers + ["Y 1 1 app_start", 0] for an on timer and ["Y ...", "off", 0] for an off one.""" + found: list[NativeTimer] = [] + if not isinstance(timers, list): + return found + for entry in timers: + if not isinstance(entry, list) or not entry or not isinstance(entry[0], str): + continue + entry_id = entry[0] + second = entry[1] if len(entry) > 1 else None + if second in ("on", "off"): + enabled: bool | None = second == "on" + elif len(entry_id.split()) >= 3 and not isinstance(second, (str, bool)) and (second is None or isinstance(second, int)): + enabled = True + else: + enabled = None + found.append(NativeTimer(native_timer_name(entry_id), entry_id, enabled)) + return found diff --git a/src/roborock_local_server/bundled_backend/shared/routine_runner.py b/src/roborock_local_server/bundled_backend/shared/routine_runner.py index 117164f4..a09d94bd 100644 --- a/src/roborock_local_server/bundled_backend/shared/routine_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/routine_runner.py @@ -35,6 +35,7 @@ def _ensure_local_python_roborock_on_path() -> None: from roborock.roborock_typing import RoborockCommand from .context import ServerContext +from .native_timers import parse_native_timers from .schedule_store import claimed_job_is_current __all__ = [ @@ -1138,9 +1139,8 @@ async def _run_steps( # cleaning, nor enqueue a start for an offline robot. if not isinstance(timers, list): raise RoutineExecutionError("Cannot verify native schedule timer state") - matching = [entry for entry in timers if isinstance(entry, list) and len(entry) >= 2 - and entry[0] == timer_name] - if len(matching) != 1 or matching[0][1] != "on": + matching = [timer for timer in parse_native_timers(timers) if timer.name == timer_name] + if len(matching) != 1 or matching[0].enabled is not True: logger.info("Skipping schedule: robot timer is off, missing, or ambiguous") continue elif schedule_job is not None: diff --git a/tests/test_native_schedule_sync.py b/tests/test_native_schedule_sync.py index 62c56563..be9e23fc 100644 --- a/tests/test_native_schedule_sync.py +++ b/tests/test_native_schedule_sync.py @@ -128,7 +128,7 @@ async def asyncSetUp(self): runtime_credentials=SimpleNamespace(resolve_device=lambda **kw: {"duid": "robot", "localkey": "test"}), ) self.coordinator = sync.NativeScheduleSync(self.context) - self.robot = SimpleNamespace(timers=[["native-1", "off"]], calls=[], after_read=None, write_error=None, apply=True) + self.robot = SimpleNamespace(timers=[["native-1", "off"]], calls=[], after_read=None, write_error=None, apply=True, s6=False) robot = self.robot class Client: @@ -145,7 +145,11 @@ async def send_command(self, command, params): if robot.after_read: await robot.after_read() return result - if robot.apply: + if robot.apply and robot.s6: + # An S6 MaxV only accepts the whole entry string it listed; a bare id answers ok and changes nothing. + if params[0][0] == robot.timers[0][0]: + robot.timers[0] = [params[0][0], *(["off"] if params[0][1] == "off" else []), 0] + elif robot.apply: robot.timers[0][1] = params[0][1] if robot.write_error: raise robot.write_error @@ -247,5 +251,54 @@ async def pause(): self.assertEqual(self.robot.calls[-1], "close") +# Taken from an S6 MaxV (roborock.vacuum.a10, firmware 02.62.46): the entry is one string, and an +# off timer carries "off" between the string and the trailing 0. +S6_ID = "Y native-1 1 1 00 10 * * ? app_start" +S6_ON = [[S6_ID, 0]] +S6_OFF = [[S6_ID, "off", 0]] + + +class S6TimerLayoutTests(NativeCoordinatorTests): + async def asyncSetUp(self): + await super().asyncSetUp() + self.robot.s6 = True + self.robot.timers = copy.deepcopy(S6_OFF) + + def test_parses_both_layouts(self): + self.assertEqual(sync.timer_states(S6_ON), {"native-1": True}) + self.assertEqual(sync.timer_states(S6_OFF), {"native-1": False}) + self.assertEqual(sync.timer_states([["native-1", "on", 1], ["other", "off", 0]]), {"native-1": True, "other": False}) + self.assertEqual(sync.timer_states([[S6_ID, 0], [S6_ID, "off", 0]]), {}) + + async def test_periodic_read_syncs_an_off_timer_into_the_job(self): + self.assertEqual(await self.coordinator.refresh("robot"), [701]) + self.assertFalse(store.list_jobs(self.context, "robot")[0]["enabled"]) + + async def test_enable_sends_the_whole_entry_string_and_confirms_by_reading_back(self): + self.assertEqual(await sync.sync_timer(self.context, "robot", "native-1", True, check_current=lambda: None), "on") + self.assertEqual(self.robot.timers, S6_ON) + self.assertEqual(self.robot.calls.count("upd_server_timer"), 1) + + async def test_disable_sends_the_whole_entry_string(self): + self.robot.timers = copy.deepcopy(S6_ON) + self.assertEqual(await sync.sync_timer(self.context, "robot", "native-1", False, check_current=lambda: None), "off") + self.assertEqual(self.robot.timers, S6_OFF) + + def test_an_observed_enable_of_the_whole_entry_string_is_tied_to_the_job(self): + from roborock.protocol import create_mqtt_encoder + from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol + + context = SimpleNamespace( + http_jsonl=self.path.parent / "http.jsonl", + runtime_credentials=SimpleNamespace(resolve_device=lambda **kw: {"duid": "robot", "localkey": "testkey000000000"}), + ) + coordinator = sync.NativeScheduleSync(context) + body = json.dumps({"id": 1, "method": "upd_server_timer", "params": [[S6_ID, "on"]]}) + payload = json.dumps({"t": 1, "dps": {"101": body}}).encode() + frame = create_mqtt_encoder("testkey000000000")(RoborockMessage(protocol=RoborockMessageProtocol.RPC_REQUEST, payload=payload)) + coordinator.observe_request("robot", frame) + self.assertIn("native-1", coordinator._enable_intent["robot"]) + + if __name__ == "__main__": unittest.main() diff --git a/tests/test_routine_runner.py b/tests/test_routine_runner.py index 0add6380..894c35d9 100644 --- a/tests/test_routine_runner.py +++ b/tests/test_routine_runner.py @@ -1106,3 +1106,40 @@ async def send_command(self, command, params=None): steps=[_server_start_step(name="timer")], sync_scene_tids=False, schedule_job=job, schedule_deadline=deadline)) assert sent == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER] + + +# What an S6 MaxV (a10, 02.62.46) answers to get_server_timer: one string per timer, 0 at the end, +# and "off" before the 0 when the timer is off. +S6_TIMER_ON = [["Y 1765735413736 1 1 00 10 * * ? app_start", 0]] +S6_TIMER_OFF = [["Y 1765735413736 1 1 00 10 * * ? app_start", "off", 0]] + + +def _run_native_step_against(tmp_path: Path, monkeypatch, timers) -> list: + sent = [] + + class Client: + def __init__(self, *args): pass + async def connect(self): pass + async def close(self): pass + async def send_command(self, command, params=None): + sent.append(command) + if command == routine_runner_module.SchedulerCommand.GET_SERVER_TIMER: + return timers + return ["ok"] + + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", Client) + runner = RoutineRunner(_test_context(tmp_path)) + scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") + asyncio.run(runner._run_steps(scene=scene, steps=[_server_start_step(name="1765735413736")], sync_scene_tids=False)) + return sent + + +def test_a_native_schedule_starts_when_an_s6_style_timer_entry_is_on(tmp_path: Path, monkeypatch) -> None: + sent = _run_native_step_against(tmp_path, monkeypatch, S6_TIMER_ON) + assert sent == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER, + routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START] + + +def test_a_native_schedule_is_skipped_when_an_s6_style_timer_entry_is_off(tmp_path: Path, monkeypatch) -> None: + sent = _run_native_step_against(tmp_path, monkeypatch, S6_TIMER_OFF) + assert sent == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER] From 05bd28727fdb99148f4de3afe0448a75d312dcec Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:23:26 -0500 Subject: [PATCH 13/27] Stop opening a connection and reloading the inventory on every poll The native schedule sync opened a new MQTT connection and sent get_server_timer to every robot with a native-linked job every 30 seconds, forever, and the scheduler re-read and parsed the whole inventory file every 20 seconds, both on the event loop. - The sync keeps one connection per robot between reads and drops it on any failed or cancelled read, and closes it when the loop stops. - A robot that cannot be read is retried after 30 s, then 60 s, 120 s and so on up to 10 minutes. A forwarded native write still triggers an immediate read. - The scheduler and the sync's device scan read the inventory through CachedInventory, which parses the file again only when its size or modification time changed. - The job list read and the reconciliation write now run in a worker thread. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/inventory_io.py | 24 ++++ .../shared/native_schedule_sync.py | 114 ++++++++++++------ .../bundled_backend/shared/schedule_runner.py | 8 +- tests/test_inventory_io.py | 25 ++++ tests/test_native_schedule_sync.py | 56 ++++++++- tests/test_schedule_runner.py | 12 ++ 6 files changed, 199 insertions(+), 40 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/shared/inventory_io.py b/src/roborock_local_server/bundled_backend/shared/inventory_io.py index 7ceab9fd..62219202 100644 --- a/src/roborock_local_server/bundled_backend/shared/inventory_io.py +++ b/src/roborock_local_server/bundled_backend/shared/inventory_io.py @@ -75,6 +75,30 @@ def load_inventory(ctx: ServerContext) -> dict[str, Any]: return loaded if isinstance(loaded, dict) else {} +class CachedInventory: + """Re-reads the inventory file only when its size or modification time changed. + + The returned dict is shared: callers must treat it as read-only. + """ + + def __init__(self, ctx: ServerContext) -> None: + self._ctx = ctx + self._signature: tuple[int, int] | None = None + self._inventory: dict[str, Any] = {} + + def read(self) -> dict[str, Any]: + path = self._ctx.http_jsonl.parent / WEB_API_INVENTORY_FILE + try: + stat = path.stat() + signature: tuple[int, int] | None = (stat.st_mtime_ns, stat.st_size) + except OSError: + signature = None + if signature is None or signature != self._signature: + self._inventory = load_inventory(self._ctx) + self._signature = signature + return self._inventory + + def write_inventory(ctx: ServerContext, inventory: dict[str, Any]) -> bool: path = ctx.http_jsonl.parent / WEB_API_INVENTORY_FILE try: diff --git a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py index 3925ae33..82d39d19 100644 --- a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py +++ b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py @@ -9,7 +9,7 @@ import time from typing import Any -from .inventory_io import load_inventory +from .inventory_io import CachedInventory, load_inventory from .native_timers import NativeTimer, native_timer_name as entry_timer_name, parse_native_timers from .schedule_store import SettingsError, list_jobs, native_timer_name, reconcile_native_enabled @@ -124,6 +124,8 @@ class NativeScheduleSync: INTERVAL = 30.0 DEBOUNCE = 0.3 + # A robot that cannot be read is retried after INTERVAL, then twice as long each time, up to this. + MAX_BACKOFF = 600.0 def __init__(self, context: Any) -> None: self.context = context @@ -133,6 +135,11 @@ def __init__(self, context: Any) -> None: self._dirty: set[str] = set() self._wake = asyncio.Event() self._slots = asyncio.Semaphore(2) + self._inventory = CachedInventory(context) + # One MQTT connection per robot, kept between reads and dropped on any failure. + self._clients: dict[str, tuple[str, Any]] = {} + self._failures: dict[str, int] = {} + self._retry_at: dict[str, float] = {} def lock(self, duid: str) -> asyncio.Lock: return self._locks.setdefault(duid, asyncio.Lock()) @@ -186,11 +193,45 @@ def observe_request(self, duid: str, payload: bytes) -> None: except Exception: # Unrelated/unsupported frames must always pass through. return - async def refresh(self, duid: str) -> list[Any]: + async def _client(self, duid: str, device: dict[str, Any]) -> Any: from .routine_runner import _RoutineMqttClient + localkey = str(device.get("localkey") or "") + cached = self._clients.get(duid) + if cached is not None and cached[0] == localkey: + return cached[1] + await self._drop_client(duid) + client = _RoutineMqttClient(self.context, device, _LOGGER) + await client.connect() + self._clients[duid] = (localkey, client) + return client + + async def _drop_client(self, duid: str) -> None: + cached = self._clients.pop(duid, None) + if cached is None: + return + try: + async with asyncio.timeout(5): + await cached[1].close() + except Exception: + _LOGGER.debug("Could not close native schedule read for %s", duid, exc_info=True) + + async def aclose(self) -> None: + for duid in list(self._clients): + await self._drop_client(duid) + + def _read_failed(self, duid: str) -> None: + failures = self._failures.get(duid, 0) + 1 + self._failures[duid] = failures + self._retry_at[duid] = time.monotonic() + min(self.INTERVAL * 2 ** (failures - 1), self.MAX_BACKOFF) + + def _read_succeeded(self, duid: str) -> None: + self._failures.pop(duid, None) + self._retry_at.pop(duid, None) + + async def refresh(self, duid: str) -> list[Any]: async with self._slots, self.lock(duid): - snapshot = list_jobs(self.context, duid) + snapshot = await asyncio.to_thread(list_jobs, self.context, duid) names = [] for job in snapshot: try: @@ -209,25 +250,28 @@ async def refresh(self, duid: str) -> list[Any]: if intent[0] > time.monotonic()} self._enable_intent[duid] = intents explicit_enable = {name: intent[1] for name, intent in intents.items()} - client = None + read_ok = False try: try: async with asyncio.timeout(15): - client = _RoutineMqttClient(self.context, device, _LOGGER) - await client.connect() + client = await self._client(duid, device) states = timer_states(await client.send_command(TimerCommand.GET, [])) + read_ok = True except Exception: # A robot that is offline or has not reconnected yet keeps its last saved state. # TimeoutError is an OSError, so it must not reach the save-failure warning below. _LOGGER.debug("Native schedule read unavailable for %s", duid, exc_info=True) + self._read_failed(duid) return [] + self._read_succeeded(duid) if self._generation.get(duid, 0) != generation: # A newer native write overlapped the read. A later pass # will confirm it; do not apply this older snapshot. self._dirty.add(duid) self._wake.set() return [] - changed = reconcile_native_enabled( + changed = await asyncio.to_thread( + reconcile_native_enabled, self.context, duid, snapshot, states, explicit_enable=explicit_enable, ) # A read can arrive before an outstanding command is applied. @@ -245,34 +289,34 @@ async def refresh(self, duid: str) -> list[Any]: _LOGGER.debug("Native schedule read unavailable for %s", duid, exc_info=True) return [] finally: - if client is not None: - try: - async with asyncio.timeout(5): - await client.close() - except Exception: - _LOGGER.debug("Could not close native schedule read for %s", duid, exc_info=True) + if not read_ok: + # A failed or cancelled read leaves the connection in an unknown state. + await self._drop_client(duid) async def run(self, stop_event: asyncio.Event) -> None: next_scan = 0.0 - while not stop_event.is_set(): - now = time.monotonic() - due = set(self._dirty) - self._dirty.clear() - self._wake.clear() - if now >= next_scan: - schedules = load_inventory(self.context).get("schedules", {}) - if isinstance(schedules, dict): - due.update(str(duid) for duid in schedules) - next_scan = now + self.INTERVAL - if due: - await asyncio.sleep(self.DEBOUNCE) - results = await asyncio.gather(*(self.refresh(duid) for duid in sorted(due)), return_exceptions=True) - for duid, result in zip(sorted(due), results): - if isinstance(result, Exception): - _LOGGER.warning("Native schedule reconciliation unavailable for %s: %s", duid, type(result).__name__) - if self._dirty: - continue - try: - await asyncio.wait_for(self._wake.wait(), timeout=max(0.1, next_scan - time.monotonic())) - except TimeoutError: - pass + try: + while not stop_event.is_set(): + now = time.monotonic() + due = set(self._dirty) + self._dirty.clear() + self._wake.clear() + if now >= next_scan: + schedules = self._inventory.read().get("schedules", {}) + if isinstance(schedules, dict): + due.update(str(duid) for duid in schedules if self._retry_at.get(str(duid), 0.0) <= now) + next_scan = now + self.INTERVAL + if due: + await asyncio.sleep(self.DEBOUNCE) + results = await asyncio.gather(*(self.refresh(duid) for duid in sorted(due)), return_exceptions=True) + for duid, result in zip(sorted(due), results): + if isinstance(result, Exception): + _LOGGER.warning("Native schedule reconciliation unavailable for %s: %s", duid, type(result).__name__) + if self._dirty: + continue + try: + await asyncio.wait_for(self._wake.wait(), timeout=max(0.1, next_scan - time.monotonic())) + except TimeoutError: + pass + finally: + await self.aclose() diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index 84c99440..b2daab4e 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -18,7 +18,7 @@ from .constants import DEFAULT_TIMEZONE from .data_helpers import as_bool -from .inventory_io import load_inventory +from .inventory_io import CachedInventory, load_inventory # noqa: F401 (load_inventory stays importable from here) from .routine_runner import ( SCHEDULE_APP_START, SCHEDULE_SERVER_START, @@ -262,11 +262,11 @@ def __init__(self, context: ServerContext, *, default_timezone: str = "") -> Non self._context = context self._default_timezone = default_timezone self._last_tick: datetime | None = None + self._inventory = CachedInventory(context) self._logger = context.loggers.get("api") or context.loggers.get("unknown") or _LOGGER def due_jobs(self, since: datetime, until: datetime) -> list[tuple[str, dict[str, Any], datetime]]: - inventory = load_inventory(self._context) - schedules = inventory.get("schedules") + schedules = self._inventory.read().get("schedules") if not isinstance(schedules, dict): return [] due: list[tuple[str, dict[str, Any], datetime]] = [] @@ -287,7 +287,7 @@ def due_jobs(self, since: datetime, until: datetime) -> list[tuple[str, dict[str continue upcoming = next_fire(spec, since.astimezone(zone)) if upcoming is not None and upcoming.astimezone(timezone.utc) <= until: - due.append((str(device_id), job, upcoming)) + due.append((str(device_id), copy.deepcopy(job), upcoming)) return due def tick(self, now: datetime | None = None) -> list[dict[str, Any]]: diff --git a/tests/test_inventory_io.py b/tests/test_inventory_io.py index 1bb588a2..43449803 100644 --- a/tests/test_inventory_io.py +++ b/tests/test_inventory_io.py @@ -69,3 +69,28 @@ def fail(*args): with caplog.at_level(logging.ERROR): assert inventory_io.write_inventory(ctx, {}) is False assert "Unable to persist inventory" in caplog.text + + +def test_cached_inventory_reads_the_file_only_when_it_changed(tmp_path, monkeypatch): + ctx = SimpleNamespace(http_jsonl=tmp_path / "http.jsonl") + inventory_io.atomic_write_inventory(tmp_path / inventory_io.WEB_API_INVENTORY_FILE, {"schedules": {}}) + reads = [] + real_load = inventory_io.load_inventory + monkeypatch.setattr(inventory_io, "load_inventory", lambda c: reads.append(1) or real_load(c)) + cache = inventory_io.CachedInventory(ctx) + + assert cache.read() == {"schedules": {}} + assert cache.read() == {"schedules": {}} + assert len(reads) == 1 + + inventory_io.atomic_write_inventory(tmp_path / inventory_io.WEB_API_INVENTORY_FILE, {"schedules": {"a": []}}) + assert cache.read() == {"schedules": {"a": []}} + assert len(reads) == 2 + + +def test_cached_inventory_follows_a_file_that_appears_later(tmp_path): + ctx = SimpleNamespace(http_jsonl=tmp_path / "http.jsonl") + cache = inventory_io.CachedInventory(ctx) + assert cache.read() == {} + inventory_io.atomic_write_inventory(tmp_path / inventory_io.WEB_API_INVENTORY_FILE, {"x": 1}) + assert cache.read() == {"x": 1} diff --git a/tests/test_native_schedule_sync.py b/tests/test_native_schedule_sync.py index be9e23fc..00a0f78e 100644 --- a/tests/test_native_schedule_sync.py +++ b/tests/test_native_schedule_sync.py @@ -5,6 +5,7 @@ import json from pathlib import Path import tempfile +import time from types import SimpleNamespace import unittest from unittest.mock import patch @@ -162,11 +163,64 @@ async def close(self): self.network.start() self.addCleanup(self.network.stop) - async def test_external_toggle_reconciliation_reads_only_and_closes_connection(self): + async def test_external_toggle_reconciliation_reads_only_and_keeps_the_connection(self): self.assertEqual(await self.coordinator.refresh("robot"), [701]) self.assertFalse(store.list_jobs(self.context, "robot")[0]["enabled"]) + self.assertEqual(self.robot.calls, ["connect", "get_server_timer"]) + await self.coordinator.aclose() self.assertEqual(self.robot.calls, ["connect", "get_server_timer", "close"]) + async def test_later_reads_reuse_the_connection_instead_of_opening_a_new_one(self): + for _ in range(3): + await self.coordinator.refresh("robot") + self.assertEqual(self.robot.calls.count("connect"), 1) + self.assertEqual(self.robot.calls.count("get_server_timer"), 3) + await self.coordinator.aclose() + + async def test_a_failed_read_drops_the_connection_and_backs_off_exponentially(self): + async def never_answers(): + raise TimeoutError + + self.robot.after_read = never_answers + await self.coordinator.refresh("robot") + first = self.coordinator._retry_at["robot"] - time.monotonic() + self.assertEqual(self.robot.calls[-1], "close") + await self.coordinator.refresh("robot") + second = self.coordinator._retry_at["robot"] - time.monotonic() + self.assertEqual(self.robot.calls.count("connect"), 2) + self.assertLessEqual(first, self.coordinator.INTERVAL) + self.assertGreater(second, first) + self.robot.after_read = None + await self.coordinator.refresh("robot") + self.assertNotIn("robot", self.coordinator._retry_at) + + async def test_the_periodic_scan_skips_a_robot_that_is_backing_off(self): + self.coordinator.INTERVAL = 0.02 + self.coordinator.DEBOUNCE = 0 + self.coordinator._retry_at["robot"] = time.monotonic() + 3600 + stop = asyncio.Event() + task = asyncio.create_task(self.coordinator.run(stop)) + await asyncio.sleep(0.15) + stop.set() + task.cancel() + try: + await task + except asyncio.CancelledError: + pass + self.assertEqual(self.robot.calls, []) + + async def test_stopping_the_loop_closes_the_connection(self): + self.coordinator.DEBOUNCE = 0 + stop = asyncio.Event() + task = asyncio.create_task(self.coordinator.run(stop)) + async with asyncio.timeout(1): + while "get_server_timer" not in self.robot.calls: + await asyncio.sleep(0.001) + stop.set() + self.coordinator._wake.set() + await task + self.assertEqual(self.robot.calls[-1], "close") + async def test_a_read_that_times_out_is_quiet(self): async def never_answers(): raise TimeoutError diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py index a9331056..632c417e 100644 --- a/tests/test_schedule_runner.py +++ b/tests/test_schedule_runner.py @@ -285,3 +285,15 @@ def test_malformed_import_does_not_block_other_jobs(tmp_path, monkeypatch): runner.tick(_utc(2026, 9, 28, 14, 59, 40)) runner.tick(_utc(2026, 9, 28, 15, 0, 5)) assert len(fake.started) == 1 + + +def test_ticks_do_not_reload_an_unchanged_inventory(tmp_path, monkeypatch): + from shared import inventory_io + + runner, ctx, _ = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + loads = [] + real_load = inventory_io.load_inventory + monkeypatch.setattr(inventory_io, "load_inventory", lambda c: loads.append(1) or real_load(c)) + for second in range(5): + runner.tick(_utc(2026, 9, 28, 14, 0, second * 10)) + assert len(loads) == 1 From 58ab1f6e0598c51656f4d4266e042d79be1e214e Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:23:51 -0500 Subject: [PATCH 14/27] Accept day and month names in schedule crons A cron such as "00 11 ? * MON-FRI" was rejected by the scheduler and by the job routes. Day names (SUN to SAT) and month names (JAN to DEC) now work in lists, ranges and steps, in any case. get_timer writes them out as numbers, since robot clients read digits. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/schedule_runner.py | 20 +++++++++++++----- .../bundled_backend/shared/timer_rpc.py | 8 +++++++ tests/test_schedule_runner.py | 21 +++++++++++++++++++ tests/test_timer_rpc.py | 6 ++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index b2daab4e..ba9a47b1 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -65,7 +65,17 @@ def day_matches(self, day: date) -> bool: return dom_ok and dow_ok -def _parse_field(text: str, low: int, high: int, *, name: str) -> tuple[frozenset[int], bool]: +_MONTH_NAMES = {n: i for i, n in enumerate(("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"), 1)} +_WEEKDAY_NAMES = {n: i for i, n in enumerate(("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"))} + + +def _parse_field( + text: str, low: int, high: int, *, name: str, names: dict[str, int] | None = None +) -> tuple[frozenset[int], bool]: + def number(token: str) -> int: + token = token.strip() + return names[token.upper()] if names and token.upper() in names else int(token) + text = text.strip() if text in ("*", "?"): return frozenset(range(low, high + 1)), False @@ -88,12 +98,12 @@ def _parse_field(text: str, low: int, high: int, *, name: str) -> tuple[frozense elif "-" in part: first, _, last = part.partition("-") try: - start, end = int(first), int(last) + start, end = number(first), number(last) except ValueError as exc: raise CronError(f"bad {name} range {part!r}") from exc else: try: - start = int(part) + start = number(part) except ValueError as exc: raise CronError(f"bad {name} value {part!r}") from exc end = high if step != 1 else start @@ -110,8 +120,8 @@ def parse_cron(expression: str) -> CronSpec: minutes, _ = _parse_field(fields[0], 0, 59, name="minute") hours, _ = _parse_field(fields[1], 0, 23, name="hour") dom, dom_restricted = _parse_field(fields[2], 1, 31, name="day-of-month") - months, _ = _parse_field(fields[3], 1, 12, name="month") - raw_dow, dow_restricted = _parse_field(fields[4], 0, 7, name="day-of-week") + months, _ = _parse_field(fields[3], 1, 12, name="month", names=_MONTH_NAMES) + raw_dow, dow_restricted = _parse_field(fields[4], 0, 7, name="day-of-week", names=_WEEKDAY_NAMES) dow = frozenset(0 if value == 7 else value for value in raw_dow) return CronSpec( minutes=tuple(sorted(minutes)), diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index 760180ea..db0f6774 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -24,6 +24,7 @@ import json import logging +import re import time from typing import TYPE_CHECKING, Any, NamedTuple @@ -65,6 +66,10 @@ class ForwardToRobot(Exception): _PATROL_ACTIONS = frozenset({"app_start_patrol", "start_patrol"}) _STATUS_WORDS = frozenset({"on", "off", "disable"}) _PENDING_SECONDS = 30.0 +_NAME_RE = re.compile(r"[A-Za-z]{3}") +_MONTH_NUMBERS = {name: number for number, name in enumerate( + ("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"), 1)} +_DAY_NUMBERS = {name: number for number, name in enumerate(("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"))} class _SetTimer(NamedTuple): @@ -89,6 +94,9 @@ def device_cron(cron: str) -> str | None: minute, hour, dom, month, dow = (field if field != "?" else "*" for field in fields) if (dom == "*") != (month == "*"): return None + # Robot clients read digits, so day and month names (MON-FRI) are written out as numbers. + month = _NAME_RE.sub(lambda m: str(_MONTH_NUMBERS[m.group(0).upper()]), month) + dow = _NAME_RE.sub(lambda m: str(_DAY_NUMBERS[m.group(0).upper()]), dow) return f"{minute} {hour} {dom} {month} {dow}" diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py index 632c417e..898bc2c8 100644 --- a/tests/test_schedule_runner.py +++ b/tests/test_schedule_runner.py @@ -297,3 +297,24 @@ def test_ticks_do_not_reload_an_unchanged_inventory(tmp_path, monkeypatch): for second in range(5): runner.tick(_utc(2026, 9, 28, 14, 0, second * 10)) assert len(loads) == 1 + + +@pytest.mark.parametrize( + ("named", "numbered"), + [ + ("00 11 ? * MON", "00 11 ? * 1"), + ("00 11 ? * mon-fri", "00 11 ? * 1-5"), + ("00 11 ? * SUN,SAT", "00 11 ? * 0,6"), + ("00 11 ? * MON,WED,FRI", "00 11 ? * 1,3,5"), + ("00 11 1 JAN,JUL ?", "00 11 1 1,7 ?"), + ("00 11 ? * MON-FRI/2", "00 11 ? * 1-5/2"), + ], +) +def test_day_and_month_names_mean_the_same_as_numbers(named, numbered): + assert sr.parse_cron(named) == sr.parse_cron(numbered) + + +@pytest.mark.parametrize("expression", ["00 11 ? * FUNDAY", "00 11 ? * MON-XYZ", "00 11 1 JANE ?"]) +def test_unknown_day_or_month_names_are_rejected(expression): + with pytest.raises(sr.CronError): + sr.parse_cron(expression) diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py index b17b40d4..46f78234 100644 --- a/tests/test_timer_rpc.py +++ b/tests/test_timer_rpc.py @@ -430,3 +430,9 @@ async def exercise(): topic, payload = sent[-1] assert topic == cloud.topic_out assert [entry[0] for entry in _decode_reply(payload).data] == ["3549431", "3549434", "3878757"] + + +def test_get_timer_shows_day_names_as_numbers(ctx): + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["named", ["on", ["0 8 * * MON-FRI", ["start_clean", {}]]]]]) + listed = timer_rpc.handle_timer_request(ctx, DUID, "get_timer", None) + assert next(entry for entry in listed if entry[0] == "named")[2][0] == "0 8 * * 1-5" From 8f6e1165eec98b95e91b320e8fbe96bc8d922b2f Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:24:18 -0500 Subject: [PATCH 15/27] Document missed runs, write responses and the timer layouts in schedules.md States what happens to an occurrence when the server is down, the robot is offline or busy, or its native timer is off: no catch-up, the first tick is only a baseline, the occurrence is consumed, and a one-time job is disabled either way. Also records the string results of the job writes (unverified against the cloud), how get_timer is merged, the S6 MaxV timer layout and the read back-off. Co-Authored-By: Claude Sonnet 5.5 --- docs/schedules.md | 49 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/docs/schedules.md b/docs/schedules.md index 06b67aee..53e17555 100644 --- a/docs/schedules.md +++ b/docs/schedules.md @@ -11,11 +11,11 @@ restoration, daily reset, and recovery. The server provides the underlying APIs. | Operation | Behavior in this branch | | --- | --- | | GET `/user/devices/{duid}/jobs` | Stored jobs with calculated UTC `nextFireTime` for enabled, valid jobs | -| POST to the collection | Persist a new job with a unique numeric ID | -| PUT/PATCH `/user/devices/{duid}/jobs/{id}` | Verify native enablement when supplied, then save; preserve omitted fields and unknown model fields | -| DELETE an item | Remove the stored job and its occurrence record | +| POST to the collection | Persist a new job with a unique numeric ID; `result` is that ID as a string | +| PUT/PATCH `/user/devices/{duid}/jobs/{id}` | Verify native enablement when supplied, then save; preserve omitted fields and unknown model fields; `result` is `"done"` | +| DELETE an item | Remove the stored job and its occurrence record; `result` is `"done"` | | `get/set/upd/del_server_timer` | Forward unchanged to the robot, which owns these timer entries | -| Legacy `get/set/upd/del_timer` | Local compatibility view for jobs without a native timer name | +| Legacy `get/set/upd/del_timer` | `set_timer` and the `upd_timer`/`del_timer` of a stored job are answered from the store. `get_timer` goes to the robot and the store's jobs are added to its reply. Anything about a timer the store does not hold, and patrol timers, go to the robot | | Native scheduled execution | Verify the robot timer is on, then send `server_scheduled_start` with the complete settings entry | A job's numeric `id` is different from the robot timer's string ID in @@ -23,6 +23,23 @@ A job's numeric `id` is different from the robot timer's string ID in The server now reconciles enablement and still checks both before execution. Native jobs are not duplicated in the legacy `get_timer` view; clients must read the jobs API to list native-linked jobs. +The write calls (POST, PUT, DELETE) answer with a plain string in `result` because the +app's `IotApi` declares them as `ApiReturn`; only GET returns the job list. The +exact strings the Roborock cloud sends are not captured here, so this shape is not +verified against the real service. + +`set_timer` is accepted in the official app's layout, `[[id, [cron, ["start_clean", {...}]]]]` +with no status element (such a timer starts enabled), as well as the layouts that carry +`"on"`/`"off"`. A patrol timer (`app_start_patrol`) is forwarded to the robot, because the +server cannot start a patrol. If the store cannot save a `set_timer` or `upd_timer`, the +client gets an error and the request is not forwarded to the robot. + +Robots do not all list their native timers the same way. Most answer `get_server_timer` +with `[name, "on"|"off", ...]`. An S6 MaxV (`roborock.vacuum.a10`, 02.62.46) answers +`["Y 1 1 app_start", 0]` for an on timer and `["Y ...", "off", 0]` for an off +one, and its `upd_server_timer` needs that whole string, not the bare name. Both layouts are +read, and the whole string is sent back when the robot listed it that way. + Time updates accept the existing-job JSON PUT used by Homebridge, without requiring `id` or `nextFireTime` in the body. If `param` is supplied, it replaces the previous object exactly, so app removal of an optional field is preserved. @@ -41,7 +58,10 @@ an `ok` is not used as proof of the final state. No client-specific HTTP write i required after a native toggle. A background read also runs at startup and approximately every 30 seconds to -recover missed traffic, reconnects, and changes made directly over the LAN. +recover missed traffic, reconnects, and changes made directly over the LAN. It reuses +one connection per robot and only reads robots that have a native-linked job. A robot +that cannot be read is tried again after 30 seconds, then 1, 2, 4 and up to 10 minutes; +a forwarded native write still triggers a read at once. Successful reconciliation changes only `enabled` on existing, uniquely matched jobs. It preserves IDs, cron/timezone, settings, unknown fields, and occurrence claims. The dashboard's normal refresh shows the updated saved state. Other @@ -80,6 +100,23 @@ inventory lock does not coordinate multiple containers or external file editors. Cloud re-import replaces schedule definitions, so back up local edits before refreshing. Occurrence claims are retained across that refresh. +## Missed runs + +The scheduler only starts a job for an occurrence that falls inside the last tick, plus up +to 60 seconds of lateness. There is no catch-up: + +- The first tick after startup only sets a baseline. A job due while the server was stopped, + restarting, or suspended is lost, and so is one due during an event-loop stall longer than + 60 seconds. +- An occurrence is consumed as soon as it is claimed, before anything is sent. If the robot + is offline or does not answer, if its linked native timer is off, missing or ambiguous, if + another routine or schedule is running on that robot, or if the 60 second dispatch window + has passed by the time the robot has answered, that occurrence is skipped and is not + retried later. +- A recurring job stays enabled and runs at its next occurrence. +- A one-time job (`repeated` false) is disabled when its occurrence is claimed, whether or not + the clean then started. It does not run again, even after a failure. + A fresh native timer read must find exactly one matching `on` entry. Off, missing, ambiguous, malformed, or unreachable timer state prevents a start. After that network wait, dispatch rechecks the stored job and its deadline, so a concurrent @@ -100,7 +137,7 @@ path; they do not establish native timer support on other firmware. first occurrence of an ambiguous fall-back time. Automated tests cover this policy; matching cloud behavior has not been established. - One-time jobs are consumed when evaluated as due, including when dispatch - subsequently fails. Expired imported one-time jobs and extended downtime need + subsequently fails (see Missed runs). Expired imported one-time jobs and extended downtime need further investigation: a five-field cron has no year and can match again in a later year. Begin hardware testing with recurring jobs. - Test create/edit/delete through the actual app, disabled/missing native timers, From b80e279eace77592efb36c5548cc3e5600fae41b Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:56:25 -0500 Subject: [PATCH 16/27] Assign an id when a created schedule's id is empty The official app posts a new schedule with "id":"" and expects the cloud to assign one. The store only generated an id for None, so the job was saved with an empty id, the create answered with an empty string, a second create was refused as a duplicate and the scheduler skipped the job. None, an empty string and whitespace now all mean assign an id. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/schedule_store.py | 2 +- tests/test_schedules.py | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_store.py b/src/roborock_local_server/bundled_backend/shared/schedule_store.py index e59a09f6..b1af23b1 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_store.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_store.py @@ -237,7 +237,7 @@ def upsert_job( inventory = load_inventory(ctx) jobs = _jobs_for(inventory, duid, create=True) assert jobs is not None - if job_id is None: + if job_id is None or not str(job_id).strip(): job_id = time.time_ns() // 1_000_000 while any(_same_id(job, job_id) for job in jobs): job_id += 1 diff --git a/tests/test_schedules.py b/tests/test_schedules.py index ab71ef08..41a6228f 100644 --- a/tests/test_schedules.py +++ b/tests/test_schedules.py @@ -461,3 +461,25 @@ def test_write_calls_answer_with_a_string_result_like_iotapi_declares(tmp_path: body, headers = _signed_json(paths, collection, {"cron": "not a cron"}) refused = client.post(collection, content=body, headers=headers).json() assert refused["success"] is False and refused["result"] is None + + +@pytest.mark.parametrize("blank", ["", " "]) +def test_the_official_apps_create_body_with_an_empty_id_gets_a_server_id(tmp_path: Path, monkeypatch, blank) -> None: + # Shape captured from the official app: "id" is an empty string and the cloud assigns the id. + client, paths = _jobs_client(tmp_path, "robot-test", []) + collection = "/user/devices/robot-test/jobs" + captured = { + "id": blank, "cron": "15 23 18 10 ?", "repeated": False, "enabled": True, + "param": {"id": 1, "method": "server_scheduled_start", "params": [ + {"repeat": 1, "water_box_mode": 202, "segments": "0", "fan_power": 102, "mop_mode": 300, + "clean_mop": 1, "map_index": -1, "name": "1792329301298"}]}, + } + ids = [] + for _ in range(2): + raw, headers = _signed_json(paths, collection, captured) + result = client.post(collection, content=raw, headers=headers).json() + assert result["success"] is True and result["result"].isdigit() + ids.append(result["result"]) + assert len(set(ids)) == 2 + stored = json.loads(paths.inventory_path.read_text(encoding="utf-8"))["schedules"]["robot-test"] + assert sorted(str(job["id"]) for job in stored) == sorted(ids) From 4ec646530ae0cdccf36d108c59d355c23a42ce51 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:57:52 -0500 Subject: [PATCH 17/27] Key the inventory cache on the file id as well Writes replace the file through a temp file, so two writes inside one coarse mtime tick that leave the size unchanged would have been missed. The file id changes with every replacement. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/inventory_io.py | 6 +++--- tests/test_inventory_io.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/shared/inventory_io.py b/src/roborock_local_server/bundled_backend/shared/inventory_io.py index 62219202..6a2ffd20 100644 --- a/src/roborock_local_server/bundled_backend/shared/inventory_io.py +++ b/src/roborock_local_server/bundled_backend/shared/inventory_io.py @@ -76,21 +76,21 @@ def load_inventory(ctx: ServerContext) -> dict[str, Any]: class CachedInventory: - """Re-reads the inventory file only when its size or modification time changed. + """Re-reads the inventory file only when its size, modification time or file id changed. The returned dict is shared: callers must treat it as read-only. """ def __init__(self, ctx: ServerContext) -> None: self._ctx = ctx - self._signature: tuple[int, int] | None = None + self._signature: tuple[int, int, int] | None = None self._inventory: dict[str, Any] = {} def read(self) -> dict[str, Any]: path = self._ctx.http_jsonl.parent / WEB_API_INVENTORY_FILE try: stat = path.stat() - signature: tuple[int, int] | None = (stat.st_mtime_ns, stat.st_size) + signature: tuple[int, int, int] | None = (stat.st_mtime_ns, stat.st_size, stat.st_ino) except OSError: signature = None if signature is None or signature != self._signature: diff --git a/tests/test_inventory_io.py b/tests/test_inventory_io.py index 43449803..4ecf733b 100644 --- a/tests/test_inventory_io.py +++ b/tests/test_inventory_io.py @@ -94,3 +94,19 @@ def test_cached_inventory_follows_a_file_that_appears_later(tmp_path): assert cache.read() == {} inventory_io.atomic_write_inventory(tmp_path / inventory_io.WEB_API_INVENTORY_FILE, {"x": 1}) assert cache.read() == {"x": 1} + + +def test_cached_inventory_notices_a_same_size_replacement_with_the_same_mtime(tmp_path): + import os + + path = tmp_path / inventory_io.WEB_API_INVENTORY_FILE + ctx = SimpleNamespace(http_jsonl=tmp_path / "http.jsonl") + inventory_io.atomic_write_inventory(path, {"x": 1}) + stamp = path.stat().st_mtime_ns + cache = inventory_io.CachedInventory(ctx) + assert cache.read() == {"x": 1} + inventory_io.atomic_write_inventory(path, {"x": 2}) + os.utime(path, ns=(stamp, stamp)) + if path.stat().st_ino == 0: + pytest.skip("this filesystem reports no file id") + assert cache.read() == {"x": 2} From da01b1e54d22ba2284887de67c74e93ffbd40f37 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:57:52 -0500 Subject: [PATCH 18/27] Accept wrapping weekday ranges and treat a leading * as unrestricted MON-SUN, FRI-SUN and 6-0 style ranges were rejected as out of range; they now run to the end of the week or wrap past Saturday. A day-of-month or day-of-week field that starts with * (such as */2) now counts as unrestricted, which is how Vixie cron decides between AND and OR for the two day fields (from memory of its entry.c, not re-checked against the source here; the cloud's own rule is not known). Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/schedule_runner.py | 12 ++++++++++- tests/test_schedule_runner.py | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index ba9a47b1..ebef03e6 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -101,6 +101,15 @@ def number(token: str) -> int: start, end = number(first), number(last) except ValueError as exc: raise CronError(f"bad {name} range {part!r}") from exc + if names is _WEEKDAY_NAMES and last.strip().upper() == "SUN": + end = high # a range ending on SUN runs to the end of the week (MON-SUN, FRI-SUN) + if names is _WEEKDAY_NAMES and start > end and 0 <= end and start <= high: + # A day range that wraps past Saturday (6-0, SAT-MON). + if start < low or end > high: + raise CronError(f"{name} value out of range in {text!r}") + wrapped = [*range(start, high + 1), *range(low, end + 1)] + values.update(wrapped[::step]) + continue else: try: start = number(part) @@ -110,7 +119,8 @@ def number(token: str) -> int: if start < low or end > high or start > end: raise CronError(f"{name} value out of range in {text!r}") values.update(range(start, end + 1, step)) - return frozenset(values), True + # As in Vixie cron, a field that starts with * (such as */2) counts as unrestricted. + return frozenset(values), not text.startswith("*") def parse_cron(expression: str) -> CronSpec: diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py index 898bc2c8..315df765 100644 --- a/tests/test_schedule_runner.py +++ b/tests/test_schedule_runner.py @@ -318,3 +318,23 @@ def test_day_and_month_names_mean_the_same_as_numbers(named, numbered): def test_unknown_day_or_month_names_are_rejected(expression): with pytest.raises(sr.CronError): sr.parse_cron(expression) + + +@pytest.mark.parametrize( + ("expression", "weekdays"), + [ + ("0 8 * * MON-SUN", {0, 1, 2, 3, 4, 5, 6}), + ("0 8 * * FRI-SUN", {5, 6, 0}), + ("0 8 * * 6-0", {6, 0}), + ("0 8 * * SAT-MON", {6, 0, 1}), + ("0 8 * * 1-7", {0, 1, 2, 3, 4, 5, 6}), + ("0 8 * * 7", {0}), + ], +) +def test_weekday_ranges_that_reach_or_wrap_past_sunday(expression, weekdays): + assert sr.parse_cron(expression).days_of_week == frozenset(weekdays) + + +def test_a_day_of_month_step_starting_with_star_is_unrestricted_like_vixie_cron(): + spec = sr.parse_cron("0 8 */2 * MON") + assert spec.dom_restricted is False and spec.dow_restricted is True From 15212dceb850119df953bca42d574a4239a81be0 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:57:52 -0500 Subject: [PATCH 19/27] Answer get_timer from the store when the robot is silent, and keep robot errors - With the store holding timers and the robot offline, a forwarded get_timer got no reply at all. The bridge now asks the interceptor for a fallback and, if the robot has not answered after 6 seconds, replies with the store's timers. - A robot error reply to get_timer was turned into a success carrying only the store's timers. Error replies now pass through unchanged. - One imported job with a cron get_timer cannot show (for example an unknown day name) raised KeyError and hid every other timer. That job is skipped. get_timer also writes Sunday as 0 and expands wrapping ranges for clients that only know 0 to 6. Co-Authored-By: Claude Sonnet 5.5 --- .../mqtt_broker_server/topic_bridge.py | 27 +++++++ .../bundled_backend/shared/timer_rpc.py | 61 ++++++++++++--- src/roborock_local_server/server.py | 1 + tests/test_timer_rpc.py | 78 +++++++++++++++++++ 4 files changed, 158 insertions(+), 9 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py index 69c80ffd..9bd95a53 100644 --- a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py +++ b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py @@ -76,6 +76,7 @@ def __init__( request_interceptor: Callable[[str, bytes], bytes | None] | None = None, request_observer: Callable[[str, bytes], None] | None = None, response_rewriter: Callable[[str, bytes], bytes | None] | None = None, + request_fallback: Callable[[str, bytes], tuple[float, Callable[[], bytes | None]] | None] | None = None, ) -> None: # Given (duid, payload) for each rr/m request; returns an encoded reply to # publish back to the requester instead of forwarding, or None to forward. @@ -83,6 +84,9 @@ def __init__( self._request_observer = request_observer # Given (duid, payload) for each rr/d reply; returns a replacement payload, or None to keep it. self._response_rewriter = response_rewriter + # Given (duid, payload) for a forwarded request; may return (delay, producer) for a reply to send if the robot stays silent. + self._request_fallback = request_fallback + self._fallback_tasks: set[asyncio.Task[None]] = set() self._host = host self._port = port self._logger = logger @@ -393,6 +397,28 @@ async def _publish_mirror( retain = bool(getattr(message, "retain", False)) await client.publish(target_topic, message.payload if payload is None else payload, qos=qos, retain=retain) + def _schedule_fallback(self, client: aiomqtt.Client, message: aiomqtt.Message, cloud_topic: CloudTopicKey) -> None: + if self._request_fallback is None or bool(getattr(message, "retain", False)): + return + try: + plan = self._request_fallback(cloud_topic.duid, bytes(message.payload)) + except Exception: # noqa: BLE001 + return + if plan is None: + return + delay, produce = plan + qos = _extract_qos(message) + + async def answer() -> None: + await asyncio.sleep(delay) + reply = produce() + if reply is not None: + await client.publish(cloud_topic.topic_out, reply, qos=qos) + + task = asyncio.get_running_loop().create_task(answer()) + self._fallback_tasks.add(task) + task.add_done_callback(self._fallback_tasks.discard) + async def _handle_cloud_message( self, client: aiomqtt.Client, @@ -446,6 +472,7 @@ async def _handle_cloud_message( except Exception: # Observation must never prevent forwarding. self._logger.warning("MQTT request observer failed for %s", src_topic, exc_info=True) await self._publish_mirror(client, message, target_topic) + self._schedule_fallback(client, message, cloud_topic) self._logger.info( "MQTT bridge m->d %s -> %s bytes=%d", src_topic, diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index db0f6774..47f9558f 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -26,6 +26,7 @@ import logging import re import time +from collections.abc import Callable from typing import TYPE_CHECKING, Any, NamedTuple from roborock.protocol import create_mqtt_decoder, create_mqtt_encoder @@ -66,6 +67,8 @@ class ForwardToRobot(Exception): _PATROL_ACTIONS = frozenset({"app_start_patrol", "start_patrol"}) _STATUS_WORDS = frozenset({"on", "off", "disable"}) _PENDING_SECONDS = 30.0 +# How long a forwarded get_timer waits for the robot before the store answers alone. +FALLBACK_SECONDS = 6.0 _NAME_RE = re.compile(r"[A-Za-z]{3}") _MONTH_NUMBERS = {name: number for number, name in enumerate( ("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"), 1)} @@ -95,8 +98,14 @@ def device_cron(cron: str) -> str | None: if (dom == "*") != (month == "*"): return None # Robot clients read digits, so day and month names (MON-FRI) are written out as numbers. - month = _NAME_RE.sub(lambda m: str(_MONTH_NUMBERS[m.group(0).upper()]), month) - dow = _NAME_RE.sub(lambda m: str(_DAY_NUMBERS[m.group(0).upper()]), dow) + try: + month = _NAME_RE.sub(lambda m: str(_MONTH_NUMBERS[m.group(0).upper()]), month) + dow = _NAME_RE.sub(lambda m: str(_DAY_NUMBERS[m.group(0).upper()]), dow) + if "7" in dow or any(int(a) > int(b) for a, b in re.findall(r"(\d)-(\d)", dow)): + # Clients that only know 0-6 get Sunday as 0 and no wrapping ranges. + dow = ",".join(str(day) for day in sorted(parse_cron(f"0 0 * * {dow}").days_of_week)) + except (KeyError, CronError): + return None return f"{minute} {hour} {dom} {month} {dow}" @@ -107,6 +116,7 @@ def _timer_entry(job: dict[str, Any]) -> list[Any] | None: return None cron = device_cron(str(job.get("cron") or "")) if cron is None: + _LOGGER.debug("Schedule %s has a cron get_timer cannot show: %r", job.get("id"), job.get("cron")) return None enabled = job.get("enabled", True) on = enabled not in (False, 0, "0", "false", "False") @@ -207,6 +217,14 @@ def handle_timer_request( raise TimerRequestError(f"not a timer method: {method!r}") +class _Pending(NamedTuple): + expires: float + entries: list[Any] + seq: int | None + version: bytes | None + answered: list[bool] + + class TimerRpcInterceptor: """Bridge hook: turn an encrypted timer request into an encrypted reply, or None to forward.""" @@ -214,7 +232,7 @@ def __init__(self, context: ServerContext, *, default_timezone: str = "") -> Non self._context = context self._default_timezone = default_timezone # get_timer requests forwarded to the robot whose reply needs the store's entries added. - self._pending: dict[tuple[str, Any], tuple[float, list[Any]]] = {} + self._pending: dict[tuple[str, Any], _Pending] = {} def _localkey(self, duid: str) -> str: creds = self._context.runtime_credentials @@ -259,14 +277,36 @@ def __call__(self, duid: str, payload: bytes) -> bytes | None: if method == "get_timer" and "result" in body: # The robot may hold legacy timers of its own: let it answer and add the store's entries. if body["result"]: - self._remember_pending(duid, request_id, body["result"]) + self._remember_pending(duid, request_id, body["result"], message) return None return _encode_reply(localkey, message, body) - def _remember_pending(self, duid: str, request_id: Any, entries: list[Any]) -> None: + def _remember_pending(self, duid: str, request_id: Any, entries: list[Any], message: RoborockMessage) -> None: now = time.monotonic() - self._pending = {key: value for key, value in self._pending.items() if value[0] > now} - self._pending[(duid, request_id)] = (now + _PENDING_SECONDS, entries) + self._pending = {key: value for key, value in self._pending.items() if value.expires > now} + self._pending[(duid, request_id)] = _Pending(now + _PENDING_SECONDS, entries, message.seq, message.version, [False]) + + def request_fallback(self, duid: str, payload: bytes) -> tuple[float, Callable[[], bytes | None]] | None: + """For a get_timer just forwarded: a delay and a producer of the store-only reply if the robot stays silent.""" + localkey = self._localkey(duid) + try: + (message,) = create_mqtt_decoder(localkey)(payload) + request = _dps_body(message, RoborockMessageProtocol.RPC_REQUEST) + except Exception: # noqa: BLE001 + return None + key = (duid, request.get("id") if request else None) + pending = self._pending.get(key) + if request is None or request.get("method") != "get_timer" or pending is None: + return None + + def reply() -> bytes | None: + if pending.answered[0]: + return None + pending.answered[0] = True + self._pending.pop(key, None) + return _encode_reply(localkey, message, {"id": key[1], "result": pending.entries}) + + return FALLBACK_SECONDS, reply def merge_response(self, duid: str, payload: bytes) -> bytes | None: """Add the store's timers to the robot's get_timer reply, or None to pass it on unchanged.""" @@ -288,11 +328,14 @@ def merge_response(self, duid: str, payload: bytes) -> bytes | None: if body is None: return None pending = self._pending.get((duid, body.get("id"))) - if pending is None or pending[0] <= time.monotonic(): + if pending is None or pending.expires <= time.monotonic(): + return None + pending.answered[0] = True + if body.get("error") is not None: return None theirs = body.get("result") if isinstance(body.get("result"), list) else [] known = {str(entry[0]) for entry in theirs if isinstance(entry, list) and entry} - merged = [*theirs, *(entry for entry in pending[1] if str(entry[0]) not in known)] + merged = [*theirs, *(entry for entry in pending.entries if str(entry[0]) not in known)] return _encode_reply(localkey, message, {"id": body.get("id"), "result": merged}) diff --git a/src/roborock_local_server/server.py b/src/roborock_local_server/server.py index 517041a1..ab8ebab4 100644 --- a/src/roborock_local_server/server.py +++ b/src/roborock_local_server/server.py @@ -2025,6 +2025,7 @@ async def start(self) -> None: request_observer=self.schedule_sync.observe_request, request_interceptor=timer_rpc, response_rewriter=timer_rpc.merge_response, + request_fallback=timer_rpc.request_fallback, ) await self._topic_bridge.start() self.runtime_state.set_service("mqtt_topic_bridge", running=True, required=False, enabled=True) diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py index 46f78234..e418b0e0 100644 --- a/tests/test_timer_rpc.py +++ b/tests/test_timer_rpc.py @@ -436,3 +436,81 @@ def test_get_timer_shows_day_names_as_numbers(ctx): timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["named", ["on", ["0 8 * * MON-FRI", ["start_clean", {}]]]]]) listed = timer_rpc.handle_timer_request(ctx, DUID, "get_timer", None) assert next(entry for entry in listed if entry[0] == "named")[2][0] == "0 8 * * 1-5" + + +def _bridge_with_fallback(ctx, interceptor): + return MqttTopicBridge( + host="127.0.0.1", port=1883, logger=logging.getLogger("test.timer_fallback"), + request_interceptor=interceptor, response_rewriter=interceptor.merge_response, + request_fallback=interceptor.request_fallback, + ) + + +def test_get_timer_is_answered_by_the_store_when_the_robot_stays_silent(ctx, monkeypatch): + monkeypatch.setattr(timer_rpc, "FALLBACK_SECONDS", 0.02) + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + bridge = _bridge_with_fallback(ctx, interceptor) + bridge._remember_device_seen(DeviceTopicKey(did="robot-did", mqtt_usr="robot-user")) + cloud = CloudTopicKey(rriot_u="u", mqtt_username="m", duid=DUID) + sent = [] + + class Client: + async def publish(self, topic, payload, **kwargs): + sent.append((topic, payload)) + + async def exercise(): + request = SimpleNamespace(payload=_encode_request("get_timer", [], request_id=61), qos=0, retain=False) + await bridge._handle_cloud_message(Client(), request, cloud, cloud.topic_in) + await asyncio.sleep(0.1) + + asyncio.run(exercise()) + assert [topic for topic, _ in sent][-1] == cloud.topic_out + reply = _decode_reply(sent[-1][1]) + assert reply.request_id == 61 + assert [entry[0] for entry in reply.data] == ["3549431", "3549434", "3878757"] + + +def test_no_fallback_reply_is_sent_once_the_robot_has_answered(ctx, monkeypatch): + monkeypatch.setattr(timer_rpc, "FALLBACK_SECONDS", 0.05) + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + bridge = _bridge_with_fallback(ctx, interceptor) + device = DeviceTopicKey(did="robot-did", mqtt_usr="robot-user") + bridge._remember_device_seen(device) + cloud = CloudTopicKey(rriot_u="u", mqtt_username="m", duid=DUID) + sent = [] + + class Client: + async def publish(self, topic, payload, **kwargs): + sent.append(topic) + + async def exercise(): + request = SimpleNamespace(payload=_encode_request("get_timer", [], request_id=62), qos=0, retain=False) + await bridge._handle_cloud_message(Client(), request, cloud, cloud.topic_in) + reply = SimpleNamespace(payload=_encode_robot_reply(62, []), qos=0, retain=False) + await bridge._handle_device_message(Client(), reply, device, device.topic_in) + await asyncio.sleep(0.15) + + asyncio.run(exercise()) + assert sent.count(cloud.topic_out) == 1 + + +def test_a_robot_error_reply_to_get_timer_is_passed_through(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + interceptor(DUID, _encode_request("get_timer", [], request_id=63)) + body = json.dumps({"id": 63, "error": {"code": -10005, "message": "unsupported"}}) + payload = json.dumps({"t": 1790000000, "dps": {"102": body}}).encode() + frame = create_mqtt_encoder(LOCALKEY)(RoborockMessage(protocol=RoborockMessageProtocol.RPC_RESPONSE, payload=payload)) + assert interceptor.merge_response(DUID, frame) is None + + +def test_one_job_with_an_unreadable_cron_does_not_hide_the_other_timers(ctx): + schedule_store.upsert_job(ctx, DUID, job_id="bad", cron="0 0 * * ABC", enabled=True) + assert timer_rpc.device_cron("0 0 * * ABC") is None + listed = timer_rpc.handle_timer_request(ctx, DUID, "get_timer", None) + assert [entry[0] for entry in listed] == ["3549431", "3549434", "3878757"] + + +def test_get_timer_writes_sunday_as_zero(ctx): + assert timer_rpc.device_cron("0 8 * * 5-7") == "0 8 * * 0,5,6" + assert timer_rpc.device_cron("0 8 * * FRI-SUN") == "0 8 * * 0,5,6" + assert timer_rpc.device_cron("0 8 * * 1,7") == "0 8 * * 0,1" From e3ca33785a5c0fea5c220ab8ee8daf0626280f8d Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:58:35 -0500 Subject: [PATCH 20/27] Shorten long comments and merge the two missed-run sections Comments and docstrings added by this PR are cut to two lines, and the two overlapping missed-run sections of schedules.md are one. The docs also state the get_timer fallback. Co-Authored-By: Claude Sonnet 5.5 --- docs/schedules.md | 17 +++++----- .../bundled_backend/shared/inventory_io.py | 11 ++----- .../shared/native_schedule_sync.py | 14 ++------ .../bundled_backend/shared/native_timers.py | 3 +- .../bundled_backend/shared/routine_runner.py | 19 +++-------- .../bundled_backend/shared/schedule_runner.py | 13 ++------ .../bundled_backend/shared/schedule_store.py | 32 +++---------------- .../bundled_backend/shared/timer_rpc.py | 26 ++------------- 8 files changed, 28 insertions(+), 107 deletions(-) diff --git a/docs/schedules.md b/docs/schedules.md index 53e17555..a407dfde 100644 --- a/docs/schedules.md +++ b/docs/schedules.md @@ -15,7 +15,7 @@ restoration, daily reset, and recovery. The server provides the underlying APIs. | PUT/PATCH `/user/devices/{duid}/jobs/{id}` | Verify native enablement when supplied, then save; preserve omitted fields and unknown model fields; `result` is `"done"` | | DELETE an item | Remove the stored job and its occurrence record; `result` is `"done"` | | `get/set/upd/del_server_timer` | Forward unchanged to the robot, which owns these timer entries | -| Legacy `get/set/upd/del_timer` | `set_timer` and the `upd_timer`/`del_timer` of a stored job are answered from the store. `get_timer` goes to the robot and the store's jobs are added to its reply. Anything about a timer the store does not hold, and patrol timers, go to the robot | +| Legacy `get/set/upd/del_timer` | `set_timer` and the `upd_timer`/`del_timer` of a stored job are answered from the store. `get_timer` goes to the robot and the store's jobs are added to its reply (if the robot has not answered after 6 seconds, the store's jobs are sent alone; a robot error reply is passed through). Anything about a timer the store does not hold, and patrol timers, go to the robot | | Native scheduled execution | Verify the robot timer is on, then send `server_scheduled_start` with the complete settings entry | A job's numeric `id` is different from the robot timer's string ID in @@ -87,9 +87,8 @@ cron/settings by this enablement reconciliation. ## Execution and missed runs The scheduler evaluates five-field cron expressions in each job's IANA timezone. -It checks approximately every 20 seconds, allowing less than 60 seconds of normal -dispatch latency. Startup establishes a fresh baseline; suspended/late ticks do -not drain historical jobs. Recurring jobs remain enabled for future occurrences. +It checks approximately every 20 seconds and starts a job whose occurrence fell +inside the last tick, with up to 60 seconds of allowed lateness. Before dispatch, the server saves an occurrence claim in the persistent inventory under `schedule_occurrences`. A repeated tick, restart, or backward clock change @@ -100,10 +99,9 @@ inventory lock does not coordinate multiple containers or external file editors. Cloud re-import replaces schedule definitions, so back up local edits before refreshing. Occurrence claims are retained across that refresh. -## Missed runs +### Missed runs -The scheduler only starts a job for an occurrence that falls inside the last tick, plus up -to 60 seconds of lateness. There is no catch-up: +There is no catch-up: - The first tick after startup only sets a baseline. A job due while the server was stopped, restarting, or suspended is lost, and so is one due during an event-loop stall longer than @@ -113,10 +111,13 @@ to 60 seconds of lateness. There is no catch-up: another routine or schedule is running on that robot, or if the 60 second dispatch window has passed by the time the robot has answered, that occurrence is skipped and is not retried later. -- A recurring job stays enabled and runs at its next occurrence. +- A recurring job stays enabled and runs at its next occurrence. Day and month names + (`MON-FRI`, `JAN`) work in crons, and so do ranges that wrap past Sunday (`FRI-SUN`, `6-0`). - A one-time job (`repeated` false) is disabled when its occurrence is claimed, whether or not the clean then started. It does not run again, even after a failure. +### Native schedules + A fresh native timer read must find exactly one matching `on` entry. Off, missing, ambiguous, malformed, or unreachable timer state prevents a start. After that network wait, dispatch rechecks the stored job and its deadline, so a concurrent diff --git a/src/roborock_local_server/bundled_backend/shared/inventory_io.py b/src/roborock_local_server/bundled_backend/shared/inventory_io.py index 6a2ffd20..1fb9d045 100644 --- a/src/roborock_local_server/bundled_backend/shared/inventory_io.py +++ b/src/roborock_local_server/bundled_backend/shared/inventory_io.py @@ -22,11 +22,7 @@ @contextmanager def inventory_transaction(path: Path): - """Serialize inventory read/modify/write within this server process. - - Refuse to mutate corrupt existing inventory rather than replacing it with - an empty fallback. External writers must stop the server first. - """ + """Serialize inventory read/modify/write in this process; a corrupt existing file is refused, not replaced.""" path = path.resolve() with _LOCKS_GUARD: lock = _LOCKS.setdefault(path, threading.RLock()) @@ -76,10 +72,7 @@ def load_inventory(ctx: ServerContext) -> dict[str, Any]: class CachedInventory: - """Re-reads the inventory file only when its size, modification time or file id changed. - - The returned dict is shared: callers must treat it as read-only. - """ + """Re-reads the inventory only when its size, mtime or file id changed; the shared dict is read-only.""" def __init__(self, ctx: ServerContext) -> None: self._ctx = ctx diff --git a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py index 82d39d19..84a7ff48 100644 --- a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py +++ b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py @@ -45,12 +45,7 @@ def _timer_state(timers: Any, name: str) -> str: async def sync_timer( context: Any, duid: str, name: str, enabled: bool, *, check_current: Callable[[], None], ) -> str: - """Update only the named timer; a fresh read is the success criterion. - - The app sends upd_server_timer before its HTTP job update. A timed-out write - may still have reached the vacuum, so read it back without repeating the - write. Never create a timer or infer one from a cloud job's numeric ID. - """ + """Update only the named timer; a fresh read is the success criterion, and a lost ack is read back, not resent.""" from shared.routine_runner import _RoutineMqttClient device = context.runtime_credentials.resolve_device(duid=duid) @@ -115,12 +110,7 @@ def timer_states(timers: Any) -> dict[str, bool]: class NativeScheduleSync: - """One coordinator for API edits and read-only reconciliation from any client. - - Native RPCs still reach the robot unchanged. Observing a write only schedules - a fresh read; no optimistic request or generic 'ok' reply mutates inventory. - Periodic reads also catch LAN changes and recover after missed MQTT traffic. - """ + """Reconciles native schedule enablement for every client; an observed write only triggers a fresh read.""" INTERVAL = 30.0 DEBOUNCE = 0.3 diff --git a/src/roborock_local_server/bundled_backend/shared/native_timers.py b/src/roborock_local_server/bundled_backend/shared/native_timers.py index 5512af95..84ef03ba 100644 --- a/src/roborock_local_server/bundled_backend/shared/native_timers.py +++ b/src/roborock_local_server/bundled_backend/shared/native_timers.py @@ -22,8 +22,7 @@ def native_timer_name(entry_id: str) -> str: def parse_native_timers(timers: Any) -> list[NativeTimer]: - """Most robots answer [name, "on"|"off", ...]. An S6 MaxV (a10, 02.62.46) answers - ["Y 1 1 app_start", 0] for an on timer and ["Y ...", "off", 0] for an off one.""" + """Reads [name, "on"|"off", ...] and the S6 MaxV form ["Y 1 1 app_start", 0] (["Y ...", "off", 0] when off).""" found: list[NativeTimer] = [] if not isinstance(timers, list): return found diff --git a/src/roborock_local_server/bundled_backend/shared/routine_runner.py b/src/roborock_local_server/bundled_backend/shared/routine_runner.py index a09d94bd..39bc7190 100644 --- a/src/roborock_local_server/bundled_backend/shared/routine_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/routine_runner.py @@ -66,11 +66,7 @@ def _ensure_local_python_roborock_on_path() -> None: class SchedulerCommand(StrEnum): """A robot command python-roborock's RoborockCommand does not list.""" - # The cloud's own way to run a schedule: the robot answers it with the schedule's settings entry - # (the same one the cloud job carries, including "name", the robot-side timer id) and records the - # clean as "Schedules" in its history, for whole-home and room schedules alike. Confirmed live; - # without the entry it answers -10005 "Invalid timer", and with a bare id "Param should be - # name + fan + segment". + # The cloud's way to run a schedule; it needs the job's whole settings entry, "name" included (confirmed live). SERVER_SCHEDULED_START = "server_scheduled_start" GET_SERVER_TIMER = "get_server_timer" @@ -92,15 +88,10 @@ class SchedulerCommand(StrEnum): } _ACTION_LOCK_ERROR_CODES = {-10003, -10007} _PARAM_ERROR_CODE = -10005 -# What app_start takes in one command, as the official app's own schedule entries carry it. A -# robot accepted every one of these together and applied the modes (confirmed live); it answers -# -10005 "Param error" to a value it does not support, such as a route its model lacks. +# What app_start takes in one command, as the official app's schedule entries carry it (confirmed live). _APP_START_KEYS = ("repeat", "water_box_mode", "segments", "fan_power", "mop_mode", "clean_mop", "map_index") _APP_START_DEFAULTS = {"repeat": 1, "segments": "0", "clean_mop": 1, "map_index": -1} -# app_start's "source" field is how the robot learns who started a clean, for its history. 3 is -# python-roborock's RoborockStartType.schedule, and a robot recorded the clean as "Schedule" in the -# official app's history; an app start is 2. "start_type", "startType", "start_from", "from" and -# "trigger" were all accepted but ignored, so the clean was recorded as "App". +# app_start "source" 3 (RoborockStartType.schedule) makes the robot's history say "Schedule"; other spellings are ignored. _SCHEDULE_START_SOURCE = 3 _RESUME_COMMAND_BY_IN_CLEANING: dict[int, RoborockCommand] = { RoborockInCleaning.global_clean_not_complete.value: RoborockCommand.APP_START, @@ -1134,9 +1125,7 @@ async def _run_steps( if step.method == SCHEDULE_SERVER_START: timer_name = _single_start_entry(step).get("name") timers = await client.send_command(SchedulerCommand.GET_SERVER_TIMER, []) - # The cloud job's enabled field can remain true when the - # robot timer is off. A failed/malformed read must not start - # cleaning, nor enqueue a start for an offline robot. + # The job can stay enabled while the robot timer is off; an unreadable list must not start a clean. if not isinstance(timers, list): raise RoutineExecutionError("Cannot verify native schedule timer state") matching = [timer for timer in parse_native_timers(timers) if timer.name == timer_name] diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index ebef03e6..0b618515 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -1,9 +1,4 @@ -"""Local executor for cloud-style schedules (``server_scheduled_start`` jobs). - -Roborock's cloud fires these at ``nextFireTime``. With the vacuum pointed at this -server, nothing else would, so the server evaluates the stored cron entries -itself and hands each due job to the routine runner. -""" +"""Local executor for cloud-style schedules: evaluates stored crons and hands each due job to the routine runner.""" from __future__ import annotations @@ -145,11 +140,7 @@ def parse_cron(expression: str) -> CronSpec: def next_fire(spec: CronSpec, after: datetime) -> datetime | None: - """Next wall-clock match; skip DST gaps and use only the first repeated hour. - - This explicit DST policy is provisional until compared with cloud behavior. - Compare UTC instants because local datetime arithmetic can hide DST folds. - """ + """Next wall-clock match; skips DST gaps and uses only the first repeated hour (provisional policy).""" zone = after.tzinfo if zone is None: raise ValueError("after must be timezone-aware") diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_store.py b/src/roborock_local_server/bundled_backend/shared/schedule_store.py index b1af23b1..aa21634f 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_store.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_store.py @@ -99,11 +99,7 @@ def reconcile_native_enabled( ctx: ServerContext, duid: str, snapshot: list[dict[str, Any]], states: dict[str, bool], *, explicit_enable: dict[str, Any] | None = None, ) -> list[Any]: - """Merge a fresh robot read only into unchanged, uniquely linked jobs. - - Preserve complete records and occurrence claims. A still-on robot timer - must not resurrect a consumed one-time job after a restart or routine poll. - """ + """Merge a robot read into unchanged, uniquely linked jobs; a still-on timer never revives a consumed one-time job.""" inventory = load_inventory(ctx) jobs = _jobs_for(inventory, duid, create=False) or [] by_name: dict[str, list[dict[str, Any]]] = {} @@ -142,14 +138,7 @@ def reconcile_native_enabled( def job_settings(job: dict[str, Any]) -> dict[str, Any]: - """The vac/mop settings a job carries, as a flat dict. - - ``name``, when the job has one, rides along too, outside ``JOB_SETTING_KEYS``: it is not a - vac/mop setting a client edits, it is the official app's own link from this job to the - robot's server_timer entry it saved alongside it (POST/PUT /user/devices/{duid}/jobs carries - it in exactly this spot, confirmed live). Native timer entries themselves are - read from the robot; this helper does not turn them into legacy timers. - """ + """The vac/mop settings a job carries; ``name``, the app's link to the robot's server_timer entry, rides along.""" entry = _settings_entry(job) settings = {key: entry[key] for key in JOB_SETTING_KEYS if key in entry} name = entry.get("name") @@ -159,10 +148,7 @@ def job_settings(job: dict[str, Any]) -> dict[str, Any]: def merge_job_settings(param: dict[str, Any] | None, settings: dict[str, Any]) -> dict[str, Any]: - """Copy of ``param`` with ``settings`` merged in; a None value clears that setting. - - Settings not mentioned are left alone, so editing one option never resets the rest. - """ + """Copy of ``param`` with ``settings`` merged in; None clears a setting and unmentioned ones are kept.""" merged = copy.deepcopy(param) if isinstance(param, dict) else copy.deepcopy(DEFAULT_JOB_PARAM) params = merged.get("params") if not isinstance(params, list) or not params or not isinstance(params[0], dict): @@ -230,10 +216,7 @@ def upsert_job( create_only: bool = False, expected_job: dict[str, Any] | None = None, ) -> dict[str, Any]: - """Create the job, or update cron/enabled on an existing one while keeping its settings. - - An existing job keeps its own timezone unless ``override_time_zone`` is set. - """ + """Create the job, or update an existing one keeping its settings and (unless overridden) its timezone.""" inventory = load_inventory(ctx) jobs = _jobs_for(inventory, duid, create=True) assert jobs is not None @@ -306,12 +289,7 @@ def delete_job(ctx: ServerContext, duid: str, job_id: Any) -> bool: @inventory_mutation def claim_occurrence(ctx: ServerContext, duid: str, job: dict[str, Any], due: datetime) -> bool: - """Persist an at-most-once decision before any robot command is sent. - - A crash after this write can skip a clean; replaying a clean after restart is - deliberately avoided. Compare the snapshot under the same lock as CRUD so a - disabled, edited, or deleted job cannot be claimed from a stale tick. - """ + """Persist an at-most-once claim before any command is sent; a stale, edited or disabled job cannot be claimed.""" inventory = load_inventory(ctx) jobs = _jobs_for(inventory, duid, create=False) or [] current = next((item for item in jobs if _same_id(item, job.get("id"))), None) diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index 47f9558f..e0f6142b 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -1,23 +1,6 @@ """Answer the vacuum's legacy timer RPCs (get/set/upd/del_timer) from the schedule store. -On cloud-managed robots the schedules live server-side, not on the robot, so -clients such as LocalRock that ask the robot for its timers would otherwise see -nothing and could never change the schedules the cloud imported. The topic -bridge hands matching requests here instead of forwarding them to the robot. Requests about a -timer the store does not own (a robot's own legacy timer, a patrol timer) still go to the robot, -and get_timer replies from the robot are merged with the store's entries. - -Deliberately not handled here: get/set/upd/del_server_timer, the newer sibling -protocol some firmware speaks instead of (or alongside) this one. The robot -stores those entries itself and the official app reads them back from it, so -they must always forward to the robot untouched. Answering them from this store -instead, tried once, hid the robot's real schedules from every client reading -through the server, and the official app then deleted them (it removes -server_timer entries it does not recognize when its schedule screen opens). - -What runs a server_timer schedule is the cloud's own record of it, the job the -official app also writes to /user/devices/{duid}/jobs, which ScheduleRunner -fires like any other. The robot was not seen to run them itself. +get/set/upd/del_server_timer always go to the robot, which owns those entries; the server runs them from their /jobs record. """ from __future__ import annotations @@ -84,12 +67,9 @@ class _SetTimer(NamedTuple): def device_cron(cron: str) -> str | None: - """Cron in the robot's own 'm h dom month dow' form, with '?' written as '*'. + """Cron in the robot's 'm h dom month dow' form ('?' as '*'), or None when it has no such form. - A one-time schedule pins the day of the month and the month ('15 23 18 10 *'), exactly as - the robot and the official app write it (confirmed live), so it is shown like any other. - A job that pins only one of the two has no such form and is left out of get_timer rather - than shown wrong. + A one-time schedule pins day and month ('15 23 18 10 *'); a job pinning only one of them has no form. """ fields = str(cron or "").split() if len(fields) != 5: From 5d3bba8e177ddca1d886c380530ef2dd6d34d65c Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:10:10 -0500 Subject: [PATCH 21/27] Resolve the schedule time zone through our own helper PR 294 deletes shared.constants.DEFAULT_TIMEZONE, which jobs.py and schedule_runner.py import, so merging both broke test collection. server_timezone() uses data_helpers.default_timezone(ctx) when it exists (294) and otherwise the configured zone, then America/New_York, so the branch works with and without 294. ScheduleRunner and the timer interceptor now fall back to it instead of a fixed New York default, so EU installs get their region's zone once 294 lands. Co-Authored-By: Claude Sonnet 5.5 --- .../https_server/routes/user/devices/jobs.py | 5 ++--- .../bundled_backend/shared/schedule_runner.py | 17 +++++++++++++---- .../bundled_backend/shared/timer_rpc.py | 5 ++--- tests/test_schedule_runner.py | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py index 5a983c30..90ddb968 100644 --- a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py +++ b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py @@ -5,11 +5,10 @@ from typing import Any from zoneinfo import ZoneInfo, ZoneInfoNotFoundError -from shared.constants import DEFAULT_TIMEZONE from shared.context import ServerContext from shared.data_helpers import as_bool from shared.http_helpers import wrap_response -from shared.schedule_runner import parse_cron +from shared.schedule_runner import parse_cron, server_timezone from shared.schedule_store import SettingsError, delete_job, list_jobs, native_timer_name, upsert_job from shared.native_schedule_sync import TimerSyncError, sync_timer @@ -84,7 +83,7 @@ def build_create( body = _json_body(body_params) cron = str(body.get("cron") or "").strip() parse_cron(cron) - zone = _validate_zone(body.get("timeZoneId", getattr(ctx, "timezone", "") or DEFAULT_TIMEZONE)) + zone = _validate_zone(body.get("timeZoneId", getattr(ctx, "timezone", "") or server_timezone(ctx))) except ValueError as exc: return _error(400, str(exc)) param = _parse_param(body.get("param")) diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index 0b618515..c069ac4f 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -11,7 +11,7 @@ from typing import TYPE_CHECKING, Any from zoneinfo import ZoneInfo, ZoneInfoNotFoundError -from .constants import DEFAULT_TIMEZONE +from . import data_helpers from .data_helpers import as_bool from .inventory_io import CachedInventory, load_inventory # noqa: F401 (load_inventory stays importable from here) from .routine_runner import ( @@ -28,6 +28,8 @@ _LOGGER = logging.getLogger(__name__) +_LAST_RESORT_ZONE = "America/New_York" + TICK_SECONDS = 20 MAX_LATENESS_SECONDS = 60 _SUPPORTED_JOB_METHOD = "server_scheduled_start" @@ -162,9 +164,16 @@ def next_fire(spec: CronSpec, after: datetime) -> datetime | None: return None +def server_timezone(ctx: Any) -> str: + """The server's zone: network.timezone, else the region's; works with or without data_helpers.default_timezone.""" + region_default = getattr(data_helpers, "default_timezone", None) + if region_default is not None: + return str(region_default(ctx)) + return str(getattr(ctx, "timezone", "") or "").strip() or _LAST_RESORT_ZONE + + def _zone(name: str, fallback: str) -> tzinfo: - # DEFAULT_TIMEZONE is what /location tells the robot when none is configured. - candidate = str(name or fallback or DEFAULT_TIMEZONE).strip() + candidate = str(name or fallback or _LAST_RESORT_ZONE).strip() return ZoneInfo(candidate) @@ -271,7 +280,7 @@ def next_fire_time_utc(job: dict[str, Any], *, default_timezone: str = "", now: class ScheduleRunner: def __init__(self, context: ServerContext, *, default_timezone: str = "") -> None: self._context = context - self._default_timezone = default_timezone + self._default_timezone = default_timezone or server_timezone(context) self._last_tick: datetime | None = None self._inventory = CachedInventory(context) self._logger = context.loggers.get("api") or context.loggers.get("unknown") or _LOGGER diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index e0f6142b..6465df70 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -15,8 +15,7 @@ from roborock.protocol import create_mqtt_decoder, create_mqtt_encoder from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol -from .constants import DEFAULT_TIMEZONE -from .schedule_runner import CronError, parse_cron +from .schedule_runner import CronError, parse_cron, server_timezone from .schedule_store import ( JOB_SETTING_KEYS, SettingsError, @@ -177,7 +176,7 @@ def handle_timer_request( param = merge_job_settings(existing.get("param") if existing else None, settings) except SettingsError as exc: raise TimerRequestError(str(exc)) from exc - zone = str(getattr(ctx, "timezone", "") or default_timezone or DEFAULT_TIMEZONE) + zone = str(getattr(ctx, "timezone", "") or default_timezone or server_timezone(ctx)) upsert_job(ctx, duid, job_id=job_id, cron=cron, enabled=enabled, time_zone=zone, param=param) return list(_OK) diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py index 315df765..a0718f5a 100644 --- a/tests/test_schedule_runner.py +++ b/tests/test_schedule_runner.py @@ -338,3 +338,18 @@ def test_weekday_ranges_that_reach_or_wrap_past_sunday(expression, weekdays): def test_a_day_of_month_step_starting_with_star_is_unrestricted_like_vixie_cron(): spec = sr.parse_cron("0 8 */2 * MON") assert spec.dom_restricted is False and spec.dow_restricted is True + + +def test_server_timezone_prefers_the_configured_zone(): + assert sr.server_timezone(SimpleNamespace(timezone="Europe/Vienna", region="us")) == "Europe/Vienna" + + +def test_server_timezone_without_the_region_helper_falls_back_to_new_york(monkeypatch): + # Main has no data_helpers.default_timezone (the branch that adds it is not merged here). + monkeypatch.delattr(sr.data_helpers, "default_timezone", raising=False) + assert sr.server_timezone(SimpleNamespace(timezone="", region="eu")) == NEW_YORK + + +def test_server_timezone_uses_the_region_helper_when_present(monkeypatch): + monkeypatch.setattr(sr.data_helpers, "default_timezone", lambda ctx: "Europe/Berlin", raising=False) + assert sr.server_timezone(SimpleNamespace(timezone="", region="eu")) == "Europe/Berlin" From 19555f21c72df006b843c624828c784567df0054 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:12:20 -0500 Subject: [PATCH 22/27] Let server_timezone tolerate a context without timezone With 294 merged, default_timezone(ctx) reads ctx.timezone and raised on the minimal contexts several tests and callers pass. Fall back to the configured zone and then America/New_York instead of failing. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/schedule_runner.py | 5 ++++- tests/test_schedule_runner.py | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index c069ac4f..06dfa4ac 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -168,7 +168,10 @@ def server_timezone(ctx: Any) -> str: """The server's zone: network.timezone, else the region's; works with or without data_helpers.default_timezone.""" region_default = getattr(data_helpers, "default_timezone", None) if region_default is not None: - return str(region_default(ctx)) + try: + return str(region_default(ctx)) + except AttributeError: # a context without timezone or region + pass return str(getattr(ctx, "timezone", "") or "").strip() or _LAST_RESORT_ZONE diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py index a0718f5a..610af641 100644 --- a/tests/test_schedule_runner.py +++ b/tests/test_schedule_runner.py @@ -353,3 +353,11 @@ def test_server_timezone_without_the_region_helper_falls_back_to_new_york(monkey def test_server_timezone_uses_the_region_helper_when_present(monkeypatch): monkeypatch.setattr(sr.data_helpers, "default_timezone", lambda ctx: "Europe/Berlin", raising=False) assert sr.server_timezone(SimpleNamespace(timezone="", region="eu")) == "Europe/Berlin" + + +def test_server_timezone_survives_a_context_the_region_helper_cannot_read(monkeypatch): + def region_default(ctx): + return ctx.timezone + + monkeypatch.setattr(sr.data_helpers, "default_timezone", region_default, raising=False) + assert sr.server_timezone(SimpleNamespace()) == NEW_YORK From cbb66739f58dc5a8d84717b930458bd454686504 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:14:52 -0500 Subject: [PATCH 23/27] Show the store's timers when the robot rejects get_timer A robot that answers get_timer with an error (firmware without the legacy timers) made the app show that error and none of the store's schedules. Reply with the store's timers instead; with an empty store the error still passes through unchanged. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/timer_rpc.py | 5 ++-- tests/test_timer_rpc.py | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index 6465df70..d9dd9f5d 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -310,9 +310,10 @@ def merge_response(self, duid: str, payload: bytes) -> bytes | None: if pending is None or pending.expires <= time.monotonic(): return None pending.answered[0] = True - if body.get("error") is not None: + # A robot that rejects get_timer still has the store's timers to show; no error reaches the app then. + theirs = body.get("result") if body.get("error") is None and isinstance(body.get("result"), list) else [] + if body.get("error") is not None and not pending.entries: return None - theirs = body.get("result") if isinstance(body.get("result"), list) else [] known = {str(entry[0]) for entry in theirs if isinstance(entry, list) and entry} merged = [*theirs, *(entry for entry in pending.entries if str(entry[0]) not in known)] return _encode_reply(localkey, message, {"id": body.get("id"), "result": merged}) diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py index e418b0e0..f9f405cb 100644 --- a/tests/test_timer_rpc.py +++ b/tests/test_timer_rpc.py @@ -494,13 +494,26 @@ async def exercise(): assert sent.count(cloud.topic_out) == 1 -def test_a_robot_error_reply_to_get_timer_is_passed_through(ctx): +def _robot_error_frame(request_id): + body = json.dumps({"id": request_id, "error": {"code": -10005, "message": "unsupported"}}) + payload = json.dumps({"t": 1790000000, "dps": {"102": body}}).encode() + return create_mqtt_encoder(LOCALKEY)(RoborockMessage(protocol=RoborockMessageProtocol.RPC_RESPONSE, payload=payload)) + + +def test_a_robot_error_reply_to_get_timer_still_shows_the_stores_timers(ctx): interceptor = timer_rpc.TimerRpcInterceptor(ctx) interceptor(DUID, _encode_request("get_timer", [], request_id=63)) - body = json.dumps({"id": 63, "error": {"code": -10005, "message": "unsupported"}}) - payload = json.dumps({"t": 1790000000, "dps": {"102": body}}).encode() - frame = create_mqtt_encoder(LOCALKEY)(RoborockMessage(protocol=RoborockMessageProtocol.RPC_RESPONSE, payload=payload)) - assert interceptor.merge_response(DUID, frame) is None + reply = _decode_reply(interceptor.merge_response(DUID, _robot_error_frame(63))) + assert reply.api_error is None + assert [entry[0] for entry in reply.data] == ["3549431", "3549434", "3878757"] + + +def test_a_robot_error_reply_with_nothing_in_the_store_is_passed_through(ctx): + for job in schedule_store.list_jobs(ctx, DUID): + schedule_store.delete_job(ctx, DUID, str(job["id"])) + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + interceptor(DUID, _encode_request("get_timer", [], request_id=64)) + assert interceptor.merge_response(DUID, _robot_error_frame(64)) is None def test_one_job_with_an_unreadable_cron_does_not_hide_the_other_timers(ctx): From 152a1ad58487c4fcba3b1bb9d86fa7faa680d0df Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:16:40 -0500 Subject: [PATCH 24/27] Drop a pending get_timer entry once the robot has answered Entries lingered for 30 seconds after the merge and only left when the next get_timer pruned them. The reply path now removes its own entry; the fallback still sees the answered flag on the object it holds. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/timer_rpc.py | 1 + tests/test_timer_rpc.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index d9dd9f5d..3049398b 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -310,6 +310,7 @@ def merge_response(self, duid: str, payload: bytes) -> bytes | None: if pending is None or pending.expires <= time.monotonic(): return None pending.answered[0] = True + self._pending.pop((duid, body.get("id")), None) # A robot that rejects get_timer still has the store's timers to show; no error reaches the app then. theirs = body.get("result") if body.get("error") is None and isinstance(body.get("result"), list) else [] if body.get("error") is not None and not pending.entries: diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py index f9f405cb..2e377950 100644 --- a/tests/test_timer_rpc.py +++ b/tests/test_timer_rpc.py @@ -516,6 +516,14 @@ def test_a_robot_error_reply_with_nothing_in_the_store_is_passed_through(ctx): assert interceptor.merge_response(DUID, _robot_error_frame(64)) is None +def test_a_pending_get_timer_is_cleared_once_the_robot_has_answered(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + interceptor(DUID, _encode_request("get_timer", [], request_id=65)) + assert interceptor.merge_response(DUID, _encode_robot_reply(65, [])) is not None + assert interceptor._pending == {} + assert interceptor.merge_response(DUID, _encode_robot_reply(65, [])) is None + + def test_one_job_with_an_unreadable_cron_does_not_hide_the_other_timers(ctx): schedule_store.upsert_job(ctx, DUID, job_id="bad", cron="0 0 * * ABC", enabled=True) assert timer_rpc.device_cron("0 0 * * ABC") is None From 3a0a1757d351eb619540eb1ff99b61683eaef39f Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:20:24 -0500 Subject: [PATCH 25/27] Forward set_timer for a timer the robot listed instead of storing a copy The app edits a legacy timer by re-sending set_timer with its id. The store saved a copy while the robot kept the original, and the merged get_timer then hid the copy, so the edit looked lost (and a robot running its own timers could start twice). The interceptor now remembers the ids in each robot's last get_timer reply and forwards set_timer for one the store does not own. New ids still go to the store. The list is in memory, so it is empty after a server restart until the app reads the timer list again, which it does before offering an edit. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/timer_rpc.py | 27 ++++++++---- tests/test_timer_rpc.py | 43 +++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index 3049398b..fc21a616 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -9,7 +9,7 @@ import logging import re import time -from collections.abc import Callable +from collections.abc import Callable, Collection from typing import TYPE_CHECKING, Any, NamedTuple from roborock.protocol import create_mqtt_decoder, create_mqtt_encoder @@ -152,6 +152,7 @@ def handle_timer_request( params: Any, *, default_timezone: str = "", + robot_timers: Collection[str] = (), ) -> Any: """Apply one timer RPC to the store and return the RPC ``result`` value.""" if method == "get_timer": @@ -166,6 +167,10 @@ def handle_timer_request( parse_cron(cron) except CronError as exc: raise TimerRequestError(str(exc)) from exc + stored = {str(job.get("id")) for job in list_jobs(ctx, duid)} + if job_id in robot_timers and job_id not in stored: + # Editing a timer the robot listed: a copy in the store would run beside the original. + raise ForwardToRobot(f"timer {job_id} is owned by the robot") # An empty action object (what older clients send) leaves the job's settings alone; # keys it does carry are merged in, so a client can change one option at a time. settings = {key: value for key, value in action_settings.items() if key in JOB_SETTING_KEYS} @@ -212,6 +217,8 @@ def __init__(self, context: ServerContext, *, default_timezone: str = "") -> Non self._default_timezone = default_timezone # get_timer requests forwarded to the robot whose reply needs the store's entries added. self._pending: dict[tuple[str, Any], _Pending] = {} + # Ids of the timers each robot listed in its last get_timer reply. + self._robot_timers: dict[str, set[str]] = {} def _localkey(self, duid: str) -> str: creds = self._context.runtime_credentials @@ -242,6 +249,7 @@ def __call__(self, duid: str, payload: bytes) -> bytes | None: try: result = handle_timer_request( self._context, duid, method, request.get("params"), default_timezone=self._default_timezone, + robot_timers=self._robot_timers.get(duid, ()), ) body: dict[str, Any] = {"id": request_id, "result": result} except ForwardToRobot: @@ -255,8 +263,7 @@ def __call__(self, duid: str, payload: bytes) -> bytes | None: body = {"id": request_id, "error": {"code": -1, "message": "unable to save the schedule"}} if method == "get_timer" and "result" in body: # The robot may hold legacy timers of its own: let it answer and add the store's entries. - if body["result"]: - self._remember_pending(duid, request_id, body["result"], message) + self._remember_pending(duid, request_id, body["result"], message) return None return _encode_reply(localkey, message, body) @@ -275,7 +282,7 @@ def request_fallback(self, duid: str, payload: bytes) -> tuple[float, Callable[[ return None key = (duid, request.get("id") if request else None) pending = self._pending.get(key) - if request is None or request.get("method") != "get_timer" or pending is None: + if request is None or request.get("method") != "get_timer" or pending is None or not pending.entries: return None def reply() -> bytes | None: @@ -311,12 +318,16 @@ def merge_response(self, duid: str, payload: bytes) -> bytes | None: return None pending.answered[0] = True self._pending.pop((duid, body.get("id")), None) + failed = body.get("error") is not None + theirs = body.get("result") if not failed and isinstance(body.get("result"), list) else [] + known = {str(entry[0]) for entry in theirs if isinstance(entry, list) and entry} + if not failed: + self._robot_timers[duid] = known + extra = [entry for entry in pending.entries if str(entry[0]) not in known] # A robot that rejects get_timer still has the store's timers to show; no error reaches the app then. - theirs = body.get("result") if body.get("error") is None and isinstance(body.get("result"), list) else [] - if body.get("error") is not None and not pending.entries: + if not extra and not (failed and pending.entries): return None - known = {str(entry[0]) for entry in theirs if isinstance(entry, list) and entry} - merged = [*theirs, *(entry for entry in pending.entries if str(entry[0]) not in known)] + merged = [*theirs, *extra] return _encode_reply(localkey, message, {"id": body.get("id"), "result": merged}) diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py index 2e377950..f3b24c6f 100644 --- a/tests/test_timer_rpc.py +++ b/tests/test_timer_rpc.py @@ -524,6 +524,49 @@ def test_a_pending_get_timer_is_cleared_once_the_robot_has_answered(ctx): assert interceptor.merge_response(DUID, _encode_robot_reply(65, [])) is None +ROBOT_TIMER = ["legacy-1", "on", ["0 9 * * *", ["start_clean", {}]]] + + +def _edit_legacy_timer(job_id="legacy-1"): + return [[job_id, ["30 7 * * 1,2", ["start_clean", {"fan_power": 102}]]]] + + +def test_editing_a_timer_the_robot_listed_goes_to_the_robot(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + interceptor(DUID, _encode_request("get_timer", [], request_id=66)) + interceptor.merge_response(DUID, _encode_robot_reply(66, [ROBOT_TIMER])) + + assert interceptor(DUID, _encode_request("set_timer", _edit_legacy_timer())) is None + assert all(str(j["id"]) != "legacy-1" for j in schedule_store.list_jobs(ctx, DUID)) + + +def test_a_new_timer_is_still_stored_after_the_robot_listed_others(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + interceptor(DUID, _encode_request("get_timer", [], request_id=67)) + interceptor.merge_response(DUID, _encode_robot_reply(67, [ROBOT_TIMER])) + + assert interceptor(DUID, _encode_request("set_timer", _edit_legacy_timer("brand-new"))) is not None + assert any(str(j["id"]) == "brand-new" for j in schedule_store.list_jobs(ctx, DUID)) + + +def test_a_timer_the_store_owns_is_edited_in_the_store_even_if_the_robot_lists_it(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + interceptor(DUID, _encode_request("get_timer", [], request_id=68)) + interceptor.merge_response(DUID, _encode_robot_reply(68, [["3549431", "on", ["0 9 * * *", ["start_clean", {}]]]])) + + assert interceptor(DUID, _encode_request("set_timer", _edit_legacy_timer("3549431"))) is not None + + +def test_the_robots_timer_list_is_forgotten_when_its_next_reply_drops_a_timer(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + interceptor(DUID, _encode_request("get_timer", [], request_id=69)) + interceptor.merge_response(DUID, _encode_robot_reply(69, [ROBOT_TIMER])) + interceptor(DUID, _encode_request("get_timer", [], request_id=70)) + interceptor.merge_response(DUID, _encode_robot_reply(70, [])) + + assert interceptor(DUID, _encode_request("set_timer", _edit_legacy_timer())) is not None + + def test_one_job_with_an_unreadable_cron_does_not_hide_the_other_timers(ctx): schedule_store.upsert_job(ctx, DUID, job_id="bad", cron="0 0 * * ABC", enabled=True) assert timer_rpc.device_cron("0 0 * * ABC") is None From 6bb3020fedc522e61668702fd695ef4e80d92546 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:23:55 -0500 Subject: [PATCH 26/27] Read the app's positional start_clean action in set_timer For robots without order-segment clean the app sends ["start_clean", fan_power, segments(, map_id)] (decompiled timer screen builds mode as fan_power). _parse_set_timer saw no settings object and stored a whole-home clean at the default fan power. Map fan_power and segments; forward the request to the robot when a map id is present or the fan power is not a number, since the store has no per-map timers and the exact map id meaning is unverified. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/timer_rpc.py | 14 +++++++++++++ tests/test_timer_rpc.py | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py index fc21a616..4d5808e1 100644 --- a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -124,11 +124,25 @@ def _parse_set_timer(params: Any) -> _SetTimer: action = detail[1] if len(detail) > 1 else None action_name = str(action[0]) if isinstance(action, list) and action else "" settings = action[1] if isinstance(action, list) and len(action) > 1 and isinstance(action[1], dict) else {} + if action_name == "start_clean" and len(action) > 1 and not isinstance(action[1], dict): + settings = _positional_settings(action) # A timer written without a status starts on, as the official app's own timers do. enabled = True if status is None else str(status).lower() == "on" return _SetTimer(job_id, enabled, str(detail[0]), settings, action_name) +def _positional_settings(action: list[Any]) -> dict[str, Any]: + # Robots without order-segment clean get ["start_clean", fan_power, segments(, map_id)] from the app. + fan_power, rest = action[1], action[2:] + if len(rest) > 1 or isinstance(fan_power, (bool, list, dict)) or fan_power is None: + # A map id (multi-map timers) or an unknown shape: the store cannot place it, so the robot answers. + raise ForwardToRobot("positional start_clean the store cannot place") + settings: dict[str, Any] = {"fan_power": fan_power} + if rest: + settings["segments"] = rest[0] + return settings + + def _id_and_status(params: Any) -> tuple[str, str]: entry = params[0] if isinstance(params, list) and params and isinstance(params[0], list) else params if not isinstance(entry, list) or len(entry) < 2: diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py index f3b24c6f..38910347 100644 --- a/tests/test_timer_rpc.py +++ b/tests/test_timer_rpc.py @@ -567,6 +567,26 @@ def test_the_robots_timer_list_is_forgotten_when_its_next_reply_drops_a_timer(ct assert interceptor(DUID, _encode_request("set_timer", _edit_legacy_timer())) is not None +def test_positional_start_clean_action_carries_fan_power_and_segments(ctx): + params = [["pos-1", ["0 9 * * *", ["start_clean", 104, "16,17"]]]] + assert timer_rpc.handle_timer_request(ctx, DUID, "set_timer", params) == ["ok"] + entry = _job(ctx, "pos-1")["param"]["params"][0] + assert (entry["fan_power"], entry["segments"]) == (104, "16,17") + + +def test_positional_start_clean_without_segments_keeps_the_default_rooms(ctx): + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["pos-2", ["0 9 * * *", ["start_clean", 102]]]]) + entry = _job(ctx, "pos-2")["param"]["params"][0] + assert (entry["fan_power"], entry["segments"]) == (102, "0") + + +@pytest.mark.parametrize("action", [["start_clean", 102, "0", 0], ["start_clean", [1]], ["start_clean", None]]) +def test_positional_start_clean_the_store_cannot_place_goes_to_the_robot(ctx, action): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + assert interceptor(DUID, _encode_request("set_timer", [["pos-3", ["0 9 * * *", action]]])) is None + assert all(str(j["id"]) != "pos-3" for j in schedule_store.list_jobs(ctx, DUID)) + + def test_one_job_with_an_unreadable_cron_does_not_hide_the_other_timers(ctx): schedule_store.upsert_job(ctx, DUID, job_id="bad", cron="0 0 * * ABC", enabled=True) assert timer_rpc.device_cron("0 0 * * ABC") is None From 89b70396ea711840f0ed7d7ea5f2478b8d29fc99 Mon Sep 17 00:00:00 2001 From: Colin Haven <10933733+virtitnerd@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:25:46 -0500 Subject: [PATCH 27/27] Parse SUN-SUN as Sunday only A range ending on SUN was stretched to the end of the week, which turned SUN-SUN into every day. The stretch now applies only when the range starts after Sunday. Co-Authored-By: Claude Sonnet 5.5 --- .../bundled_backend/shared/schedule_runner.py | 4 ++-- tests/test_schedule_runner.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py index 06dfa4ac..ae9c749b 100644 --- a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -98,8 +98,8 @@ def number(token: str) -> int: start, end = number(first), number(last) except ValueError as exc: raise CronError(f"bad {name} range {part!r}") from exc - if names is _WEEKDAY_NAMES and last.strip().upper() == "SUN": - end = high # a range ending on SUN runs to the end of the week (MON-SUN, FRI-SUN) + if names is _WEEKDAY_NAMES and last.strip().upper() == "SUN" and start > 0: + end = high # a range ending on SUN runs to the end of the week (MON-SUN, FRI-SUN); SUN-SUN is Sunday if names is _WEEKDAY_NAMES and start > end and 0 <= end and start <= high: # A day range that wraps past Saturday (6-0, SAT-MON). if start < low or end > high: diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py index 610af641..22b50879 100644 --- a/tests/test_schedule_runner.py +++ b/tests/test_schedule_runner.py @@ -325,6 +325,7 @@ def test_unknown_day_or_month_names_are_rejected(expression): [ ("0 8 * * MON-SUN", {0, 1, 2, 3, 4, 5, 6}), ("0 8 * * FRI-SUN", {5, 6, 0}), + ("0 8 * * SUN-SUN", {0}), ("0 8 * * 6-0", {6, 0}), ("0 8 * * SAT-MON", {6, 0, 1}), ("0 8 * * 1-7", {0, 1, 2, 3, 4, 5, 6}),