| 1 | from collections import namedtuple |
| 2 | import orjson |
| 3 | from pyvesync.utils.colors import RGB |
| 4 | from pyvesync.devices.vesyncbulb import (VeSyncBulbESL100, VeSyncBulbESL100CW, |
| 5 | VeSyncBulbESL100MC, VeSyncBulbValcenoA19MC) |
| 6 | import call_json |
| 7 | import call_json_bulbs |
| 8 | from base_test_cases import TestBase |
| 9 | |
| 10 | DEV_LIST = call_json.DeviceList.device_list_response('ESL100') |
| 11 | |
| 12 | DEV_LIST_DETAIL = call_json.DeviceList.device_list_item('ESL100') |
| 13 | |
| 14 | DEV_LIST_DETAIL_CW = call_json.DeviceList.device_list_item('ESL100CW') |
| 15 | |
| 16 | DEV_LIST_CW = call_json.DeviceList.device_list_response('ESL100CW') |
| 17 | |
| 18 | DEV_LIST_DETAIL_MC = call_json.DeviceList.device_list_item('ESL100MC') |
| 19 | |
| 20 | DEV_LIST_MC = call_json.DeviceList.device_list_response('ESL100MC') |
| 21 | |
| 22 | DEV_LIST_DETAIL_VALCENO = call_json.DeviceList.device_list_item('XYD0001') |
| 23 | |
| 24 | DEV_LIST_VALCENO = call_json.DeviceList.device_list_response('XYD0001') |
| 25 | |
| 26 | DEVICE_DETAILS = call_json_bulbs.DETAILS_RESPONSES['ESL100'] |
| 27 | |
| 28 | DEVICE_DETAILS_CW = call_json_bulbs.DETAILS_RESPONSES['ESL100CW'] |
| 29 | |
| 30 | DEFAULTS = call_json.TestDefaults |
| 31 | |
| 32 | SUCCESS_RETURN = (orjson.dumps({'code': 0}), 200) |
| 33 | |
| 34 | |
| 35 | class TestVeSyncBulbESL100(TestBase): |
| 36 | """Tests for VeSync dimmable bulb.""" |
| 37 | device_type = 'ESL100' |
| 38 | |
| 39 | def test_esl100_conf(self): |
| 40 | """Tests that Wall Switch is instantiated properly.""" |
| 41 | self.mock_api.return_value = DEV_LIST |
| 42 | self.run_in_loop(self.manager.get_devices) |
| 43 | bulbs = self.manager.bulbs |
| 44 | assert len(bulbs) == 1 |
| 45 | bulb = bulbs[0] |
| 46 | assert isinstance(bulb, VeSyncBulbESL100) |
| 47 | assert bulb.device_name == call_json.TestDefaults.name(self.device_type) |
| 48 | assert bulb.device_type == self.device_type |
| 49 | assert bulb.cid == call_json.TestDefaults.cid(self.device_type) |
| 50 | assert bulb.uuid == call_json.TestDefaults.uuid(self.device_type) |
| 51 | |
| 52 | def test_esl100_details(self): |
| 53 | """Test WS get_details().""" |
| 54 | if callable(DEVICE_DETAILS): |
| 55 | resp_dict, status = DEVICE_DETAILS() |
| 56 | else: |
| 57 | resp_dict, status = DEVICE_DETAILS |
| 58 | self.mock_api.return_value = orjson.dumps(resp_dict), status |
| 59 | bulb = VeSyncBulbESL100(DEV_LIST_DETAIL, self.manager) |
| 60 | self.run_in_loop(bulb.get_details) |
| 61 | dev_details = bulb.details |
| 62 | assert bulb.device_status == 'on' |
| 63 | assert isinstance(dev_details, dict) |
| 64 | assert bulb.connection_status == 'online' |
| 65 | |
| 66 | def test_esl100_no_details(self): |
| 67 | """Test no device details for disconnected bulb.""" |
| 68 | self.mock_api.return_value = (orjson.dumps({'code': 5}), 200) |
| 69 | bulb = VeSyncBulbESL100(DEV_LIST_DETAIL, self.manager) |
| 70 | self.run_in_loop(bulb.update) |
| 71 | assert len(self.caplog.records) == 1 |
| 72 | |
| 73 | def test_esl100_onoff(self): |
| 74 | """Test power toggle for ESL100 bulb.""" |
| 75 | self.mock_api.return_value = (orjson.dumps({'code': 0}), 200) |
| 76 | bulb = VeSyncBulbESL100(DEV_LIST_DETAIL, self.manager) |
| 77 | assert self.run_in_loop(bulb.turn_off) |
| 78 | assert self.run_in_loop(bulb.turn_on) |
| 79 | |
| 80 | def test_brightness(self): |
| 81 | self.mock_api.return_value = (orjson.dumps({'code': 0}), 200) |
| 82 | bulb = VeSyncBulbESL100(DEV_LIST_DETAIL, self.manager) |
| 83 | assert self.run_in_loop(bulb.set_brightness, 50) |
| 84 | assert self.run_in_loop(bulb.turn_off) |
| 85 | assert self.run_in_loop(bulb.set_brightness, 50) |
| 86 | assert bulb.device_status == 'on' |
| 87 | |
| 88 | def test_invalid_brightness(self): |
| 89 | self.mock_api.return_value = (orjson.dumps({'code': 0}), 200) |
| 90 | bulb = VeSyncBulbESL100(DEV_LIST_DETAIL, self.manager) |
| 91 | assert self.run_in_loop(bulb.set_brightness, 5000) |
| 92 | assert bulb.brightness == 100 |
| 93 | |
| 94 | def test_features(self): |
| 95 | bulb = VeSyncBulbESL100(DEV_LIST_DETAIL, self.manager) |
| 96 | assert bulb.dimmable_feature |
| 97 | assert not bulb.color_temp_feature |
| 98 | assert not bulb.rgb_shift_feature |
| 99 | |
| 100 | |
| 101 | class TestVeSyncBulbESL100CW(TestBase): |
| 102 | """Tests for VeSync dimmable bulb.""" |
| 103 | device_type = 'ESL100CW' |
| 104 | |
| 105 | def test_esl100cw_conf(self): |
| 106 | """Tests that Wall Switch is instantiated properly.""" |
| 107 | if callable(DEV_LIST_CW): |
| 108 | resp_dict, status = DEV_LIST_CW() |
| 109 | else: |
| 110 | resp_dict, status = DEV_LIST_CW |
| 111 | if isinstance(resp_dict, str): |
| 112 | resp_str = resp_dict.encode('utf-8') |
| 113 | elif isinstance(resp_dict, dict): |
| 114 | resp_str = orjson.dumps(resp_dict) |
| 115 | elif isinstance(resp_dict, bytes): |
| 116 | resp_str = resp_dict |
| 117 | self.mock_api.return_value = resp_str, status |
| 118 | self.run_in_loop(self.manager.get_devices) |
| 119 | bulbs = self.manager.bulbs |
| 120 | assert len(bulbs) == 1 |
| 121 | bulb = bulbs[0] |
| 122 | assert isinstance(bulb, VeSyncBulbESL100CW) |
| 123 | assert bulb.device_name == DEFAULTS.name(self.device_type) |
| 124 | assert bulb.device_type == self.device_type |
| 125 | assert bulb.cid == DEFAULTS.cid(self.device_type) |
| 126 | assert bulb.uuid == DEFAULTS.uuid(self.device_type) |
| 127 | |
| 128 | def test_esl100cw_details(self): |
| 129 | """Test WS get_details().""" |
| 130 | if callable(DEVICE_DETAILS_CW): |
| 131 | resp_dict, status = DEVICE_DETAILS_CW() |
| 132 | else: |
| 133 | resp_dict, status = DEVICE_DETAILS_CW |
| 134 | if isinstance(resp_dict, str): |
| 135 | resp_bytes = resp_dict.encode('utf-8') |
| 136 | elif isinstance(resp_dict, dict): |
| 137 | resp_bytes = orjson.dumps(resp_dict) |
| 138 | elif isinstance(resp_dict, bytes): |
| 139 | resp_bytes = resp_dict |
| 140 | self.mock_api.return_value = resp_bytes, status |
| 141 | bulb = VeSyncBulbESL100CW(DEV_LIST_DETAIL_CW, self.manager) |
| 142 | self.run_in_loop(bulb.get_details) |
| 143 | assert self.mock_api.r |
| 144 | assert bulb.device_status == 'on' |
| 145 | assert bulb.connection_status == 'online' |
| 146 | |
| 147 | def test_esl100cw_onoff(self): |
| 148 | """Test power toggle for ESL100 bulb.""" |
| 149 | self.mock_api.return_value = SUCCESS_RETURN |
| 150 | bulb = VeSyncBulbESL100CW(DEV_LIST_DETAIL_CW, self.manager) |
| 151 | assert self.run_in_loop(bulb.turn_off) |
| 152 | assert bulb.device_status == 'off' |
| 153 | assert self.run_in_loop(bulb.turn_on) |
| 154 | assert bulb.device_status == 'on' |
| 155 | |
| 156 | def test_brightness(self): |
| 157 | self.mock_api.return_value = SUCCESS_RETURN |
| 158 | bulb = VeSyncBulbESL100CW(DEV_LIST_DETAIL_CW, self.manager) |
| 159 | assert self.run_in_loop(bulb.set_brightness, 50) |
| 160 | assert bulb.brightness == 50 |
| 161 | assert self.run_in_loop(bulb.turn_off) |
| 162 | assert self.run_in_loop(bulb.set_brightness, 50) |
| 163 | assert bulb.device_status == 'on' |
| 164 | |
| 165 | def test_invalid_brightness(self): |
| 166 | self.mock_api.return_value = SUCCESS_RETURN |
| 167 | bulb = VeSyncBulbESL100CW(DEV_LIST_DETAIL_CW, self.manager) |
| 168 | assert self.run_in_loop(bulb.set_brightness, 5000) |
| 169 | assert bulb.brightness == 100 |
| 170 | |
| 171 | def test_color_temp(self): |
| 172 | self.mock_api.return_value = SUCCESS_RETURN |
| 173 | bulb = VeSyncBulbESL100CW(DEV_LIST_DETAIL_CW, self.manager) |
| 174 | assert self.run_in_loop(bulb.set_color_temp, 50) |
| 175 | assert bulb.color_temp_pct == 50 |
| 176 | |
| 177 | def test_features(self): |
| 178 | bulb = VeSyncBulbESL100CW(DEV_LIST_DETAIL_CW, self.manager) |
| 179 | assert bulb.dimmable_feature |
| 180 | assert bulb.color_temp_feature |
| 181 | assert not bulb.rgb_shift_feature |
| 182 | |
| 183 | |
| 184 | class TestVeSyncBulbESL100MC(TestBase): |
| 185 | """Tests for VeSync dimmable bulb.""" |
| 186 | device_type = 'ESL100MC' |
| 187 | |
| 188 | def test_esl100mc_conf(self): |
| 189 | """Tests that Wall Switch is instantiated properly.""" |
| 190 | self.mock_api.return_value = DEV_LIST_MC |
| 191 | self.run_in_loop(self.manager.get_devices) |
| 192 | bulbs = self.manager.bulbs |
| 193 | assert len(bulbs) == 1 |
| 194 | bulb = bulbs[0] |
| 195 | assert isinstance(bulb, VeSyncBulbESL100MC) |
| 196 | assert bulb.device_name == DEFAULTS.name(self.device_type) |
| 197 | assert bulb.device_type == self.device_type |
| 198 | assert bulb.cid == DEFAULTS.cid(self.device_type) |
| 199 | assert bulb.uuid == DEFAULTS.uuid(self.device_type) |
| 200 | |
| 201 | def test_esl100mc_details(self): |
| 202 | """Test WS get_details().""" |
| 203 | self.mock_api.return_value = call_json_bulbs.DETAILS_RESPONSES[self.device_type] |
| 204 | bulb = VeSyncBulbESL100MC(DEV_LIST_DETAIL_MC, self.manager) |
| 205 | self.run_in_loop(bulb.get_details) |
| 206 | assert self.mock_api.r |
| 207 | assert bulb.device_status == 'on' |
| 208 | assert bulb.connection_status == 'online' |
| 209 | |
| 210 | def test_esl100mc_onoff(self): |
| 211 | """Test power toggle for ESL100 bulb.""" |
| 212 | self.mock_api.return_value = SUCCESS_RETURN |
| 213 | bulb = VeSyncBulbESL100MC(DEV_LIST_DETAIL_MC, self.manager) |
| 214 | assert self.run_in_loop(bulb.turn_off) |
| 215 | assert bulb.device_status == 'off' |
| 216 | assert self.run_in_loop(bulb.turn_on) |
| 217 | assert bulb.device_status == 'on' |
| 218 | |
| 219 | def test_brightness(self): |
| 220 | self.mock_api.return_value = SUCCESS_RETURN |
| 221 | bulb = VeSyncBulbESL100MC(DEV_LIST_DETAIL_MC, self.manager) |
| 222 | assert self.run_in_loop(bulb.set_brightness, 50) |
| 223 | assert bulb.brightness == 50 |
| 224 | assert self.run_in_loop(bulb.turn_off) |
| 225 | assert self.run_in_loop(bulb.set_brightness, 50) |
| 226 | assert bulb.device_status == 'on' |
| 227 | |
| 228 | def test_invalid_brightness(self): |
| 229 | self.mock_api.return_value = SUCCESS_RETURN |
| 230 | bulb = VeSyncBulbESL100MC(DEV_LIST_DETAIL_MC, self.manager) |
| 231 | assert self.run_in_loop(bulb.set_brightness, 5000) |
| 232 | assert bulb.brightness == 100 |
| 233 | |
| 234 | def test_color(self): |
| 235 | self.mock_api.return_value = SUCCESS_RETURN |
| 236 | bulb = VeSyncBulbESL100MC(DEV_LIST_DETAIL_MC, self.manager) |
| 237 | assert self.run_in_loop(bulb.set_rgb, 50, 100, 150) |
| 238 | assert bulb.color_rgb == namedtuple('rgb', 'red green blue')(50, 100, 150) |
| 239 | |
| 240 | def test_features(self): |
| 241 | bulb = VeSyncBulbESL100MC(DEV_LIST_DETAIL_MC, self.manager) |
| 242 | assert bulb.dimmable_feature |
| 243 | assert not bulb.color_temp_feature |
| 244 | assert bulb.rgb_shift_feature |
| 245 | |
| 246 | |
| 247 | class TestVeSyncBulbValceno(TestBase): |
| 248 | """Tests for VeSync Valceno bulb.""" |
| 249 | device_type = 'XYD0001' |
| 250 | |
| 251 | def test_valceno_conf(self): |
| 252 | """Tests that Valceno is instantiated properly.""" |
| 253 | self.mock_api.return_value = DEV_LIST_VALCENO |
| 254 | self.run_in_loop(self.manager.get_devices) |
| 255 | bulbs = self.manager.bulbs |
| 256 | assert len(bulbs) == 1 |
| 257 | bulb = bulbs[0] |
| 258 | assert isinstance(bulb, VeSyncBulbValcenoA19MC) |
| 259 | assert bulb.device_name == DEFAULTS.name(self.device_type) |
| 260 | assert bulb.device_type == self.device_type |
| 261 | assert bulb.cid == DEFAULTS.cid(self.device_type) |
| 262 | assert bulb.uuid == DEFAULTS.uuid(self.device_type) |
| 263 | |
| 264 | def test_valceno_details(self): |
| 265 | """Test Valceno get_details().""" |
| 266 | self.mock_api.return_value = DEV_LIST_VALCENO |
| 267 | bulb = VeSyncBulbValcenoA19MC(DEV_LIST_DETAIL_VALCENO, self.manager) |
| 268 | self.run_in_loop(bulb.get_details) |
| 269 | assert self.mock_api.r |
| 270 | assert bulb.device_status == 'on' |
| 271 | assert bulb.connection_status == 'online' |
| 272 | |
| 273 | def test_valceno_onoff(self): |
| 274 | """Test power toggle for Valceno MC bulb.""" |
| 275 | self.mock_api.return_value = SUCCESS_RETURN |
| 276 | bulb = VeSyncBulbValcenoA19MC(DEV_LIST_DETAIL_VALCENO, self.manager) |
| 277 | assert self.run_in_loop(bulb.turn_off) |
| 278 | assert bulb.device_status == 'off' |
| 279 | assert self.run_in_loop(bulb.turn_on) |
| 280 | assert bulb.device_status == 'on' |
| 281 | |
| 282 | def test_brightness(self): |
| 283 | """Test brightness on Valceno.""" |
| 284 | self.mock_api.return_value = SUCCESS_RETURN |
| 285 | bulb = VeSyncBulbValcenoA19MC(DEV_LIST_DETAIL_VALCENO, self.manager) |
| 286 | assert self.run_in_loop(bulb.set_brightness, 50) |
| 287 | assert bulb.brightness == 50 |
| 288 | assert self.run_in_loop(bulb.turn_off) |
| 289 | assert self.run_in_loop(bulb.set_brightness, 50) |
| 290 | assert bulb.device_status == 'on' |
| 291 | |
| 292 | def test_invalid_brightness(self): |
| 293 | """Test invalid brightness on Valceno.""" |
| 294 | self.mock_api.return_value = SUCCESS_RETURN |
| 295 | bulb = VeSyncBulbValcenoA19MC(DEV_LIST_DETAIL_VALCENO, self.manager) |
| 296 | assert not self.run_in_loop(bulb.set_brightness, 5000) |
| 297 | |
| 298 | def test_invalid_saturation(self): |
| 299 | """Test invalid saturation on Valceno.""" |
| 300 | self.mock_api.return_value = SUCCESS_RETURN |
| 301 | bulb = VeSyncBulbValcenoA19MC(DEV_LIST_DETAIL_VALCENO, self.manager) |
| 302 | assert self.run_in_loop(bulb.set_color_saturation, 100) |
| 303 | body_dict = { |
| 304 | "method": "setLightStatusV2", |
| 305 | "source": "APP", |
| 306 | "data": |
| 307 | { |
| 308 | "force": 1, |
| 309 | "brightness": "", |
| 310 | "colorTemp": "", |
| 311 | "colorMode": "hsv", |
| 312 | "hue": "", |
| 313 | "saturation": 1000, |
| 314 | "value": "" |
| 315 | } |
| 316 | } |
| 317 | mock_call = self.mock_api.call_args[1]['json_object']['payload'] |
| 318 | assert mock_call == body_dict |
| 319 | |
| 320 | def test_color(self): |
| 321 | """Test set color on Valceno.""" |
| 322 | self.mock_api.return_value = (orjson.dumps({ |
| 323 | 'code': 0, |
| 324 | 'msg': '', |
| 325 | 'result': { |
| 326 | 'code': 0, |
| 327 | 'result': { |
| 328 | "enabled": 'on', |
| 329 | "colorMode": 'hsv', |
| 330 | 'brightness': 100, |
| 331 | 'hue': 5833, |
| 332 | 'saturation': 6700, |
| 333 | 'value': 59 |
| 334 | } |
| 335 | } |
| 336 | }), 200) |
| 337 | bulb = VeSyncBulbValcenoA19MC(DEV_LIST_DETAIL_VALCENO, self.manager) |
| 338 | assert self.run_in_loop(bulb.set_rgb, 50, 100, 150) |
| 339 | assert bulb.color_rgb == RGB(50, 100, 150) |
| 340 | |
| 341 | def test_hue(self): |
| 342 | """Test hue on Valceno MC Bulb.""" |
| 343 | self.mock_api.return_value = SUCCESS_RETURN |
| 344 | bulb = VeSyncBulbValcenoA19MC(DEV_LIST_DETAIL_VALCENO, self.manager) |
| 345 | self.run_in_loop(bulb.set_color_hue, 230.5) |
| 346 | body_dict = { |
| 347 | "method": "setLightStatusV2", |
| 348 | "source": "APP", |
| 349 | "data": |
| 350 | { |
| 351 | "force": 1, |
| 352 | "brightness": "", |
| 353 | "colorTemp": "", |
| 354 | "colorMode": "hsv", |
| 355 | "hue": 6403, |
| 356 | "saturation": "", |
| 357 | "value": "" |
| 358 | } |
| 359 | } |
| 360 | mock_call = self.mock_api.call_args[1]['json_object']['payload'] |
| 361 | assert mock_call == body_dict |
| 362 | |
| 363 | def test_features(self): |
| 364 | bulb = VeSyncBulbValcenoA19MC(DEV_LIST_DETAIL_VALCENO, self.manager) |
| 365 | assert bulb.dimmable_feature |
| 366 | assert bulb.color_temp_feature |
| 367 | assert bulb.rgb_shift_feature |