Mercurial > code > home > repos > light-bridge
comparison light.py @ 15:61d4ccecfed8
rough refactor
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jan 2024 21:18:01 -0800 |
parents | e3dbd04dab96 |
children | 1d15dc4d673f |
comparison
equal
deleted
inserted
replaced
14:e3dbd04dab96 | 15:61d4ccecfed8 |
---|---|
6 | 6 |
7 from color import Color | 7 from color import Color |
8 from mqtt_io import MqttIo | 8 from mqtt_io import MqttIo |
9 | 9 |
10 log = logging.getLogger('light') | 10 log = logging.getLogger('light') |
11 | |
12 | |
13 @dataclass(frozen=True) | |
14 class DeviceColor: | |
15 """neutral representation of the adjusted color that we send to a device""" | |
16 r: float = 0 | |
17 g: float = 0 | |
18 b: float = 0 | |
19 w: float = 0 | |
20 x: float = 0 | |
21 y: float = 0 | |
22 | |
23 def summary(self) -> dict: | |
24 return dict([(k, round(v, 3)) for k, v in self.__dict__.items() if v > 0]) | |
25 | |
26 | |
27 class Transport: | |
28 | |
29 def linked(self): | |
30 return {'label': str(self)} | |
31 | |
32 async def send(self, dc: DeviceColor): | |
33 raise TypeError | |
34 | |
35 | |
36 def zigbeeHexMessage(color: DeviceColor, bw=False) -> dict: | |
37 bright = max(color.r, color.g, color.b) | |
38 msg: dict = {"transition": 0, "brightness": int(255 * bright)} | |
39 if not bw: | |
40 c = "#%02x%02x%02x" % (int(color.r * 255), int(color.g * 255), int(color.b * 255)) | |
41 msg["color"] = {"hex": c} | |
42 return msg | |
43 | |
44 | |
45 class ZigbeeTransport(Transport): | |
46 | |
47 def __init__(self, mqtt: MqttIo, name: str, ieee: str): | |
48 self.mqtt = mqtt | |
49 self.name = name | |
50 self.ieee = ieee | |
51 | |
52 def linked(self): | |
53 return {'url': f'https://bigasterisk.com/zigbee/console/#/device/{self.ieee}/info', 'label': 'do-bar'} | |
54 | |
55 async def send(self, dc: DeviceColor): | |
56 await self.mqtt.publish(f'zigbee/{self.name}/set', json.dumps(zigbeeHexMessage(dc, bw=False))) | |
57 | 11 |
58 | 12 |
59 @dataclass | 13 @dataclass |
60 class Light: | 14 class Light: |
61 name: str | 15 name: str |