From d0305071bff22837354202acd53c3b69fa629319 Mon Sep 17 00:00:00 2001 From: Jason Morcos Date: Mon, 31 Aug 2026 13:33:52 -0700 Subject: [PATCH] Preserve first device-tree resource --- smartthings_local/ocf/state_cache.py | 12 ++++++++---- tests/test_state_cache.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/smartthings_local/ocf/state_cache.py b/smartthings_local/ocf/state_cache.py index 7aa19fa..f56be81 100644 --- a/smartthings_local/ocf/state_cache.py +++ b/smartthings_local/ocf/state_cache.py @@ -62,8 +62,8 @@ def apply_optimistic(self, href: str, body: dict) -> bool: @staticmethod def index_device_tree(device0_body) -> dict[str, dict]: """Turn a /device/0 CBOR list-of-{href, rep} sweep response into - a dict keyed by href. Entry [0] is the device-level rep itself - and isn't useful here, so it's skipped. + a dict keyed by href. Some responses put a device-level `/device/0` + rep first, while others put a normal resource in that slot. Replaces the old standalone sensors.index_links — folded in here because every current and future caller immediately feeds @@ -71,8 +71,12 @@ def index_device_tree(device0_body) -> dict[str, dict]: out: dict[str, dict] = {} if not isinstance(device0_body, list): return out - for entry in device0_body[1:]: - if isinstance(entry, dict) and 'href' in entry: + for entry in device0_body: + if ( + isinstance(entry, dict) + and 'href' in entry + and entry['href'] != '/device/0' + ): out[entry['href']] = entry.get('rep') or {} return out diff --git a/tests/test_state_cache.py b/tests/test_state_cache.py index 82c9807..6947a77 100644 --- a/tests/test_state_cache.py +++ b/tests/test_state_cache.py @@ -17,6 +17,24 @@ def test_index_device_tree_skips_device_level_entry_at_index_zero(): assert '/device/0' not in indexed +def test_index_device_tree_keeps_resource_entry_at_index_zero(): + tree = [ + { + 'href': '/information/vs/0', + 'rep': {'x.com.samsung.da.modelNum': 'synthetic-model'}, + }, + {'rt': ['x.com.samsung.devcol']}, + {'href': '/configuration/vs/0', 'rep': {'configured': True}}, + ] + + assert StateCache.index_device_tree(tree) == { + '/information/vs/0': { + 'x.com.samsung.da.modelNum': 'synthetic-model', + }, + '/configuration/vs/0': {'configured': True}, + } + + def test_index_device_tree_stub_entry_becomes_empty_dict(): tree = [ {'href': '/device/0', 'rep': {}},