| 1 |
"""Real and crafted scale messages used by the decode tests.""" |
| 2 |
|
| 3 |
from aioacaia.const import HEADER1, HEADER2 |
| 4 |
|
| 5 |
# Command byte for event notifications (weight/timer/button/heartbeat). |
| 6 |
_EVENT_CMD = 0x0C |
| 7 |
|
| 8 |
|
| 9 |
def _frame(command: int, payload: bytes) -> bytes: |
| 10 |
"""Wrap a command payload with its length and split checksum.""" |
| 11 |
body = bytes([len(payload) + 1]) + payload |
| 12 |
checksum = bytes((sum(body[0::2]) & 0xFF, sum(body[1::2]) & 0xFF)) |
| 13 |
return bytes([HEADER1, HEADER2, command]) + body + checksum |
| 14 |
|
| 15 |
|
| 16 |
def frame(msg_type: int, payload: bytes) -> bytes: |
| 17 |
"""Wrap a payload in an event-notification frame (command 12).""" |
| 18 |
return _frame(_EVENT_CMD, bytes([msg_type]) + payload) |
| 19 |
|
| 20 |
|
| 21 |
# Reusable sub-payloads with known decoded values. |
| 22 |
_WEIGHT = bytes([0xDF, 0x06, 0x00, 0x00, 0x01, 0x00]) # -> 175.9 g (unit 1, positive) |
| 23 |
_TIME = bytes([0x01, 0x1E, 0x05]) # -> 90.5 s (1 min, 30 s, .5) |
| 24 |
|
| 25 |
# --- Weight (msg_type 5) --- |
| 26 |
# Real capture from a scale; decodes to 175.9 g. |
| 27 |
WEIGHT = bytes.fromhex("efdd0c0c05df060000010007000002f30d") |
| 28 |
WEIGHT_NEGATIVE = frame(5, bytes([0xDF, 0x06, 0x00, 0x00, 0x01, 0x02])) # -> -175.9 |
| 29 |
WEIGHT_UNIT_100 = frame(5, bytes([0xDF, 0x06, 0x00, 0x00, 0x02, 0x00])) # -> 17.59 |
| 30 |
WEIGHT_UNIT_1000 = frame(5, bytes([0xDF, 0x06, 0x00, 0x00, 0x03, 0x00])) # -> 1.759 |
| 31 |
WEIGHT_UNIT_10000 = frame(5, bytes([0xDF, 0x06, 0x00, 0x00, 0x04, 0x00])) # -> 0.1759 |
| 32 |
WEIGHT_BAD_UNIT = frame( |
| 33 |
5, bytes([0xDF, 0x06, 0x00, 0x00, 0x00, 0x00]) |
| 34 |
) # unit 0 -> ValueError |
| 35 |
|
| 36 |
# --- Timer (msg_type 7) --- |
| 37 |
TIMER = frame(7, _TIME) # -> 90.5 |
| 38 |
|
| 39 |
# --- Heartbeat (msg_type 11) wrapping a weight or timer payload --- |
| 40 |
HEARTBEAT_WEIGHT = frame(11, bytes([0x00, 0x00, 0x05]) + _WEIGHT) # value 175.9 |
| 41 |
HEARTBEAT_TIME = frame(11, bytes([0x00, 0x00, 0x07]) + _TIME) # time 90.5 |
| 42 |
# First wrapped record is a weight, followed by a timer record. The wrapper is |
| 43 |
# the first record, so this must decode to the weight, not the trailing timer. |
| 44 |
HEARTBEAT_WEIGHT_THEN_TIME = frame( |
| 45 |
11, bytes([0x00, 0x00, 0x05]) + _WEIGHT + bytes([0x07]) + _TIME |
| 46 |
) |
| 47 |
|
| 48 |
# --- Button (msg_type 8) --- |
| 49 |
# A press is a key code followed by the ordinary record chain, so the byte |
| 50 |
# after the 3 time bytes is not padding — it is the tag introducing the weight |
| 51 |
# record that follows. |
| 52 |
_TIME_FIELD = _TIME + bytes([0x05]) |
| 53 |
BUTTON_TARE = frame(8, bytes([0, 5]) + _WEIGHT) |
| 54 |
BUTTON_START_WEIGHT = frame(8, bytes([8, 5]) + _WEIGHT) |
| 55 |
BUTTON_START = frame(8, bytes([8])) |
| 56 |
BUTTON_STOP_TIME_WEIGHT = frame(8, bytes([10, 7]) + _TIME_FIELD + _WEIGHT) |
| 57 |
BUTTON_STOP_TIME = frame(8, bytes([10, 7]) + _TIME) |
| 58 |
BUTTON_STOP = frame(8, bytes([10, 13])) |
| 59 |
BUTTON_RESET_TIME_WEIGHT = frame(8, bytes([9, 7]) + _TIME_FIELD + _WEIGHT) |
| 60 |
BUTTON_RESET_TIME = frame(8, bytes([9, 7]) + _TIME) |
| 61 |
BUTTON_RESET = frame(8, bytes([9, 12])) |
| 62 |
BUTTON_STOP_WEIGHT = frame(8, bytes([10, 5]) + _WEIGHT) |
| 63 |
BUTTON_RESET_WEIGHT = frame(8, bytes([9, 5]) + _WEIGHT) |
| 64 |
BUTTON_UNKNOWN = frame(8, bytes([7, 7])) |
| 65 |
|
| 66 |
# Verbatim captures from a PEARL-244684 (original Pearl). Elapsed times were |
| 67 |
# cross-checked against wall clock, so these are ground truth rather than |
| 68 |
# frames built to match the parser. |
| 69 |
REAL_STOP_WEIGHT_TIME = frame( # 55.1 g, 9.7 s |
| 70 |
8, bytes.fromhex("0a05270200000101 07000907".replace(" ", "")) |
| 71 |
) |
| 72 |
REAL_START_BEHIND_TAG_0B = frame( # 54.9 g, behind a 0b record |
| 73 |
8, bytes.fromhex("080b00e005250200000101") |
| 74 |
) |
| 75 |
REAL_RESET_BEHIND_TAG_0B = frame( # 0 g, behind a 0b record |
| 76 |
8, bytes.fromhex("090b00e005000000000101") |
| 77 |
) |
| 78 |
REAL_HEARTBEAT_WRAPPED_START = frame( # a start press nested in a heartbeat |
| 79 |
11, bytes.fromhex("00e00808050000000001 01".replace(" ", "")) |
| 80 |
) |
| 81 |
|
| 82 |
# Verbatim captures from a 2021+ new-style scale. On this firmware a start or |
| 83 |
# reset press is a bare key code with no trailing records — notably no tag 0x0b — |
| 84 |
# while a stop still carries a weight then a time record, the same layout as the |
| 85 |
# original Pearl. The stop time was cross-checked against the wall clock |
| 86 |
# (10.8 s decoded vs 10.6 s measured). |
| 87 |
REAL_2021_START = frame(8, bytes.fromhex("08")) # start, timer running |
| 88 |
REAL_2021_STOP_WEIGHT_TIME = frame( # stop, 0 g, 10.8 s |
| 89 |
8, bytes.fromhex("0a0500000000010107000a08") |
| 90 |
) |
| 91 |
REAL_2021_RESET = frame(8, bytes.fromhex("09")) # reset |
| 92 |
|
| 93 |
# --- Unknown msg_type --- |
| 94 |
UNKNOWN_TYPE = frame(99, bytes([0, 0])) |
| 95 |
|
| 96 |
# --- Settings (command 8) --- |
| 97 |
# Real capture; battery 93, grams, auto_off 5 min, beep on. |
| 98 |
SETTINGS_GRAMS = bytes.fromhex("efdd08095d020201000101000d60") |
| 99 |
# Same message with the units byte flipped to ounces (0x05). |
| 100 |
SETTINGS_OUNCES = _frame(0x08, bytes.fromhex("5d05020100010100")) |
| 101 |
|
| 102 |
# --- decode() framing edge cases --- |
| 103 |
HEADER_ONLY = bytes.fromhex("efdd0c") # too short; also split part 1 |
| 104 |
SPLIT_REMAINDER = bytes.fromhex("0c05df060000010007000002f30d") # split part 2 |
| 105 |
TOO_LONG = bytes.fromhex("efdd0c200500") # length byte claims more bytes than present |
| 106 |
UNKNOWN_COMMAND = _frame(0x05, b"\x00") |
| 107 |
LEADING_GARBAGE = bytes([0x00, 0x01, 0x02]) + WEIGHT |
| 108 |
TWO_MESSAGES = WEIGHT + SETTINGS_GRAMS |
| 109 |
BAD_CHECKSUM = WEIGHT[:-1] + bytes([WEIGHT[-1] ^ 0x01]) |
| 110 |
SHORT_WEIGHT = frame(5, b"\x00") |
| 111 |
WEIGHT_WITH_EMBEDDED_HEADER = frame( |
| 112 |
5, bytes([HEADER1, HEADER2, 0x00, 0x00, 0x01, 0x00]) |
| 113 |
) |