comparison light.py @ 12:7cc004eafb82

refactor (approx)
author drewp@bigasterisk.com
date Sun, 28 Jan 2024 20:02:34 -0800
parents 028ed39aa78f
children 1c865af058e7
comparison
equal deleted inserted replaced
11:028ed39aa78f 12:7cc004eafb82
66 self.notifyChanged() 66 self.notifyChanged()
67 67
68 68
69 class Lights: 69 class Lights:
70 _d: dict[str, Light] = {} 70 _d: dict[str, Light] = {}
71 _changeSleepTask: asyncio.Task | None = None
72 71
73 def __init__(self): 72 def __init__(self):
74 self.add(Light('do-desk', 'topic1')) 73 self.add(Light('do-desk', 'topic1'))
75 self.add(Light('do-desk2', 'topic2')) 74 self.add(Light('do-desk2', 'topic2'))
76 75
80 79
81 self.notifyChanged() 80 self.notifyChanged()
82 81
83 def byName(self, name: str) -> Light: 82 def byName(self, name: str) -> Light:
84 return self._d[name] 83 return self._d[name]
84
85 def to_dict(self):
86 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]}
87
88 # the following is bad. Get a better events lib.
89 _changeSleepTask: asyncio.Task | None = None
85 90
86 async def changes(self): # yields None on any data change 91 async def changes(self): # yields None on any data change
87 while True: 92 while True:
88 log.info('Lights has a change') 93 log.info('Lights has a change')
89 yield None 94 yield None
99 104
100 def notifyChanged(self): 105 def notifyChanged(self):
101 log.info('Lights.notifyChanged()') 106 log.info('Lights.notifyChanged()')
102 if self._changeSleepTask is not None: 107 if self._changeSleepTask is not None:
103 self._changeSleepTask.cancel() 108 self._changeSleepTask.cancel()
104
105 def to_dict(self):
106 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]}