Mercurial > code > home > repos > light-bridge
annotate light.py @ 5:7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jan 2024 16:01:43 -0800 |
parents | e8e4fd6d5619 |
children | fc8ed0efcd72 |
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 | |
37 def add(self, d: Light): | |
38 self._d[d.name] = d | |
39 | |
40 def byName(self, name: str) -> Light: | |
41 return self._d[name] | |
42 | |
43 async def changes(self): # yields None on any data change | |
44 while True: | |
45 yield None | |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
46 await asyncio.sleep(1) # todo |
2 | 47 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
48 def to_dict(self): |
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
49 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]} |