From efe195d6592d5c6cc64de71fc65f85522bc45dfa Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 10 Aug 2021 20:12:17 +0200 Subject: homebridge to homeassistant baby steps --- driver.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 driver.py (limited to 'driver.py') diff --git a/driver.py b/driver.py new file mode 100644 index 0000000..91b537e --- /dev/null +++ b/driver.py @@ -0,0 +1,58 @@ +#!/bin/python3 +from bluepy.btle import Peripheral, ADDR_TYPE_PUBLIC, BTLEDisconnectError +import threading +import time +import sys + +def makemsg(r, g, b, l=0): + return bytes([ + int(g > 0), g, + 0x00, 0x00, + int(b > 0), b, + int(r > 0), r, + int(l > 0), l, + ]) + +class BekenConnection: + def __init__(self, mac): + self.mac = mac + self.dev = None + self.messages = [] + + def keep_alive(self): + while True: + self.send(0x0001, bytes(10)) + time.sleep(10) + + def send(self, characteristic, message): + self.messages.append((characteristic, message)) + + def verify_connection(self): + while self.dev == None or self.dev.getState() == 'disc': + try: + self.dev = Peripheral(self.mac, ADDR_TYPE_PUBLIC) + except BTLEDisconnectError as e: + continue + + def message_handler(self): + self.verify_connection() + while True: + if len(self.messages) < 1: continue + message = self.messages.pop(0) + self.verify_connection() + self.dev.writeCharacteristic(message[0], bytearray(message[1])) + + def start_threads(self): + threading.Thread(target=self.message_handler).start() + threading.Thread(target=self.keep_alive).start() + +if __name__ == "__main__": + mac = sys.argv[1] + con = BekenConnection(mac) + con.start_threads() + def user_input(): + for line in sys.stdin: + r, g, b, l = [ int(x, 16) for x in [ line.strip()[i:i+2] for i in range(0, 8, 2) ] ] + con.send(0x002a, makemsg(r, g, b, l)) + threading.Thread(target=user_input).start() + -- cgit v1.2.3 From 63102954fa115629d3fef84c0e251aa1a8269201 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 13 Aug 2021 13:58:12 +0200 Subject: working (still buggy) --- driver.py | 7 +++++-- light.py | 32 +++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 7 deletions(-) (limited to 'driver.py') diff --git a/driver.py b/driver.py index 91b537e..5280d7d 100644 --- a/driver.py +++ b/driver.py @@ -4,6 +4,9 @@ import threading import time import sys +BEKEN_CHARACTERISTIC_NULL = 0x0001 +BEKEN_CHARACTERISTIC_LAMP = 0x002a + def makemsg(r, g, b, l=0): return bytes([ int(g > 0), g, @@ -21,7 +24,7 @@ class BekenConnection: def keep_alive(self): while True: - self.send(0x0001, bytes(10)) + self.send(BEKEN_CHARACTERISTIC_NULL, bytes(10)) time.sleep(10) def send(self, characteristic, message): @@ -53,6 +56,6 @@ if __name__ == "__main__": def user_input(): for line in sys.stdin: r, g, b, l = [ int(x, 16) for x in [ line.strip()[i:i+2] for i in range(0, 8, 2) ] ] - con.send(0x002a, makemsg(r, g, b, l)) + con.send(BEKEN_CHARACTERISTIC_LAMP, makemsg(r, g, b, l)) threading.Thread(target=user_input).start() diff --git a/light.py b/light.py index 6b5961f..f7cb243 100644 --- a/light.py +++ b/light.py @@ -1,7 +1,8 @@ import homeassistant.helpers.config_validation as cv import voluptuous as vol +from math import floor from homeassistant.const import CONF_MAC -from .driver import BekenConnection, makemsg +from .driver import BekenConnection, makemsg, BEKEN_CHARACTERISTIC_LAMP from homeassistant.components.light import ( LightEntity, @@ -33,7 +34,10 @@ class BekenLight(LightEntity): self._address = kwargs["address"] self._on = False self._brightness = 255 - self._rgbw = (255, 255, 255, 255) # r, g, b, w + self._rgb = (255, 255, 255) + self._w = 255 + self._connection = BekenConnection(self._address) + self._connection.start_threads() @property def color_mode(self): @@ -65,12 +69,30 @@ class BekenLight(LightEntity): @property def rgbw_color(self): - return self._rgbw + return self._rgb + (self._w,) def turn_on(self, **kwargs): self._on = True - self._brightness = kwargs.get(ATTR_BRIGHTNESS) - self._rgbw = kwargs.get(ATTR_RGBW_COLOR) + + brightness = kwargs.get(ATTR_BRIGHTNESS) + if brightness != None: + self._brightness = brightness + + rgbw = kwargs.get(ATTR_RGBW_COLOR) + if rgbw != None: + self._rgb = rgbw[0:3] + self._w = rgbw[3] + + self.update_beken_lamp() def turn_off(self, **kwargs): self._on = False + self.update_beken_lamp() + + def update_beken_lamp(self): + r = int(self._on) * self._rgb[0] + g = int(self._on) * self._rgb[1] + b = int(self._on) * self._rgb[2] + l = int(self._on) * self._w + self._connection.send(BEKEN_CHARACTERISTIC_LAMP, makemsg(r, g, b, l)) + -- cgit v1.2.3