annotate light.py @ 13:1c865af058e7

start make* funcs and add links to light addresses
author drewp@bigasterisk.com
date Sun, 28 Jan 2024 20:03:20 -0800
parents 7cc004eafb82
children e3dbd04dab96
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
1 import asyncio
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
2 import logging
13
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
3 from dataclasses import dataclass
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
4 from typing import Callable
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
5
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 4
diff changeset
6 from color import Color
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 4
diff changeset
7
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
8 log = logging.getLogger('light')
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
9
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
10
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
11 @dataclass(frozen=True)
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
12 class DeviceColor:
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
13 """neutral representation of the adjusted color that we send to a device"""
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
14 r: float = 0
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
15 g: float = 0
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
16 b: float = 0
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
17 w: float = 0
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
18 x: float = 0
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
19 y: float = 0
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
20
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
21 def summary(self) -> dict:
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
22 return dict([(k, round(v, 3)) for k, v in self.__dict__.items() if v > 0])
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
23
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
24
13
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
25 class Address:
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
26
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
27 def linked(self):
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
28 return {'label': str(self)}
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
29
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
30
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
31 class ZigbeeAddress(Address):
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
32
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
33 def __init__(self, name: str, ieee: str):
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
34 self.name = name
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
35 self.ieee = ieee
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
36
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
37 def linked(self):
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
38 return {'url': f'https://bigasterisk.com/zigbee/console/#/device/{self.ieee}/info', 'label': 'do-bar'}
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
39
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
40
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
41 @dataclass
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
42 class Light:
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
43 name: str
13
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
44 address: Address
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
45
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
46 requestingColor: Color = Color.fromHex('#000000')
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
47 requestingDeviceColor: DeviceColor = DeviceColor()
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
48
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
49 emittingColor: Color = Color.fromHex('#000000')
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
50 online: bool | None = None
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
51 latencyMs: float | None = None
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
52
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
53 notifyChanged: Callable | None = None
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
54
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
55 def __post_init__(self):
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
56 self.requestingDeviceColor = self.deviceColor(self.requestingColor)
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
57
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 4
diff changeset
58 def to_dict(self):
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
59 d = {
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
60 'name': self.name,
13
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
61 'address': self.address.linked(),
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
62 'requestingColor': self.requestingColor.hex(),
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
63 'requestingDeviceColor': self.requestingDeviceColor.summary(),
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
64 'emittingColor': self.emittingColor.hex(),
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
65 'online': self.online,
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
66 'latencyMs': self.latencyMs,
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
67 }
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
68
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
69 return {'light': d}
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
70
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
71 def deviceColor(self, c: Color) -> DeviceColor:
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
72 # do LUT here
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
73 return DeviceColor(r=c.r, g=c.g, b=c.b)
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
74
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
75 async def setColor(self, c: Color):
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
76 log.info(f'setColor from {self.requestingColor} to {c}')
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
77 if c == self.requestingColor:
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
78 return
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
79 self.requestingColor = c
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
80 self.requestingDeviceColor = self.deviceColor(self.requestingColor)
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
81 if self.notifyChanged:
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
82 self.notifyChanged()
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
83
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
84
13
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
85 def makeZbBar(name: str, ieee: str) -> Light:
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
86 return Light(name=name, address=ZigbeeAddress(name, ieee))
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
87
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
88
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
89 class Lights:
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
90 _d: dict[str, Light] = {}
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
91
6
fc8ed0efcd72 move init to Lights
drewp@bigasterisk.com
parents: 5
diff changeset
92 def __init__(self):
13
1c865af058e7 start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents: 12
diff changeset
93 self.add(makeZbBar('do-bar', '0xa4c13844948d2da4'))
6
fc8ed0efcd72 move init to Lights
drewp@bigasterisk.com
parents: 5
diff changeset
94
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
95 def add(self, d: Light):
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
96 d.notifyChanged = self.notifyChanged
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
97 self._d[d.name] = d
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
98
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
99 self.notifyChanged()
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
100
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
101 def byName(self, name: str) -> Light:
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
102 return self._d[name]
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
103
12
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
104 def to_dict(self):
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
105 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]}
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
106
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
107 # the following is bad. Get a better events lib.
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
108 _changeSleepTask: asyncio.Task | None = None
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
109
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
110 async def changes(self): # yields None on any data change
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
111 while True:
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
112 log.info('Lights has a change')
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
113 yield None
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
114 self._changeSleepTask = asyncio.create_task(self._sleep())
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
115 try:
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
116 await self._changeSleepTask
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
117 except asyncio.CancelledError:
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
118 pass
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
119 self._changeSleepTask = None
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
120
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
121 async def _sleep(self):
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
122 await asyncio.sleep(100)
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
123
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
124 def notifyChanged(self):
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
125 log.info('Lights.notifyChanged()')
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
126 if self._changeSleepTask is not None:
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
127 self._changeSleepTask.cancel()