| 1 |
import pytest |
| 2 |
|
| 3 |
from govee_local_api.message import ( |
| 4 |
ScanMessage, |
| 5 |
ColorMessage, |
| 6 |
BrightnessMessage, |
| 7 |
OnOffMessage, |
| 8 |
MessageResponseFactory, |
| 9 |
ScanResponse, |
| 10 |
DevStatusResponse, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
def test_scan_message() -> None: |
| 15 |
msg: ScanMessage = ScanMessage() |
| 16 |
assert msg.as_dict() == { |
| 17 |
"msg": {"cmd": "scan", "data": {"account_topic": "reserve"}} |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
def test_color_message_ok(): |
| 22 |
msg: ColorMessage = ColorMessage(rgb=(64, 128, 255), temperature=None) |
| 23 |
assert msg.as_dict() == { |
| 24 |
"msg": { |
| 25 |
"cmd": "colorwc", |
| 26 |
"data": {"color": {"r": 64, "g": 128, "b": 255}, "colorTemInKelvin": 0}, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
msg = ColorMessage(rgb=None, temperature=5000) |
| 31 |
assert msg.as_dict() == { |
| 32 |
"msg": { |
| 33 |
"cmd": "colorwc", |
| 34 |
"data": {"color": {"r": 0, "g": 0, "b": 0}, "colorTemInKelvin": 5000}, |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
msg: ColorMessage = ColorMessage(rgb=(64, 128, 255), temperature=5000) |
| 39 |
assert msg.as_dict() == { |
| 40 |
"msg": { |
| 41 |
"cmd": "colorwc", |
| 42 |
"data": {"color": {"r": 64, "g": 128, "b": 255}, "colorTemInKelvin": 0}, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
def test_color_clipping(): |
| 48 |
msg: ColorMessage = ColorMessage(rgb=(-500, 42, 500), temperature=None) |
| 49 |
assert msg.as_dict() == { |
| 50 |
"msg": { |
| 51 |
"cmd": "colorwc", |
| 52 |
"data": {"color": {"r": 0, "g": 42, "b": 255}, "colorTemInKelvin": 0}, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
msg = ColorMessage(rgb=None, temperature=1) |
| 57 |
assert msg.as_dict() == { |
| 58 |
"msg": { |
| 59 |
"cmd": "colorwc", |
| 60 |
"data": {"color": {"r": 0, "g": 0, "b": 0}, "colorTemInKelvin": 2000}, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
msg = ColorMessage(rgb=None, temperature=9999) |
| 65 |
assert msg.as_dict() == { |
| 66 |
"msg": { |
| 67 |
"cmd": "colorwc", |
| 68 |
"data": {"color": {"r": 0, "g": 0, "b": 0}, "colorTemInKelvin": 9000}, |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
def test_brightness(): |
| 74 |
msg: BrightnessMessage = BrightnessMessage(42) |
| 75 |
assert msg.as_dict() == { |
| 76 |
"msg": { |
| 77 |
"cmd": "brightness", |
| 78 |
"data": {"value": 42}, |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
def test_brightness_clipping(): |
| 84 |
msg: BrightnessMessage = BrightnessMessage(-5) |
| 85 |
assert msg.as_dict() == { |
| 86 |
"msg": { |
| 87 |
"cmd": "brightness", |
| 88 |
"data": {"value": 0}, |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
msg: BrightnessMessage = BrightnessMessage(101) |
| 93 |
assert msg.as_dict() == { |
| 94 |
"msg": { |
| 95 |
"cmd": "brightness", |
| 96 |
"data": {"value": 100}, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
def test_on_off(): |
| 102 |
msg: OnOffMessage = OnOffMessage(True) |
| 103 |
assert msg.as_dict() == {"msg": {"cmd": "turn", "data": {"value": 1}}} |
| 104 |
|
| 105 |
msg: OnOffMessage = OnOffMessage(False) |
| 106 |
assert msg.as_dict() == {"msg": {"cmd": "turn", "data": {"value": 0}}} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
def test_factory_returns_none_for_invalid_json() -> None: |
| 113 |
factory = MessageResponseFactory() |
| 114 |
assert factory.create_message(b"not-json") is None |
| 115 |
assert factory.create_message(b"") is None |
| 116 |
assert factory.create_message(b"\x00\x01\x02") is None |
| 117 |
|
| 118 |
|
| 119 |
def test_factory_returns_none_for_missing_msg_wrapper() -> None: |
| 120 |
factory = MessageResponseFactory() |
| 121 |
assert factory.create_message(b'{"foo": "bar"}') is None |
| 122 |
assert factory.create_message(b"[]") is None |
| 123 |
assert factory.create_message(b'"string"') is None |
| 124 |
|
| 125 |
|
| 126 |
def test_factory_returns_none_for_missing_data_field() -> None: |
| 127 |
"""Previously this raised KeyError because the guard only triggered when |
| 128 |
BOTH cmd and data were missing.""" |
| 129 |
factory = MessageResponseFactory() |
| 130 |
assert factory.create_message(b'{"msg": {"cmd": "scan"}}') is None |
| 131 |
|
| 132 |
|
| 133 |
def test_factory_returns_none_for_missing_cmd_field() -> None: |
| 134 |
factory = MessageResponseFactory() |
| 135 |
assert factory.create_message(b'{"msg": {"data": {}}}') is None |
| 136 |
|
| 137 |
|
| 138 |
def test_factory_returns_none_for_unknown_command() -> None: |
| 139 |
"""Previously this raised StopIteration.""" |
| 140 |
factory = MessageResponseFactory() |
| 141 |
assert factory.create_message(b'{"msg": {"cmd": "future-cmd", "data": {}}}') is None |
| 142 |
|
| 143 |
|
| 144 |
def test_factory_returns_scan_response_for_valid_scan() -> None: |
| 145 |
factory = MessageResponseFactory() |
| 146 |
payload = ( |
| 147 |
b'{"msg": {"cmd": "scan", "data": ' |
| 148 |
b'{"device": "ab:cd", "sku": "H6008", "ip": "192.168.1.42"}}}' |
| 149 |
) |
| 150 |
message = factory.create_message(payload) |
| 151 |
assert isinstance(message, ScanResponse) |
| 152 |
assert message.device == "ab:cd" |
| 153 |
assert message.sku == "H6008" |
| 154 |
assert message.ip == "192.168.1.42" |
| 155 |
|
| 156 |
|
| 157 |
def test_factory_returns_dev_status_response_for_valid_dev_status() -> None: |
| 158 |
factory = MessageResponseFactory() |
| 159 |
payload = ( |
| 160 |
b'{"msg": {"cmd": "devStatus", "data": ' |
| 161 |
b'{"onOff": 1, "brightness": 50, "color": {"r": 1, "g": 2, "b": 3},' |
| 162 |
b' "colorTemInKelvin": 4000}}}' |
| 163 |
) |
| 164 |
message = factory.create_message(payload) |
| 165 |
assert isinstance(message, DevStatusResponse) |
| 166 |
assert message.is_on is True |
| 167 |
assert message.brightness == 50 |
| 168 |
assert message.color == (1, 2, 3) |
| 169 |
assert message.color_temperature == 4000 |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
def test_color_message_raises_on_no_input() -> None: |
| 176 |
"""rgb=None + temperature=None|0 used to raise UnboundLocalError.""" |
| 177 |
with pytest.raises(ValueError): |
| 178 |
ColorMessage(rgb=None, temperature=None) |
| 179 |
with pytest.raises(ValueError): |
| 180 |
ColorMessage(rgb=None, temperature=0) |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
def test_dev_status_response_handles_missing_onoff() -> None: |
| 187 |
"""A partial payload must not raise; defaults are safe.""" |
| 188 |
msg = DevStatusResponse( |
| 189 |
{"brightness": 50, "color": {"r": 1, "g": 2, "b": 3}, "colorTemInKelvin": 4000} |
| 190 |
) |
| 191 |
assert msg.is_on is False |
| 192 |
assert msg.brightness == 50 |
| 193 |
assert msg.color == (1, 2, 3) |
| 194 |
assert msg.color_temperature == 4000 |
| 195 |
|
| 196 |
|
| 197 |
def test_dev_status_response_handles_missing_color() -> None: |
| 198 |
msg = DevStatusResponse({"onOff": 1, "brightness": 50, "colorTemInKelvin": 4000}) |
| 199 |
assert msg.is_on is True |
| 200 |
assert msg.color == (0, 0, 0) |
| 201 |
assert msg.color_temperature == 4000 |
| 202 |
|
| 203 |
|
| 204 |
def test_dev_status_response_handles_partial_color_dict() -> None: |
| 205 |
"""Only some of r/g/b are present.""" |
| 206 |
msg = DevStatusResponse( |
| 207 |
{"onOff": 1, "color": {"r": 200}, "brightness": 50, "colorTemInKelvin": 4000} |
| 208 |
) |
| 209 |
assert msg.color == (200, 0, 0) |
| 210 |
|
| 211 |
|
| 212 |
def test_dev_status_response_handles_null_color() -> None: |
| 213 |
"""color = None in the payload — previously raised TypeError.""" |
| 214 |
msg = DevStatusResponse( |
| 215 |
{"onOff": 1, "color": None, "brightness": 50, "colorTemInKelvin": 4000} |
| 216 |
) |
| 217 |
assert msg.color == (0, 0, 0) |
| 218 |
|
| 219 |
|
| 220 |
def test_dev_status_response_handles_empty_payload() -> None: |
| 221 |
"""No fields at all. Every accessor must return its safe default.""" |
| 222 |
msg = DevStatusResponse({}) |
| 223 |
assert msg.is_on is False |
| 224 |
assert msg.color == (0, 0, 0) |
| 225 |
assert msg.brightness == 0 |
| 226 |
assert msg.color_temperature == 0 |