Mercurial > code > home > repos > light-bridge
annotate light.py @ 6:fc8ed0efcd72
move init to Lights
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jan 2024 16:02:31 -0800 |
parents | 7eeda7f4f9cd |
children | 9f427d8073c3 |
rev | line source |
---|---|
2 | 1 import asyncio |
2 import logging | |
3 from dataclasses import dataclass | |
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 | |
10 @dataclass | |
11 class Light: | |
12 name: str | |
13 address: str | |
14 online: bool | |
15 colorRequest: Color | |
16 colorMessage: dict | |
17 colorCurrent: Color | |
18 latencyMs: float | |
19 | |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
20 def to_dict(self): |
2 | 21 return { |
22 'light': { | |
23 'name': self.name, | |
24 'address': self.address, | |
25 'online': self.online, | |
26 'colorRequest': self.colorRequest.to_js(), | |
27 'colorMessage': self.colorMessage, | |
28 'colorCurrent': self.colorCurrent.to_js(), | |
29 'latencyMs': self.latencyMs, | |
30 } | |
31 } | |
32 | |
33 | |
34 class Lights: | |
35 _d: dict[str, Light] = {} | |
36 | |
6 | 37 def __init__(self): |
38 self.add(Light('do-desk', 'topic1', True, Color('#ff0000'), {'r': 255}, Color('#000000'), 100)) | |
39 self.add(Light('do-desk2', 'topic2', True, Color('#ff00ff'), {'r': 255}, Color('#000000'), 200)) | |
40 | |
2 | 41 def add(self, d: Light): |
42 self._d[d.name] = d | |
43 | |
44 def byName(self, name: str) -> Light: | |
45 return self._d[name] | |
46 | |
47 async def changes(self): # yields None on any data change | |
48 while True: | |
49 yield None | |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
50 await asyncio.sleep(1) # todo |
2 | 51 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
52 def to_dict(self): |
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
53 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]} |