Skip to content

Commit d54a7c5

Browse files
committed
fix(backup): unwrap WS send_command envelope in _ws_send
Root cause of every WS-backed auto-backup capture silently skipping: ``ws_client.send_command(...)`` returns the HA standard envelope ``{"success": True, "result": <inner>}``, not the bare ``<inner>``. ``_ws_send`` was returning that envelope, but every fetch handler downstream did ``items = await _ws_send(...)`` followed by ``isinstance(items, list)`` — which is always False for the ``{"success": ..., "result": [...]}`` dict, so the handler returned None, ``maybe_snapshot`` skipped silently, and the e2e log filled with ``Auto-backup: fetch returned None for <domain>:<id>`` lines. Affected every fetch handler that hits a ``<type>/list`` style WS endpoint: label, category, group, zone, area_or_floor, integration, storage-backed helpers (input_boolean / input_text / input_number / input_select / input_datetime / input_button / counter / timer / schedule), and dashboard_resource. Automation / script / scene were unaffected because they go through typed client helpers (``get_automation_config`` etc.) that already unwrap. Calendar and todo fetch handlers use ``execute_script`` which returns a nested dict; they then do ``result.get("response", ...)``. Unwrapping the envelope here is still correct for them — the inner ``result`` is the ``execute_script`` response dict that already has the ``response`` key. No call-site behavior change needed. Restore handlers that use ``_ws_send`` (dashboard, dashboard_resource, label, category, zone, area_or_floor, integration, helpers) treat the return as opaque (it's just embedded in ``ha_manage_backup``'s ``data.result``). Unwrapping affects what shows up there but doesn't break anything — the inner result is more informative than the envelope anyway. Also demote the ``fetch returned None`` log from INFO back to DEBUG now that the root cause is fixed (it was a one-iteration diagnostic).
1 parent 7c56b60 commit d54a7c5

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src/ha_mcp/backup_manager.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,7 @@ async def maybe_snapshot(
278278
if config is None:
279279
# Entity didn't exist at fetch time (create operation, or
280280
# already-deleted at remove time before our pre-fetch).
281-
# Logged at INFO so e2e diagnostics surface the
282-
# "entity-not-yet-indexed" case without polluting prod
283-
# WARNING streams; downgrade to DEBUG later once #1288
284-
# full-loop e2e coverage is stable.
285-
logger.info(
281+
logger.debug(
286282
"Auto-backup: fetch returned None for %s — skipping snapshot",
287283
key,
288284
)
@@ -601,7 +597,7 @@ async def _ws_send(client: Any, message: dict[str, Any]) -> Any:
601597
raise RuntimeError(msg)
602598
try:
603599
cmd_type = message.pop("type")
604-
result = await ws_client.send_command(cmd_type, **message)
600+
envelope = await ws_client.send_command(cmd_type, **message)
605601
finally:
606602
# Best-effort close: narrow to transport/network errors; let
607603
# other exceptions propagate so they show up in logs rather
@@ -614,7 +610,17 @@ async def _ws_send(client: Any, message: dict[str, Any]) -> Any:
614610
type(err).__name__,
615611
err,
616612
)
617-
return result
613+
# ``send_command`` returns ``{"success": True, "result": <inner>}``
614+
# — unwrap so every fetch / restore handler downstream sees the
615+
# inner ``<inner>`` shape directly (list for ``<type>/list`` calls,
616+
# dict for ``execute_script`` calls, etc.). Without this unwrap the
617+
# ``isinstance(items, list)`` guards in every fetch handler return
618+
# None silently, the snapshot is skipped, and the auto-backup loop
619+
# never captures anything for WS-backed domains (#1288 full-loop
620+
# e2e regression — only the REST-backed automation lane worked).
621+
if isinstance(envelope, dict) and "result" in envelope:
622+
return envelope["result"]
623+
return envelope
618624

619625

620626
# Automation / Script / Scene — reuse the typed client helpers, which

0 commit comments

Comments
 (0)