changeset 15:61d4ccecfed8

rough refactor
author drewp@bigasterisk.com
date Sun, 28 Jan 2024 21:18:01 -0800
parents e3dbd04dab96
children ab80e8826441
files color_convert.py light.py protocols.py
diffstat 3 files changed, 58 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/color_convert.py	Sun Jan 28 21:18:01 2024 -0800
@@ -0,0 +1,25 @@
+import logging
+from dataclasses import dataclass
+
+from color import Color
+
+log = logging.getLogger('conv')
+
+
+@dataclass(frozen=True)
+class DeviceColor:
+    """neutral representation of the adjusted color that we send to a device"""
+    r: float = 0
+    g: float = 0
+    b: float = 0
+    w: float = 0
+    x: float = 0
+    y: float = 0
+    brightness: float = 0
+
+    def summary(self) -> dict:
+        return dict([(k, round(v, 3)) for k, v in self.__dict__.items() if v > 0])
+
+# fix this to send what z2m likes
+def zbConv(c: Color) -> DeviceColor:
+    return DeviceColor(r=c.r, g=c.g, b=c.b, brightness=max(c.r, c.g, c.b))
--- a/light.py	Sun Jan 28 20:49:42 2024 -0800
+++ b/light.py	Sun Jan 28 21:18:01 2024 -0800
@@ -10,52 +10,6 @@
 log = logging.getLogger('light')
 
 
-@dataclass(frozen=True)
-class DeviceColor:
-    """neutral representation of the adjusted color that we send to a device"""
-    r: float = 0
-    g: float = 0
-    b: float = 0
-    w: float = 0
-    x: float = 0
-    y: float = 0
-
-    def summary(self) -> dict:
-        return dict([(k, round(v, 3)) for k, v in self.__dict__.items() if v > 0])
-
-
-class Transport:
-
-    def linked(self):
-        return {'label': str(self)}
-
-    async def send(self, dc: DeviceColor):
-        raise TypeError
-
-
-def zigbeeHexMessage(color: DeviceColor, bw=False) -> dict:
-    bright = max(color.r, color.g, color.b)
-    msg: dict = {"transition": 0, "brightness": int(255 * bright)}
-    if not bw:
-        c = "#%02x%02x%02x" % (int(color.r * 255), int(color.g * 255), int(color.b * 255))
-        msg["color"] = {"hex": c}
-    return msg
-
-
-class ZigbeeTransport(Transport):
-
-    def __init__(self, mqtt: MqttIo, name: str, ieee: str):
-        self.mqtt = mqtt
-        self.name = name
-        self.ieee = ieee
-
-    def linked(self):
-        return {'url': f'https://bigasterisk.com/zigbee/console/#/device/{self.ieee}/info', 'label': 'do-bar'}
-
-    async def send(self, dc: DeviceColor):
-        await self.mqtt.publish(f'zigbee/{self.name}/set', json.dumps(zigbeeHexMessage(dc, bw=False)))
-
-
 @dataclass
 class Light:
     name: str
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/protocols.py	Sun Jan 28 21:18:01 2024 -0800
@@ -0,0 +1,33 @@
+import json
+from color_convert import DeviceColor
+from mqtt_io import MqttIo
+
+
+class Transport:
+
+    def linked(self):
+        return {'label': str(self)}
+
+    async def send(self, dc: DeviceColor):
+        raise TypeError
+
+
+def zigbeeHexMessage(dc: DeviceColor) -> dict:
+    msg: dict = {"transition": 0, "brightness": int(255 * dc.brightness)}
+    c = "#%02x%02x%02x" % (int(dc.r * 255), int(dc.g * 255), int(dc.b * 255))
+    msg["color"] = {"hex": c}
+    return msg
+
+
+class ZigbeeTransport(Transport):
+
+    def __init__(self, mqtt: MqttIo, name: str, ieee: str):
+        self.mqtt = mqtt
+        self.name = name
+        self.ieee = ieee
+
+    def linked(self):
+        return {'url': f'https://bigasterisk.com/zigbee/console/#/device/{self.ieee}/info', 'label': 'do-bar'}
+
+    async def send(self, dc: DeviceColor):
+        await self.mqtt.publish(f'zigbee/{self.name}/set', json.dumps(zigbeeHexMessage(dc)))