Mercurial > code > home > repos > light-bridge
view 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 |
line wrap: on
line source
import asyncio import logging from dataclasses import dataclass from color import Color log = logging.getLogger('light') @dataclass class Light: name: str address: str online: bool colorRequest: Color colorMessage: dict colorCurrent: Color latencyMs: float def to_dict(self): return { 'light': { 'name': self.name, 'address': self.address, 'online': self.online, 'colorRequest': self.colorRequest.to_js(), 'colorMessage': self.colorMessage, 'colorCurrent': self.colorCurrent.to_js(), 'latencyMs': self.latencyMs, } } class Lights: _d: dict[str, Light] = {} def __init__(self): self.add(Light('do-desk', 'topic1', True, Color('#ff0000'), {'r': 255}, Color('#000000'), 100)) self.add(Light('do-desk2', 'topic2', True, Color('#ff00ff'), {'r': 255}, Color('#000000'), 200)) def add(self, d: Light): self._d[d.name] = d def byName(self, name: str) -> Light: return self._d[name] async def changes(self): # yields None on any data change while True: yield None await asyncio.sleep(1) # todo def to_dict(self): return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]}