Mercurial > code > home > repos > light-bridge
annotate light.py @ 10:140633abfa2a
route colors from PUT to Light.setColor
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jan 2024 16:53:35 -0800 |
parents | 9f427d8073c3 |
children | 028ed39aa78f |
rev | line source |
---|---|
2 | 1 import asyncio |
2 import logging | |
9 | 3 from dataclasses import dataclass, field |
2 | 4 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
5 from color import Color |
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
6 |
2 | 7 log = logging.getLogger('light') |
8 | |
9 | |
9 | 10 @dataclass(frozen=True) |
11 class DeviceColor: | |
12 """neutral representation of the adjusted color that we send to a device""" | |
13 r: float = 0 | |
14 g: float = 0 | |
15 b: float = 0 | |
16 w: float = 0 | |
17 x: float = 0 | |
18 y: float = 0 | |
19 | |
20 def summary(self) -> dict: | |
21 return dict([(k, round(v, 3)) for k, v in self.__dict__.items() if v > 0]) | |
22 | |
23 | |
2 | 24 @dataclass |
25 class Light: | |
26 name: str | |
27 address: str | |
9 | 28 |
29 requestingColor: Color = Color.fromHex('#000000') | |
30 requestingDeviceColor: DeviceColor = DeviceColor() | |
31 | |
32 emittingColor: Color = Color.fromHex('#000000') | |
33 online: bool | None = None | |
34 latencyMs: float | None = None | |
35 | |
36 def __post_init__(self): | |
37 self.requestingDeviceColor = self.deviceColor(self.requestingColor) | |
2 | 38 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
39 def to_dict(self): |
9 | 40 d = { |
41 'name': self.name, | |
42 'address': self.address, | |
43 'requestingColor': self.requestingColor.hex(), | |
44 'requestingDeviceColor': self.requestingDeviceColor.summary(), | |
45 'emittingColor': self.emittingColor.hex(), | |
46 'online': self.online, | |
47 'latencyMs': self.latencyMs, | |
2 | 48 } |
49 | |
9 | 50 return {'light': d} |
51 | |
52 def deviceColor(self, c: Color) -> DeviceColor: | |
53 # do LUT here | |
54 return DeviceColor(r=c.r, g=c.g, b=c.b) | |
55 | |
56 async def setColor(self, c: Color): | |
57 self.requestingColor = c | |
58 self.requestingDeviceColor = self.deviceColor(self.requestingColor) | |
59 | |
2 | 60 |
61 class Lights: | |
62 _d: dict[str, Light] = {} | |
63 | |
6 | 64 def __init__(self): |
9 | 65 self.add(Light('do-desk', 'topic1')) |
66 self.add(Light('do-desk2', 'topic2')) | |
6 | 67 |
2 | 68 def add(self, d: Light): |
69 self._d[d.name] = d | |
70 | |
71 def byName(self, name: str) -> Light: | |
72 return self._d[name] | |
73 | |
74 async def changes(self): # yields None on any data change | |
75 while True: | |
76 yield None | |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
77 await asyncio.sleep(1) # todo |
2 | 78 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
79 def to_dict(self): |
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
80 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]} |