Mercurial > code > home > repos > light-bridge
comparison light.py @ 9:9f427d8073c3
redo data model; add ui colors
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jan 2024 16:53:08 -0800 |
parents | fc8ed0efcd72 |
children | 028ed39aa78f |
comparison
equal
deleted
inserted
replaced
8:181a86533286 | 9:9f427d8073c3 |
---|---|
1 import asyncio | 1 import asyncio |
2 import logging | 2 import logging |
3 from dataclasses import dataclass | 3 from dataclasses import dataclass, field |
4 | 4 |
5 from color import Color | 5 from color import Color |
6 | 6 |
7 log = logging.getLogger('light') | 7 log = logging.getLogger('light') |
8 | 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 | |
10 @dataclass | 24 @dataclass |
11 class Light: | 25 class Light: |
12 name: str | 26 name: str |
13 address: str | 27 address: str |
14 online: bool | 28 |
15 colorRequest: Color | 29 requestingColor: Color = Color.fromHex('#000000') |
16 colorMessage: dict | 30 requestingDeviceColor: DeviceColor = DeviceColor() |
17 colorCurrent: Color | 31 |
18 latencyMs: float | 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) | |
19 | 38 |
20 def to_dict(self): | 39 def to_dict(self): |
21 return { | 40 d = { |
22 'light': { | 41 'name': self.name, |
23 'name': self.name, | 42 'address': self.address, |
24 'address': self.address, | 43 'requestingColor': self.requestingColor.hex(), |
25 'online': self.online, | 44 'requestingDeviceColor': self.requestingDeviceColor.summary(), |
26 'colorRequest': self.colorRequest.to_js(), | 45 'emittingColor': self.emittingColor.hex(), |
27 'colorMessage': self.colorMessage, | 46 'online': self.online, |
28 'colorCurrent': self.colorCurrent.to_js(), | 47 'latencyMs': self.latencyMs, |
29 'latencyMs': self.latencyMs, | |
30 } | |
31 } | 48 } |
49 | |
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) | |
32 | 59 |
33 | 60 |
34 class Lights: | 61 class Lights: |
35 _d: dict[str, Light] = {} | 62 _d: dict[str, Light] = {} |
36 | 63 |
37 def __init__(self): | 64 def __init__(self): |
38 self.add(Light('do-desk', 'topic1', True, Color('#ff0000'), {'r': 255}, Color('#000000'), 100)) | 65 self.add(Light('do-desk', 'topic1')) |
39 self.add(Light('do-desk2', 'topic2', True, Color('#ff00ff'), {'r': 255}, Color('#000000'), 200)) | 66 self.add(Light('do-desk2', 'topic2')) |
40 | 67 |
41 def add(self, d: Light): | 68 def add(self, d: Light): |
42 self._d[d.name] = d | 69 self._d[d.name] = d |
43 | 70 |
44 def byName(self, name: str) -> Light: | 71 def byName(self, name: str) -> Light: |