|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from yeelib.discover import YeelightProtocol, search_bulbs |
| 6 | +from yeelib.exceptions import YeelightError |
| 7 | + |
| 8 | + |
| 9 | +notify = b"""NOTIFY * HTTP/1.1 |
| 10 | +Host: 239.255.255.250:1982 |
| 11 | +Cache-Control: max-age=3600 |
| 12 | +Location: yeelight://192.168.1.239:55443 |
| 13 | +NTS: ssdp:alive |
| 14 | +Server: POSIX, UPnP/1.0 YGLC/1 |
| 15 | +id: 0x000000000015243f |
| 16 | +model: color |
| 17 | +fw_ver: 18 |
| 18 | +support: get_prop set_default set_power toggle set_bright start_cf stop_cf set_scene |
| 19 | +cron_add cron_get cron_del set_ct_abx set_rgb |
| 20 | +power: on |
| 21 | +bright: 100 |
| 22 | +color_mode: 2 |
| 23 | +ct: 4000 |
| 24 | +rgb: 16711680 |
| 25 | +hue: 100 |
| 26 | +sat: 35 |
| 27 | +name: my_bulb""" |
| 28 | + |
| 29 | +mcast = b"""HTTP/1.1 200 OK |
| 30 | +Cache-Control: max-age=3600 |
| 31 | +Date: |
| 32 | +Ext: |
| 33 | +Location: yeelight://192.168.1.239:55443 |
| 34 | +Server: POSIX UPnP/1.0 YGLC/1 |
| 35 | +id: 0x000000000015243f |
| 36 | +model: color |
| 37 | +fw_ver: 18 |
| 38 | +support: get_prop set_default set_power toggle set_bright start_cf stop_cf set_scene |
| 39 | +cron_add cron_get cron_del set_ct_abx set_rgb |
| 40 | +power: on |
| 41 | +bright: 100 |
| 42 | +color_mode: 2 |
| 43 | +ct: 4000 |
| 44 | +rgb: 16711680 |
| 45 | +hue: 100 |
| 46 | +sat: 35 |
| 47 | +name: my_bulb""" |
| 48 | + |
| 49 | +wrong_location = b"""HTTP/1.1 200 OK |
| 50 | +Cache-Control: max-age=3600 |
| 51 | +Date: |
| 52 | +Ext: |
| 53 | +Location: yeelight://not.an.ip:55443 |
| 54 | +Server: POSIX UPnP/1.0 YGLC/1""" |
| 55 | + |
| 56 | + |
| 57 | +class TestYeelightProtocoll: |
| 58 | + def test_notify(self, ): |
| 59 | + bulbs = {} |
| 60 | + p = YeelightProtocol(bulbs=bulbs) |
| 61 | + p.datagram_received(data=notify, addr=('192.168.1.239', 1982)) |
| 62 | + assert len(bulbs) == 1 |
| 63 | + assert bulbs['0x000000000015243f'].ip == '192.168.1.239' |
| 64 | + |
| 65 | + def test_mcast(self, ): |
| 66 | + bulbs = {} |
| 67 | + p = YeelightProtocol(bulbs=bulbs) |
| 68 | + p.datagram_received(data=mcast, addr=('192.168.1.239', 1982)) |
| 69 | + assert len(bulbs) == 1 |
| 70 | + assert bulbs['0x000000000015243f'].ip == '192.168.1.239' |
| 71 | + |
| 72 | + def test_duplicate(self): |
| 73 | + bulbs = {} |
| 74 | + p = YeelightProtocol(bulbs=bulbs) |
| 75 | + p.datagram_received(data=notify, addr=('192.168.1.239', 1982)) |
| 76 | + p.datagram_received(data=notify, addr=('192.168.1.239', 1982)) |
| 77 | + assert len(bulbs) == 1 |
| 78 | + assert bulbs['0x000000000015243f'].ip == '192.168.1.239' |
| 79 | + |
| 80 | + def test_wrong_location(self): |
| 81 | + bulbs = {} |
| 82 | + p = YeelightProtocol(bulbs=bulbs) |
| 83 | + with pytest.raises(YeelightError) as e: |
| 84 | + p.datagram_received(data=wrong_location, addr=('192.168.1.239', 1982)) |
| 85 | + assert 'Location does not match: yeelight://not.an.ip:55443' in str(e) |
| 86 | + |
| 87 | + |
| 88 | +def test_search_bulbs(): |
| 89 | + loop = asyncio.get_event_loop() |
| 90 | + with search_bulbs(): |
| 91 | + loop.run_until_complete(asyncio.sleep(1)) |
0 commit comments