-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue.py
69 lines (51 loc) · 1.88 KB
/
hue.py
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
import asyncio
import struct
from bleak import BleakClient
LIGHT_UUID = "932c32bd-0002-47a2-835a-a8d455b859dd"
BRIGHTNESS_UUID = "932c32bd-0003-47a2-835a-a8d455b859dd"
TEMPERATURE_UUID = "932c32bd-0004-47a2-835a-a8d455b859dd"
class HueLightInstance:
def __init__(self, mac: str) -> None:
self.client = BleakClient(mac)
self.connected = False
async def _write(self, uuid: str, data: bytes) -> None:
if not self.connected:
await self.connect()
await self.client.write_gatt_char(uuid, data)
async def _read(self, uuid: str) -> bytearray:
if not self.connected:
await self.connect()
return await self.client.read_gatt_char(uuid)
async def connect(self) -> None:
await self.client.connect(timeout=20)
await asyncio.sleep(1)
self.connected = True
async def set_brightness(self, brightness: int) -> None:
await self._write(BRIGHTNESS_UUID, struct.pack("B", brightness))
async def get_brightness(self) -> int:
ret_val = await self._read(BRIGHTNESS_UUID)
val = struct.unpack("B", ret_val)
return val[0]
async def turn_on(self) -> None:
await self._write(LIGHT_UUID, b"\x01")
async def turn_off(self) -> None:
await self._write(LIGHT_UUID, b"\x00")
async def is_on(self) -> bool:
ret_val = await self._read(LIGHT_UUID)
val = struct.unpack("B", ret_val)
return val[0]==1
async def main():
address = "CB:E0:41:A5:E6:42"
light = HueLightInstance(address)
for i in range(10, 250, 10):
await light.set_brightness(i)
await asyncio.sleep(0.1)
bright = await light.get_brightness()
print(bright)
await light.turn_off()
await asyncio.sleep(2)
await light.turn_on()
is_on = await light.is_on()
print(is_on)
if __name__ == "__main__":
asyncio.run(main())