Mercurial > code > home > repos > light-bridge
diff protocols.py @ 28:fb2e91f230f4
new wled mode and more lights
author | drewp@bigasterisk.com |
---|---|
date | Mon, 02 Sep 2024 20:00:53 -0700 |
parents | 33b3eb24506e |
children |
line wrap: on
line diff
--- a/protocols.py Sat Mar 23 15:25:02 2024 -0700 +++ b/protocols.py Mon Sep 02 20:00:53 2024 -0700 @@ -1,6 +1,9 @@ +import json import logging -import json +from enum import Enum + import aiohttp + from color_convert import DeviceColor from mqtt_io import MqttIo @@ -116,19 +119,32 @@ # {"POWER":"ON","Dimmer":21,"Color":"3636363600","HSBColor":"0,0,21","White":21,"CT":153,"Channel":[21,21,21,21,0]} +class WledControl(Enum): + wholeStringColor = 'wholeStringColor' + brightness = 'brightness' + + class WledTransport(_WebTransport): + def __init__(self, hostname: str, control: WledControl): + super().__init__(hostname) + self.control = control + async def send(self, dc: DeviceColor): - # see https://kno.wled.ge/interfaces/json-api/ - msg = { - 'seg': [{ - "cct": 128, # 0 warm; 255 cool - "col": [[to8(x) for x in [dc.r, dc.g, dc.b, dc.cw]], [], []] - }] - } + if self.control == WledControl.wholeStringColor: + # see https://kno.wled.ge/interfaces/json-api/ + msg = { + 'seg': [{ + "cct": 128, # 0 warm; 255 cool + "col": [[to8(x) for x in [dc.r, dc.g, dc.b, dc.cw]], [], []] + }] + } + elif self.control == WledControl.brightness: + msg = {"bri": to8(dc.brightness)} + else: + raise NotImplementedError async with self._session.post(f'http://{self.hostname}/json/state', headers={'content-type': 'application/json'}, data=json.dumps(msg)) as resp: await resp.text() - # {"POWER":"ON","Dimmer":21,"Color":"3636363600","HSBColor":"0,0,21","White":21,"CT":153,"Channel":[21,21,21,21,0]} class ShellyGen1WebTransport(_WebTransport):