comparison 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
comparison
equal deleted inserted replaced
12:7cc004eafb82 13:1c865af058e7
1 import asyncio 1 import asyncio
2 import logging 2 import logging
3 from dataclasses import dataclass, field 3 from dataclasses import dataclass
4 from typing import Callable 4 from typing import Callable
5 5
6 from color import Color 6 from color import Color
7 7
8 log = logging.getLogger('light') 8 log = logging.getLogger('light')
20 20
21 def summary(self) -> dict: 21 def summary(self) -> dict:
22 return dict([(k, round(v, 3)) for k, v in self.__dict__.items() if v > 0]) 22 return dict([(k, round(v, 3)) for k, v in self.__dict__.items() if v > 0])
23 23
24 24
25 class Address:
26
27 def linked(self):
28 return {'label': str(self)}
29
30
31 class ZigbeeAddress(Address):
32
33 def __init__(self, name: str, ieee: str):
34 self.name = name
35 self.ieee = ieee
36
37 def linked(self):
38 return {'url': f'https://bigasterisk.com/zigbee/console/#/device/{self.ieee}/info', 'label': 'do-bar'}
39
40
25 @dataclass 41 @dataclass
26 class Light: 42 class Light:
27 name: str 43 name: str
28 address: str 44 address: Address
29 45
30 requestingColor: Color = Color.fromHex('#000000') 46 requestingColor: Color = Color.fromHex('#000000')
31 requestingDeviceColor: DeviceColor = DeviceColor() 47 requestingDeviceColor: DeviceColor = DeviceColor()
32 48
33 emittingColor: Color = Color.fromHex('#000000') 49 emittingColor: Color = Color.fromHex('#000000')
40 self.requestingDeviceColor = self.deviceColor(self.requestingColor) 56 self.requestingDeviceColor = self.deviceColor(self.requestingColor)
41 57
42 def to_dict(self): 58 def to_dict(self):
43 d = { 59 d = {
44 'name': self.name, 60 'name': self.name,
45 'address': self.address, 61 'address': self.address.linked(),
46 'requestingColor': self.requestingColor.hex(), 62 'requestingColor': self.requestingColor.hex(),
47 'requestingDeviceColor': self.requestingDeviceColor.summary(), 63 'requestingDeviceColor': self.requestingDeviceColor.summary(),
48 'emittingColor': self.emittingColor.hex(), 64 'emittingColor': self.emittingColor.hex(),
49 'online': self.online, 65 'online': self.online,
50 'latencyMs': self.latencyMs, 66 'latencyMs': self.latencyMs,
64 self.requestingDeviceColor = self.deviceColor(self.requestingColor) 80 self.requestingDeviceColor = self.deviceColor(self.requestingColor)
65 if self.notifyChanged: 81 if self.notifyChanged:
66 self.notifyChanged() 82 self.notifyChanged()
67 83
68 84
85 def makeZbBar(name: str, ieee: str) -> Light:
86 return Light(name=name, address=ZigbeeAddress(name, ieee))
87
88
69 class Lights: 89 class Lights:
70 _d: dict[str, Light] = {} 90 _d: dict[str, Light] = {}
71 91
72 def __init__(self): 92 def __init__(self):
73 self.add(Light('do-desk', 'topic1')) 93 self.add(makeZbBar('do-bar', '0xa4c13844948d2da4'))
74 self.add(Light('do-desk2', 'topic2'))
75 94
76 def add(self, d: Light): 95 def add(self, d: Light):
77 d.notifyChanged = self.notifyChanged 96 d.notifyChanged = self.notifyChanged
78 self._d[d.name] = d 97 self._d[d.name] = d
79 98