| 1 | """Test pydeCONZ light.""" |
| 2 | |
| 3 | from unittest.mock import Mock |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from pydeconz.models.light.light import ( |
| 8 | LightAlert, |
| 9 | LightColorCapability, |
| 10 | LightColorMode, |
| 11 | LightEffect, |
| 12 | LightFanSpeed, |
| 13 | ) |
| 14 | |
| 15 | DATA = { |
| 16 | "capabilities": { |
| 17 | "alerts": [ |
| 18 | "none", |
| 19 | "select", |
| 20 | "lselect", |
| 21 | "blink", |
| 22 | "breathe", |
| 23 | "okay", |
| 24 | "channelchange", |
| 25 | "finish", |
| 26 | "stop", |
| 27 | ], |
| 28 | "bri": {"min_dim_level": 0.2}, |
| 29 | "color": { |
| 30 | "ct": {"computes_xy": True, "max": 500, "min": 153}, |
| 31 | "effects": [ |
| 32 | "none", |
| 33 | "colorloop", |
| 34 | "candle", |
| 35 | "cosmos", |
| 36 | "enchant", |
| 37 | "fire", |
| 38 | "prism", |
| 39 | "sunbeam", |
| 40 | "sunrise", |
| 41 | "sunset", |
| 42 | "sparkle", |
| 43 | "underwater", |
| 44 | "opal", |
| 45 | "glisten", |
| 46 | ], |
| 47 | "gamut_type": "C", |
| 48 | "modes": ["ct", "effect", "hs", "xy"], |
| 49 | "xy": { |
| 50 | "blue": [0.1532, 0.0475], |
| 51 | "green": [0.17, 0.7], |
| 52 | "red": [0.6915, 0.3083], |
| 53 | }, |
| 54 | }, |
| 55 | }, |
| 56 | "colorcapabilities": 15, |
| 57 | "ctmax": 500, |
| 58 | "ctmin": 153, |
| 59 | "etag": "026bcfe544ad76c7534e5ca8ed39047c", |
| 60 | "hascolor": True, |
| 61 | "manufacturername": "dresden elektronik", |
| 62 | "modelid": "FLS-PP3", |
| 63 | "name": "Light 1", |
| 64 | "pointsymbol": {}, |
| 65 | "state": { |
| 66 | "alert": None, |
| 67 | "bri": 111, |
| 68 | "colormode": "ct", |
| 69 | "ct": 307, |
| 70 | "effect": None, |
| 71 | "gradient": { |
| 72 | "color_adjustment": 0, |
| 73 | "offset": 0, |
| 74 | "offset_adjustment": 0, |
| 75 | "points": [ |
| 76 | [0.2728, 0.6226], |
| 77 | [0.163, 0.4262], |
| 78 | [0.1563, 0.1699], |
| 79 | [0.1551, 0.1147], |
| 80 | [0.1534, 0.0579], |
| 81 | ], |
| 82 | "segments": 5, |
| 83 | "style": "linear", |
| 84 | }, |
| 85 | "hascolor": True, |
| 86 | "hue": 7998, |
| 87 | "on": False, |
| 88 | "reachable": True, |
| 89 | "sat": 172, |
| 90 | "speed": 3, |
| 91 | "xy": [0.421253, 0.39921], |
| 92 | }, |
| 93 | "swversion": "020C.201000A0", |
| 94 | "type": "Extended color light", |
| 95 | "uniqueid": "xx:xx:xx:xx:xx:xx:xx:xx-0A", |
| 96 | } |
| 97 | |
| 98 | |
| 99 | async def test_handler_light(mock_aioresponse, deconz_session, deconz_called_with): |
| 100 | """Verify that creating a light works.""" |
| 101 | lights = deconz_session.lights.lights |
| 102 | |
| 103 | mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 104 | await lights.set_state( |
| 105 | id="0", |
| 106 | alert=LightAlert.SHORT, |
| 107 | brightness=200, |
| 108 | color_loop_speed=10, |
| 109 | color_temperature=400, |
| 110 | effect=LightEffect.COLOR_LOOP, |
| 111 | fan_speed=LightFanSpeed.OFF, |
| 112 | hue=1000, |
| 113 | on=True, |
| 114 | on_time=100, |
| 115 | saturation=150, |
| 116 | transition_time=250, |
| 117 | xy=(0.1, 0.1), |
| 118 | ) |
| 119 | assert deconz_called_with( |
| 120 | "put", |
| 121 | path="/lights/0/state", |
| 122 | json={ |
| 123 | "alert": "select", |
| 124 | "bri": 200, |
| 125 | "colorloopspeed": 10, |
| 126 | "ct": 400, |
| 127 | "effect": "colorloop", |
| 128 | "hue": 1000, |
| 129 | "on": True, |
| 130 | "ontime": 100, |
| 131 | "speed": 0, |
| 132 | "sat": 150, |
| 133 | "transitiontime": 250, |
| 134 | "xy": (0.1, 0.1), |
| 135 | }, |
| 136 | ) |
| 137 | |
| 138 | mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 139 | await lights.set_state("0", on=False) |
| 140 | assert deconz_called_with( |
| 141 | "put", |
| 142 | path="/lights/0/state", |
| 143 | json={"on": False}, |
| 144 | ) |
| 145 | |
| 146 | |
| 147 | async def test_handler_fan(mock_aioresponse, deconz_session, deconz_called_with): |
| 148 | """Verify light fixture with fan work.""" |
| 149 | lights = deconz_session.lights.lights |
| 150 | |
| 151 | mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 152 | await lights.set_state("0", fan_speed=LightFanSpeed.OFF) |
| 153 | assert deconz_called_with("put", path="/lights/0/state", json={"speed": 0}) |
| 154 | |
| 155 | mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 156 | await lights.set_state("0", fan_speed=LightFanSpeed.PERCENT_25) |
| 157 | assert deconz_called_with("put", path="/lights/0/state", json={"speed": 1}) |
| 158 | |
| 159 | mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 160 | await lights.set_state("0", fan_speed=LightFanSpeed.PERCENT_50) |
| 161 | assert deconz_called_with("put", path="/lights/0/state", json={"speed": 2}) |
| 162 | |
| 163 | mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 164 | await lights.set_state("0", fan_speed=LightFanSpeed.PERCENT_75) |
| 165 | assert deconz_called_with("put", path="/lights/0/state", json={"speed": 3}) |
| 166 | |
| 167 | mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 168 | await lights.set_state("0", fan_speed=LightFanSpeed.PERCENT_100) |
| 169 | assert deconz_called_with("put", path="/lights/0/state", json={"speed": 4}) |
| 170 | |
| 171 | mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 172 | await lights.set_state("0", fan_speed=LightFanSpeed.AUTO) |
| 173 | assert deconz_called_with("put", path="/lights/0/state", json={"speed": 5}) |
| 174 | |
| 175 | mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 176 | await lights.set_state("0", fan_speed=LightFanSpeed.COMFORT_BREEZE) |
| 177 | assert deconz_called_with("put", path="/lights/0/state", json={"speed": 6}) |
| 178 | |
| 179 | |
| 180 | async def test_light_light(mock_aioresponse, deconz_light, deconz_called_with): |
| 181 | """Verify that creating a light works.""" |
| 182 | light = await deconz_light(DATA) |
| 183 | |
| 184 | assert light.supported_effects == [ |
| 185 | LightEffect.NONE, |
| 186 | LightEffect.COLOR_LOOP, |
| 187 | LightEffect.CANDLE, |
| 188 | LightEffect.COSMOS, |
| 189 | LightEffect.ENCHANT, |
| 190 | LightEffect.FIRE, |
| 191 | LightEffect.PRISM, |
| 192 | LightEffect.SUNBEAM, |
| 193 | LightEffect.SUNRISE, |
| 194 | LightEffect.SUNSET, |
| 195 | LightEffect.SPARKLE, |
| 196 | LightEffect.UNDERWATER, |
| 197 | LightEffect.OPAL, |
| 198 | LightEffect.GLISTEN, |
| 199 | ] |
| 200 | |
| 201 | assert light.state is False |
| 202 | assert light.on is False |
| 203 | assert light.alert == LightAlert.UNKNOWN |
| 204 | |
| 205 | assert light.brightness == 111 |
| 206 | assert light.hue == 7998 |
| 207 | assert light.saturation == 172 |
| 208 | assert light.color_temp == 307 |
| 209 | assert light.xy == (0.421253, 0.39921) |
| 210 | assert light.gradient == { |
| 211 | "color_adjustment": 0, |
| 212 | "offset": 0, |
| 213 | "offset_adjustment": 0, |
| 214 | "points": [ |
| 215 | [0.2728, 0.6226], |
| 216 | [0.163, 0.4262], |
| 217 | [0.1563, 0.1699], |
| 218 | [0.1551, 0.1147], |
| 219 | [0.1534, 0.0579], |
| 220 | ], |
| 221 | "segments": 5, |
| 222 | "style": "linear", |
| 223 | } |
| 224 | assert light.color_mode == LightColorMode.CT |
| 225 | assert light.max_color_temp == 500 |
| 226 | assert light.min_color_temp == 153 |
| 227 | assert light.effect == LightEffect.UNKNOWN |
| 228 | assert light.fan_speed == LightFanSpeed.PERCENT_75 |
| 229 | assert light.supports_fan_speed is True |
| 230 | assert ( |
| 231 | light.color_capabilities |
| 232 | == LightColorCapability.HUE_SATURATION |
| 233 | | LightColorCapability.ENHANCED_HUE |
| 234 | | LightColorCapability.COLOR_LOOP |
| 235 | | LightColorCapability.XY_ATTRIBUTES |
| 236 | | LightColorCapability.COLOR_TEMPERATURE |
| 237 | ) |
| 238 | assert light.reachable is True |
| 239 | |
| 240 | assert light.deconz_id == "/lights/0" |
| 241 | assert light.etag == "026bcfe544ad76c7534e5ca8ed39047c" |
| 242 | assert light.manufacturer == "dresden elektronik" |
| 243 | assert light.model_id == "FLS-PP3" |
| 244 | assert light.name == "Light 1" |
| 245 | assert light.software_version == "020C.201000A0" |
| 246 | assert light.type == "Extended color light" |
| 247 | assert light.unique_id == "xx:xx:xx:xx:xx:xx:xx:xx-0A" |
| 248 | |
| 249 | light.register_callback(mock_callback := Mock()) |
| 250 | assert light._callbacks |
| 251 | |
| 252 | event = {"state": {"xy": [0.1, 0.1]}} |
| 253 | light.update(event) |
| 254 | |
| 255 | assert light.brightness == 111 |
| 256 | assert light.xy == (0.1, 0.1) |
| 257 | mock_callback.assert_called_once() |
| 258 | assert light.changed_keys == {"state", "xy"} |
| 259 | |
| 260 | light.remove_callback(mock_callback) |
| 261 | assert not light._callbacks |
| 262 | |
| 263 | light.raw["state"]["xy"] = (65555, 65555) |
| 264 | assert light.xy == (1, 1) |
| 265 | |
| 266 | del light.raw["state"]["xy"] |
| 267 | assert light.xy is None |
| 268 | |
| 269 | light.raw["ctmax"] = 1000 |
| 270 | assert light.max_color_temp == 650 |
| 271 | |
| 272 | light.raw["ctmin"] = 0 |
| 273 | assert light.min_color_temp == 140 |
| 274 | |
| 275 | del light.raw["ctmax"] |
| 276 | assert light.max_color_temp is None |
| 277 | |
| 278 | del light.raw["ctmin"] |
| 279 | assert light.min_color_temp is None |
| 280 | |
| 281 | light.update({"state": {"bri": 2}}) |
| 282 | assert light.brightness == 2 |
| 283 | |
| 284 | |
| 285 | ENUM_PROPERTY_DATA = [ |
| 286 | ( |
| 287 | ("state", "alert"), |
| 288 | "alert", |
| 289 | { |
| 290 | "none": LightAlert.NONE, |
| 291 | "lselect": LightAlert.LONG, |
| 292 | "select": LightAlert.SHORT, |
| 293 | None: LightAlert.UNKNOWN, |
| 294 | }, |
| 295 | ), |
| 296 | ( |
| 297 | ("colorcapabilities",), |
| 298 | "color_capabilities", |
| 299 | { |
| 300 | 0: LightColorCapability.HUE_SATURATION, |
| 301 | 9: LightColorCapability.UNKNOWN, |
| 302 | None: LightColorCapability.UNKNOWN, |
| 303 | }, |
| 304 | ), |
| 305 | ( |
| 306 | ("state", "colormode"), |
| 307 | "color_mode", |
| 308 | { |
| 309 | "ct": LightColorMode.CT, |
| 310 | "gradient": LightColorMode.GRADIENT, |
| 311 | "hs": LightColorMode.HS, |
| 312 | "xy": LightColorMode.XY, |
| 313 | None: LightColorMode.UNKNOWN, |
| 314 | }, |
| 315 | ), |
| 316 | ( |
| 317 | ("state", "effect"), |
| 318 | "effect", |
| 319 | { |
| 320 | "colorloop": LightEffect.COLOR_LOOP, |
| 321 | "none": LightEffect.NONE, |
| 322 | None: LightEffect.UNKNOWN, |
| 323 | }, |
| 324 | ), |
| 325 | ( |
| 326 | ("state", "speed"), |
| 327 | "fan_speed", |
| 328 | { |
| 329 | 0: LightFanSpeed.OFF, |
| 330 | 1: LightFanSpeed.PERCENT_25, |
| 331 | 2: LightFanSpeed.PERCENT_50, |
| 332 | 3: LightFanSpeed.PERCENT_75, |
| 333 | 4: LightFanSpeed.PERCENT_100, |
| 334 | 5: LightFanSpeed.AUTO, |
| 335 | 6: LightFanSpeed.COMFORT_BREEZE, |
| 336 | }, |
| 337 | ), |
| 338 | ] |
| 339 | |
| 340 | |
| 341 | @pytest.mark.parametrize(("path", "property", "data"), ENUM_PROPERTY_DATA) |
| 342 | async def test_enum_light_properties(deconz_light, path, property, data): |
| 343 | """Verify enum properties return expected values or None.""" |
| 344 | light = await deconz_light({"config": {}, "state": {}, "type": "Color light"}) |
| 345 | |
| 346 | for input, output in data.items(): |
| 347 | data = {path[0]: input} |
| 348 | if len(path) == 2: |
| 349 | data = {path[0]: {path[1]: input}} |
| 350 | light.update(data) |
| 351 | assert getattr(light, property) == output |
| 352 | |
| 353 | |
| 354 | async def test_enum_light_properties_no_key(deconz_light): |
| 355 | """Verify enum properties return expected values or None.""" |
| 356 | light = await deconz_light({"config": {}, "state": {}, "type": "Color light"}) |
| 357 | |
| 358 | assert light.supported_effects is None |
| 359 | assert light.alert is None |
| 360 | assert light.color_capabilities is None |
| 361 | assert light.color_mode is None |
| 362 | assert light.effect is None |
| 363 | with pytest.raises(KeyError): |
| 364 | assert light.fan_speed |
| 365 | |
| 366 | |
| 367 | ENUM_CAPABILITIES_DATA = [ |
| 368 | ({"capabilities": {}}), |
| 369 | ({"capabilities": {"color": {}}}), |
| 370 | ] |
| 371 | |
| 372 | |
| 373 | @pytest.mark.parametrize(("data"), ENUM_CAPABILITIES_DATA) |
| 374 | async def test_enum_light_properties_no_capabilities(deconz_light, data): |
| 375 | """Verify enum properties return expected values or None.""" |
| 376 | light = await deconz_light( |
| 377 | {"config": {}, "state": {}, "type": "Color light"} | data |
| 378 | ) |
| 379 | |
| 380 | assert light.supported_effects is None |