Files
ledgrab/server/tests/test_ble_protocols.py
T
alexei.dolgolyov 2b5dac2c42
Build Android APK / build-android (push) Failing after 1m44s
Lint & Test / test (push) Successful in 4m22s
feat(devices): BLE LED controller support (SP110E/Triones/Zengge/Govee)
End-to-end BLE streaming: provider + client + per-protocol wire encoders
with whole-strip averaging, desktop (bleak) and Android (Kotlin BleBridge
via Chaquopy) transports, discovery with protocol-family detection that
auto-fills the UI, throttled not-connected warning + 10 s reconnect
cooldown so a dropped link no longer stalls the pipeline at ~30 s/frame,
and an explicit asyncio.wait_for wrapper around bleak connect() since
the WinRT backend doesn't always honor the timeout kwarg.

Also rewrites server/restart.ps1 to be parameterized (-Port / -Module /
-PythonVersion / timeouts / -Quiet), pick the right interpreter via the
py launcher, pre-flight the target module, poll port readiness on both
shutdown and startup, redirect child stdout/stderr so Start-Process
doesn't hang on inherited Git-Bash handles, and return proper exit codes.

Rolls in concurrent work: Android BLE permissions + launcher icons + ru/zh
resources, Chaquopy-safe value_stream psutil fallback, setup-required
modal, asset-store test coverage, and misc system/config touch-ups.
2026-04-21 14:58:35 +03:00

131 lines
4.4 KiB
Python

"""Unit tests for BLE LED controller wire protocols.
These tests exercise the pure byte-encoding functions for each family,
so they run without ``bleak`` installed and without any real BLE hardware.
"""
import pytest
from ledgrab.core.devices.ble_protocols import (
all_protocols,
get_protocol,
identify_family,
govee,
sp110e,
triones,
zengge,
)
class TestSP110E:
def test_color_frame_is_five_bytes_with_cmd_tail(self):
frame = sp110e.encode_color(255, 128, 64)
assert frame == bytes((255, 128, 64, 0x00, 0x1E))
def test_brightness_scales_rgb(self):
frame = sp110e.encode_color(200, 200, 200, brightness=128)
# 200 * 128 // 255 == 100
assert frame == bytes((100, 100, 100, 0x00, 0x1E))
def test_brightness_255_is_passthrough(self):
assert sp110e.encode_color(1, 2, 3, 255) == bytes((1, 2, 3, 0x00, 0x1E))
def test_clamps_out_of_range(self):
assert sp110e.encode_color(-5, 300, 128) == bytes((0, 255, 128, 0x00, 0x1E))
def test_power_frames(self):
assert sp110e.encode_power(True) == bytes((0, 0, 0, 0, 0xAA))
assert sp110e.encode_power(False) == bytes((0, 0, 0, 0, 0xAB))
class TestTriones:
def test_color_frame_matches_documented_template(self):
# 7E 07 05 03 RR GG BB 10 EF
frame = triones.encode_color(0xAA, 0xBB, 0xCC)
assert frame == bytes((0x7E, 0x07, 0x05, 0x03, 0xAA, 0xBB, 0xCC, 0x10, 0xEF))
def test_frame_is_nine_bytes(self):
assert len(triones.encode_color(10, 20, 30)) == 9
def test_brightness_scales(self):
frame = triones.encode_color(255, 255, 255, brightness=51) # 20%
# 255 * 51 // 255 == 51
assert frame[4:7] == bytes((51, 51, 51))
def test_power_on_off_distinct(self):
on = triones.encode_power(True)
off = triones.encode_power(False)
assert on != off
assert on[0] == 0x7E and off[0] == 0x7E
assert on[-1] == 0xEF and off[-1] == 0xEF
class TestZengge:
def test_color_frame_format(self):
# 56 RR GG BB 00 F0 AA
frame = zengge.encode_color(0x11, 0x22, 0x33)
assert frame == bytes((0x56, 0x11, 0x22, 0x33, 0x00, 0xF0, 0xAA))
def test_frame_is_seven_bytes(self):
assert len(zengge.encode_color(1, 2, 3)) == 7
def test_power_frames(self):
assert zengge.encode_power(True) == bytes((0xCC, 0x23, 0x33))
assert zengge.encode_power(False) == bytes((0xCC, 0x24, 0x33))
class TestGovee:
def test_color_frame_is_twenty_bytes_with_checksum(self):
frame = govee.encode_color(255, 0, 0)
assert len(frame) == 20
# Verify XOR checksum
expected_checksum = 0
for i in range(19):
expected_checksum ^= frame[i]
assert frame[19] == expected_checksum
def test_color_frame_header(self):
# 33 05 02 RR GG BB ...
frame = govee.encode_color(0x10, 0x20, 0x30)
assert frame[0] == 0x33
assert frame[1] == 0x05
assert frame[2] == 0x02
assert frame[3:6] == bytes((0x10, 0x20, 0x30))
def test_brightness_scales(self):
frame = govee.encode_color(100, 100, 100, brightness=0)
assert frame[3:6] == bytes((0, 0, 0))
def test_power_frames_have_valid_checksum(self):
for state in (True, False):
frame = govee.encode_power(state)
assert len(frame) == 20
checksum = 0
for i in range(19):
checksum ^= frame[i]
assert frame[19] == checksum
class TestRegistry:
def test_all_four_families_registered(self):
families = set(all_protocols().keys())
assert families == {"sp110e", "triones", "zengge", "govee"}
def test_get_protocol_raises_on_unknown(self):
with pytest.raises(ValueError, match="Unknown BLE family"):
get_protocol("not-a-real-family")
def test_get_protocol_returns_correct_family(self):
assert get_protocol("sp110e").family == "sp110e"
assert get_protocol("triones").family == "triones"
def test_identify_family_by_name(self):
assert identify_family("SP110E_A1B2C3") == "sp110e"
assert identify_family("Triones-ABCDEF") == "triones"
assert identify_family("Zengge-123456") == "zengge"
assert identify_family("ihoment_H6008_xxxx") == "govee"
def test_identify_family_returns_none_for_unknown(self):
assert identify_family("SomeRandomDevice") is None
assert identify_family("") is None