File: test_philips_bulb.py

package info (click to toggle)
python-miio 0.5.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,288 kB
  • sloc: python: 14,798; makefile: 20
file content (265 lines) | stat: -rw-r--r-- 8,500 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from unittest import TestCase

import pytest

from miio import PhilipsBulb, PhilipsWhiteBulb
from miio.philips_bulb import (
    MODEL_PHILIPS_LIGHT_BULB,
    MODEL_PHILIPS_LIGHT_HBULB,
    PhilipsBulbException,
    PhilipsBulbStatus,
)

from .dummies import DummyDevice


class DummyPhilipsBulb(DummyDevice, PhilipsBulb):
    def __init__(self, *args, **kwargs):
        self.model = MODEL_PHILIPS_LIGHT_BULB
        self.state = {"power": "on", "bright": 100, "cct": 10, "snm": 0, "dv": 0}
        self.return_values = {
            "get_prop": self._get_state,
            "set_power": lambda x: self._set_state("power", x),
            "set_bright": lambda x: self._set_state("bright", x),
            "set_cct": lambda x: self._set_state("cct", x),
            "delay_off": lambda x: self._set_state("dv", x),
            "apply_fixed_scene": lambda x: self._set_state("snm", x),
            "set_bricct": lambda x: (
                self._set_state("bright", [x[0]]),
                self._set_state("cct", [x[1]]),
            ),
        }
        super().__init__(args, kwargs)


@pytest.fixture(scope="class")
def philips_bulb(request):
    request.cls.device = DummyPhilipsBulb()
    # TODO add ability to test on a real device


@pytest.mark.usefixtures("philips_bulb")
class TestPhilipsBulb(TestCase):
    def is_on(self):
        return self.device.status().is_on

    def state(self):
        return self.device.status()

    def test_on(self):
        self.device.off()  # ensure off
        assert self.is_on() is False

        self.device.on()
        assert self.is_on() is True

    def test_off(self):
        self.device.on()  # ensure on
        assert self.is_on() is True

        self.device.off()
        assert self.is_on() is False

    def test_status(self):
        self.device._reset_state()

        assert repr(self.state()) == repr(PhilipsBulbStatus(self.device.start_state))

        assert self.is_on() is True
        assert self.state().brightness == self.device.start_state["bright"]
        assert self.state().color_temperature == self.device.start_state["cct"]
        assert self.state().scene == self.device.start_state["snm"]
        assert self.state().delay_off_countdown == self.device.start_state["dv"]

    def test_set_brightness(self):
        def brightness():
            return self.device.status().brightness

        self.device.set_brightness(1)
        assert brightness() == 1
        self.device.set_brightness(50)
        assert brightness() == 50
        self.device.set_brightness(100)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness(-1)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness(0)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness(101)

    def test_set_color_temperature(self):
        def color_temperature():
            return self.device.status().color_temperature

        self.device.set_color_temperature(20)
        assert color_temperature() == 20
        self.device.set_color_temperature(30)
        assert color_temperature() == 30
        self.device.set_color_temperature(10)

        with pytest.raises(PhilipsBulbException):
            self.device.set_color_temperature(-1)

        with pytest.raises(PhilipsBulbException):
            self.device.set_color_temperature(0)

        with pytest.raises(PhilipsBulbException):
            self.device.set_color_temperature(101)

    def test_set_brightness_and_color_temperature(self):
        def color_temperature():
            return self.device.status().color_temperature

        def brightness():
            return self.device.status().brightness

        self.device.set_brightness_and_color_temperature(20, 21)
        assert brightness() == 20
        assert color_temperature() == 21
        self.device.set_brightness_and_color_temperature(31, 30)
        assert brightness() == 31
        assert color_temperature() == 30
        self.device.set_brightness_and_color_temperature(10, 11)
        assert brightness() == 10
        assert color_temperature() == 11

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness_and_color_temperature(-1, 10)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness_and_color_temperature(10, -1)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness_and_color_temperature(0, 10)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness_and_color_temperature(10, 0)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness_and_color_temperature(101, 10)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness_and_color_temperature(10, 101)

    def test_delay_off(self):
        def delay_off_countdown():
            return self.device.status().delay_off_countdown

        self.device.delay_off(100)
        assert delay_off_countdown() == 100
        self.device.delay_off(200)
        assert delay_off_countdown() == 200

        with pytest.raises(PhilipsBulbException):
            self.device.delay_off(-1)

        with pytest.raises(PhilipsBulbException):
            self.device.delay_off(0)

    def test_set_scene(self):
        def scene():
            return self.device.status().scene

        self.device.set_scene(1)
        assert scene() == 1
        self.device.set_scene(2)
        assert scene() == 2

        with pytest.raises(PhilipsBulbException):
            self.device.set_scene(-1)

        with pytest.raises(PhilipsBulbException):
            self.device.set_scene(0)

        with pytest.raises(PhilipsBulbException):
            self.device.set_scene(5)


class DummyPhilipsWhiteBulb(DummyDevice, PhilipsWhiteBulb):
    def __init__(self, *args, **kwargs):
        self.model = MODEL_PHILIPS_LIGHT_HBULB
        self.state = {"power": "on", "bri": 100, "dv": 0}
        self.return_values = {
            "get_prop": self._get_state,
            "set_power": lambda x: self._set_state("power", x),
            "set_bright": lambda x: self._set_state("bri", x),
            "delay_off": lambda x: self._set_state("dv", x),
        }
        super().__init__(args, kwargs)


@pytest.fixture(scope="class")
def philips_white_bulb(request):
    request.cls.device = DummyPhilipsWhiteBulb()
    # TODO add ability to test on a real device


@pytest.mark.usefixtures("philips_white_bulb")
class TestPhilipsWhiteBulb(TestCase):
    def is_on(self):
        return self.device.status().is_on

    def state(self):
        return self.device.status()

    def test_on(self):
        self.device.off()  # ensure off
        assert self.is_on() is False

        self.device.on()
        assert self.is_on() is True

    def test_off(self):
        self.device.on()  # ensure on
        assert self.is_on() is True

        self.device.off()
        assert self.is_on() is False

    def test_status(self):
        self.device._reset_state()

        assert repr(self.state()) == repr(PhilipsBulbStatus(self.device.start_state))

        assert self.is_on() is True
        assert self.state().brightness == self.device.start_state["bri"]
        assert self.state().delay_off_countdown == self.device.start_state["dv"]
        assert self.state().color_temperature is None
        assert self.state().scene is None

    def test_set_brightness(self):
        def brightness():
            return self.device.status().brightness

        self.device.set_brightness(1)
        assert brightness() == 1
        self.device.set_brightness(50)
        assert brightness() == 50
        self.device.set_brightness(100)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness(-1)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness(0)

        with pytest.raises(PhilipsBulbException):
            self.device.set_brightness(101)

    def test_delay_off(self):
        def delay_off_countdown():
            return self.device.status().delay_off_countdown

        self.device.delay_off(100)
        assert delay_off_countdown() == 100
        self.device.delay_off(200)
        assert delay_off_countdown() == 200

        with pytest.raises(PhilipsBulbException):
            self.device.delay_off(-1)

        with pytest.raises(PhilipsBulbException):
            self.device.delay_off(0)