Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions smartthings_local/ocf/state_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,21 @@ 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
the result into apply_rep on this same cache."""
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

Expand Down
18 changes: 18 additions & 0 deletions tests/test_state_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': {}},
Expand Down