From ef23133a23c71f3919f479eecd73c17a00fbb82f Mon Sep 17 00:00:00 2001 From: Mirco Bauer Date: Sun, 26 May 2024 13:48:41 +0800 Subject: [PATCH] flash_helios_light.py: restore previous light color after flashing The default light color (kind of white) is used to restore the previous light color. This has a side-effect that changes to the lights by users of the user interface will be lost. Instead we read the previous color state and restore it once the flash is completed. --- .../on-message-received/flash_helios_light.py | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/smuxi_hooks/on-message-received/flash_helios_light.py b/smuxi_hooks/on-message-received/flash_helios_light.py index 0674b7f..ddaf659 100755 --- a/smuxi_hooks/on-message-received/flash_helios_light.py +++ b/smuxi_hooks/on-message-received/flash_helios_light.py @@ -3,7 +3,7 @@ # A Smuxi hook script. This hooks flashes the ceiling light when a new message # on the #dimsumlabs channel was received. # -# Copyright (C) 2021 Mirco Bauer +# Copyright (C) 2021, 2024 Mirco Bauer # Copyright (C) 2021 Neil Pahl # Copyright (C) 2021 Felix E. Klee @@ -12,15 +12,34 @@ import os import sys import time +import re from os.path import exists -DND_TIME_FILENAME="/home/pi/dsl-wall/smuxi_scripting_fun/dnd_expire_epoch.txt" +DND_TIME_FILENAME="$HOME/dsl-wall/smuxi_scripting_fun/dnd_expire_epoch.txt" if exists(DND_TIME_FILENAME) == True: - dnd_file = open(DND_TIME_FILENAME, "r") + path = os.path.expandvars(DND_TIME_FILENAME); + dnd_file = open(path, "r") dnd_on = int(dnd_file.read().strip()) > time.time() else: dnd_on = False +def get_ceiling_color(light_host): + request_url = light_host + 'rgb' + req = request.Request(request_url) + # timeout after 1 second as the light might be turned off + res = request.urlopen(req, data=None, timeout=1) + body = res.read().decode("ascii") + print("body: '{}'".format(body)) + match = re.search("(?P[0-9]+)\n" + + "(?P[0-9]+)\n" + + "(?P[0-9]+).*", + body) + if not match: + return None + return ( match.group('red'), + match.group('green'), + match.group('blue') ) + def change_ceiling_color(light_host, red, green, blue): color_url = 'rgb.lua?r={}&g={}&b={}'.format(red, green, blue) request_url = light_host + color_url @@ -35,12 +54,20 @@ def change_ceiling_color(light_host, red, green, blue): if os.environ['SMUXI_MSG_TYPE'] != "Normal" or os.environ['SMUXI_CHAT_ID'] != "#dimsumlabs" or dnd_on: sys.exit(0) -helios_service = 'http://helios/' -#helios2_service = 'http://helios2/' +helios_service = 'http://helios.in.dimsumlabs.com/' +#helios2_service = 'http://helios2.in.dimsumlabs.com/' # HACK: disable helios2 as it is broken and needs repair. The HTTP/REST call # just hang forever... helios2_service = None +# WTF: 0 is brightest value, lulz +white = (0, 0, 0) + +current_color = get_ceiling_color(helios_service) +if not current_color: + print('could not determine current color, assuming white...') + current_color = white + # color value range 0 to 1024 light_pink = (7, 751, 130) light_green = (783, 7, 652) @@ -51,9 +78,7 @@ def change_ceiling_color(light_host, red, green, blue): # sleep for 1 second to give time for the color transition to complete sleep(1) -# WTF: 0 is brightest value, lulz -white = (0, 0, 0) -change_ceiling_color(helios_service, *white) +# restore to previous color +change_ceiling_color(helios_service, *current_color) if helios2_service: - change_ceiling_color(helios2_service, *white) - + change_ceiling_color(helios2_service, *current_color)