src.bluetooth_sig.utils.values

Value utilities for consumers of parsed Bluetooth SIG data.

Provides helpers that avoid leaking implementation details (msgspec, enum ordering) into every consumer codebase.

Functions

Name

Description

is_struct_value(→ bool)

Check whether obj is a parsed struct produced by the library.

to_primitive(→ int | float | str | bool)

Coerce a parsed characteristic value to a plain Python primitive.

Module Contents

src.bluetooth_sig.utils.values.is_struct_value(obj: object) bool

Check whether obj is a parsed struct produced by the library.

Use this instead of hasattr(obj, '__struct_fields__') so consumer code does not depend on the msgspec implementation detail.

Parameters:

obj – Any parsed characteristic value.

Returns:

True if obj is a msgspec.Struct instance.

src.bluetooth_sig.utils.values.to_primitive(value: Any) int | float | str | bool

Coerce a parsed characteristic value to a plain Python primitive.

Handles the full range of types the library may return (bool, IntFlag, IntEnum, Enum, int, float, str, datetime, timedelta, msgspec Structs, …).

Order matters:

  • bool before intbool is a subclass of int.

  • IntFlag before the .name branch — bit-field values expose a .name attribute but should be stored as a plain int.

  • IntEnum / Enum.name string.

Parameters:

value – Any value returned by BaseCharacteristic.parse_value() or extracted from a struct field.

Returns:

A plain int, float, str, or bool.