Created
February 5, 2021 11:00
-
-
Save Toniob/113246128adaa49fbbe61061d6c8fc6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import paho.mqtt.client as mqtt | |
import struct | |
from functools import reduce | |
from pydantic import ValidationError | |
from pydantic.color import Color | |
class Techlife(): | |
msg = struct.Struct('<B6H3B') | |
def __init__(self, mqtt_server, mac_addr, username=None, password=None): | |
self.client = mqtt.Client() | |
if username: | |
self.client.username_pw_set(username, password) | |
self.client.connect(mqtt_server) | |
self.device_mac = mac_addr | |
self._color = Color('#fff') | |
self._brightness = 1 | |
self._on = False | |
self._animation_speed = 99 | |
@staticmethod | |
def calc_checksum(stream): | |
payload = bytearray(stream) | |
checksum = reduce(lambda x, y: x ^ y, payload[1:14]) | |
payload[14] = checksum & 0xFF | |
return payload | |
def send(self, cmd): | |
self.client.publish("dev_sub_" + self.device_mac, self.calc_checksum(cmd)) | |
def _update_leds(self): | |
rgb = self._color.as_rgb_tuple(alpha=False) | |
red, green, blue = map(lambda x: int(x * 10000 * self._brightness // 255), rgb) | |
brightness = int(0x64*self._brightness) | |
payload = self.msg.pack(0x28, red, green, blue, 0, 0, brightness, 0x0f, 0, 0x29) | |
self.send(payload) | |
@property | |
def brightness(self): | |
return self._brightness * 100 | |
@brightness.setter | |
def brightness(self, value): | |
self._brightness = value / 100 | |
self._update_leds() | |
@property | |
def color(self): | |
return self._color | |
@color.setter | |
def color(self, color): | |
try: | |
self._color = Color(color) | |
self._update_leds() | |
except ValidationError: | |
pass | |
@property | |
def speed(self): | |
return self._animation_speed | |
@speed.setter | |
def speed(self, speed): | |
self._animation_speed = speed | |
def animate(self, animate): | |
if animate: | |
speed = (100 - self.speed) * 255 // 100 | |
payload = self.msg.pack(0x66, 0x25, speed, 0x64, 0, 0, 0, 0, 0, 0x99) | |
self.send(payload) | |
else: | |
self._update_leds() | |
def is_on(self): | |
return self._on | |
def on(self): | |
payload = self.msg.pack(0xfa, 0x23, 0, 0, 0, 0, 0, 0, 0, 0xfb) | |
self.send(payload) | |
self._on = True | |
def off(self): | |
payload = self.msg.pack(0xfa, 0x24, 0, 0, 0, 0, 0, 0, 0, 0xfb) | |
self.send(payload) | |
self._on = False | |
def update(self): | |
payload = self.msg.pack(0xa9, 0xf0, 0, 0, 0, 0, 0, 0, 0, 0x9a) | |
self.send(payload) | |
self._on = True | |
if __name__ == '__main__': | |
import time | |
led = Techlife('ip or host', 'mac-addr', 'testuser', 'testpassword') | |
led.on() | |
time.sleep(3) | |
led.color = '#F02' | |
time.sleep(3) | |
led.brightness = 50 | |
time.sleep(3) | |
led.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much! This script helped a lot