| 1 | from unittest.mock import Mock |
| 2 | from blebox_uniapi.box_types import BOX_TYPE_CONF |
| 3 | import pytest |
| 4 | |
| 5 | from blebox_uniapi.light import Light |
| 6 | from blebox_uniapi.box import Box |
| 7 | |
| 8 | |
| 9 | @pytest.fixture |
| 10 | def product(): |
| 11 | return Mock(spec=Box) |
| 12 | |
| 13 | |
| 14 | @pytest.fixture |
| 15 | def dimmer_box(product): |
| 16 | product.type = "dimmerBox" |
| 17 | many = Light.many_from_config( |
| 18 | product, BOX_TYPE_CONF["dimmerBox"][20170829]["lights"], extended_state={} |
| 19 | ) |
| 20 | assert len(many) == 1 |
| 21 | return many[0] |
| 22 | |
| 23 | |
| 24 | @pytest.fixture |
| 25 | def w_light_box_rgbww(product): |
| 26 | product.type = "wLightBox" |
| 27 | extended_state = { |
| 28 | "rgbw": { |
| 29 | "colorMode": 7, |
| 30 | "effectID": 0, |
| 31 | "desiredColor": "fcfffcff00", |
| 32 | "currentColor": "fcfffcff00", |
| 33 | "lastOnColor": "fcfffcff00", |
| 34 | "durationsMs": {"colorFade": 1000, "effectFade": 1000, "effectStep": 1000}, |
| 35 | "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"}, |
| 36 | "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"}, |
| 37 | } |
| 38 | } |
| 39 | many = Light.many_from_config( |
| 40 | product, |
| 41 | BOX_TYPE_CONF["wLightBox"][20200229]["lights"], |
| 42 | extended_state=extended_state, |
| 43 | ) |
| 44 | return many[0] |
| 45 | |
| 46 | |
| 47 | @pytest.mark.parametrize("input_on", range(1, 255)) |
| 48 | async def test_dimmer_box_async_on_with_int(dimmer_box: Light, product: Mock, input_on): |
| 49 | await dimmer_box.async_on(input_on) |
| 50 | product.async_api_command.assert_called_with("set", input_on) |
| 51 | |
| 52 | |
| 53 | async def test_dimmer_box_async_on_zero(dimmer_box: Light, product: Mock): |
| 54 | with pytest.raises(ValueError): |
| 55 | await dimmer_box.async_on(0) |
| 56 | |
| 57 | |
| 58 | async def test_dimmer_box_async_on_with_hex(dimmer_box: Light, product: Mock): |
| 59 | await dimmer_box.async_on("ff") |
| 60 | product.async_api_command.assert_called_with("set", 255) |
| 61 | |
| 62 | |
| 63 | @pytest.mark.parametrize("input_on", [255, "ff"]) |
| 64 | async def test_dimmer_box_async_on(dimmer_box: Light, product: Mock, input_on): |
| 65 | await dimmer_box.async_on(input_on) |
| 66 | product.async_api_command.assert_called_with("set", 255) |
| 67 | |
| 68 | |
| 69 | async def test_dimmer_box_async_off(dimmer_box: Light, product: Mock): |
| 70 | await dimmer_box.async_off() |
| 71 | product.async_api_command.assert_called_with("set", 0) |
| 72 | |
| 73 | |
| 74 | @pytest.mark.parametrize( |
| 75 | "io_params", |
| 76 | [("ff", [255]), ("ffff", [255, 255]), ("ff14cdfe6a", [255, 20, 205, 254, 106])], |
| 77 | ) |
| 78 | def test_dimmer_box_rgb_hex_to_rgb_list(dimmer_box: Light, io_params): |
| 79 | assert dimmer_box.rgb_hex_to_rgb_list(io_params[0]) == io_params[1] |
| 80 | |
| 81 | |
| 82 | @pytest.mark.parametrize( |
| 83 | "io_params", |
| 84 | [ |
| 85 | (["ff"], [255]), |
| 86 | (["ff", "ff"], [255, 255]), |
| 87 | (["ff", "14", "cd", "fe", "6a"], [255, 20, 205, 254, 106]), |
| 88 | ], |
| 89 | ) |
| 90 | def test_dimmer_box_rgb_list_to_rgb_hex_list(dimmer_box: Light, io_params): |
| 91 | assert dimmer_box.rgb_list_to_rgb_hex_list(io_params[1]) == io_params[0] |
| 92 | |
| 93 | |
| 94 | @pytest.mark.parametrize( |
| 95 | "io_params", |
| 96 | [ |
| 97 | ([255], [255]), |
| 98 | ([255, 255], [255, 255]), |
| 99 | ([120, 20, 205, 96, 106], [149, 25, 255, 119, 132]), |
| 100 | ], |
| 101 | ) |
| 102 | def test_dimmer_box_normalise_elements_of_rgb(dimmer_box: Light, io_params): |
| 103 | assert dimmer_box.normalise_elements_of_rgb(io_params[0]) == io_params[1] |
| 104 | |
| 105 | |
| 106 | def test_dimmer_box_normalise_elements_of_rgb_invalid_values(dimmer_box: Light): |
| 107 | with pytest.raises(ValueError): |
| 108 | dimmer_box.normalise_elements_of_rgb([-10]) |
| 109 | |
| 110 | with pytest.raises(ValueError): |
| 111 | dimmer_box.normalise_elements_of_rgb([256]) |
| 112 | |
| 113 | with pytest.raises(ValueError): |
| 114 | dimmer_box.normalise_elements_of_rgb([-1, 0]) |
| 115 | |
| 116 | |
| 117 | @pytest.mark.parametrize( |
| 118 | "io_params", [([145, 135, 90], 145), ([145, 135, 240], 240), ([255], 255)] |
| 119 | ) |
| 120 | def test_dimmer_box_evaluate_brightness_from_rgb(dimmer_box: Light, io_params): |
| 121 | assert dimmer_box.evaluate_brightness_from_rgb(io_params[0]) == io_params[1] |
| 122 | |
| 123 | |
| 124 | def test_dimmer_box_apply_brightness_zero(dimmer_box: Light): |
| 125 | assert dimmer_box.apply_brightness(10, 0) == [0] |
| 126 | |
| 127 | |
| 128 | def test_dimmer_box_evaluate_brightness_from_rgb_out_of_range(dimmer_box: Light): |
| 129 | with pytest.raises(ValueError): |
| 130 | dimmer_box.evaluate_brightness_from_rgb([257, 135, 90]) |
| 131 | |
| 132 | with pytest.raises(ValueError): |
| 133 | dimmer_box.evaluate_brightness_from_rgb([145, 135, -1]) |
| 134 | |
| 135 | with pytest.raises(ValueError): |
| 136 | dimmer_box.evaluate_brightness_from_rgb([145, -10, 900]) |
| 137 | |
| 138 | |
| 139 | def test_light_sensible_on_value_last_is_zero(w_light_box_rgbww: Light): |
| 140 | assert len(w_light_box_rgbww.effect_list) == 4 |
| 141 | assert w_light_box_rgbww.effect == "NONE" |