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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "python_thingset"
version = "0.5.1"
version = "0.5.2"
description = "A Python library for ThingSet functionality"
authors = [
{ name = "Adam Mitchell", email = "adam.mitchell@brillpower.com" }
Expand Down
13 changes: 12 additions & 1 deletion python_thingset/encoders/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class ThingSetBinaryEncoder(object):
NULL_BYTE = 0xF6
ROOT_ID = 0x00

def __init__(self):
pass
Expand Down Expand Up @@ -62,7 +63,17 @@ def encode_exec(self, value_id: int, args: List[Union[Any, None]]) -> bytes:
+ list(cbor2.dumps(p_args, canonical=True))
)

def encode_update(self, parent_id: int, value_id: int, value: Any) -> bytes:
def encode_update(
self, parent_id: Union[int, None], value_id: int, value: Any
) -> bytes:
"""Update request: ``07 <parent id> {value_id: value}``.

``parent_id`` is the containing group; ``None`` means the object sits
at the root and is addressed via the root node (ID 0). A CBOR null is
never a valid endpoint — the device rejects it with BAD_REQUEST before
looking at the map — so it must not be emitted for a missing parent."""
if parent_id is None:
parent_id = self.ROOT_ID
value = self._coerce_value(value)

return bytes(
Expand Down
13 changes: 13 additions & 0 deletions tests/encoders/binary/test_enc_bin_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,16 @@ def test_update_list_of_strings():
encoded = encoder.encode_update(0x0, 0x4F, ["a", "b"])
expected = bytes([0x07]) + cbor2.dumps(0x0) + cbor2.dumps({0x4F: ["a", "b"]}, canonical=True)
assert encoded == expected


def test_update_no_parent_targets_root():
# A root-level object has no containing group: the request must address
# the root node (ID 0), never a CBOR null endpoint.
encoded = encoder.encode_update(None, 0x4F, 1)
assert encoded == encoder.encode_update(0x0, 0x4F, 1)
assert encoded[1] == 0x00


def test_update_native_bool_false():
encoded = encoder.encode_update(0x0, 0x4F, False)
assert encoded == b"\x07\x00\xa1\x18\x4f\xf4"
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading