From bd125904603e7ca4a819de2a875cc3c5eca2c984 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Mon, 11 Nov 2024 18:06:48 +0100 Subject: [PATCH] Fix joystick power switch --- oin_ha/display/__init__.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/oin_ha/display/__init__.py b/oin_ha/display/__init__.py index 5104719..5283925 100644 --- a/oin_ha/display/__init__.py +++ b/oin_ha/display/__init__.py @@ -2,7 +2,7 @@ import json from threading import Timer import bdfparser -from sense_hat import ACTION_RELEASED, SenseHat +from sense_hat import ACTION_HELD, ACTION_RELEASED, SenseHat class Display: @@ -73,6 +73,7 @@ class Light: self._values = [""] * n self._n = n self._i = 0 + self._toggled = False self.font = bdfparser.Font("src/tom-thumb.bdf") self.timer = Timer(0, self.__init__) @@ -95,7 +96,7 @@ class Light: "icon": "mdi:dots-grid", "name": f"{self.name} {i}", "on_command_type": "brightness", - "rgb_command_topic": self.command_topic(i, "command"), + "rgb_command_topic": self.command_topic(i, "rgb"), "rgb_state_topic": self.state_topic, "rgb_value_template": "{{" + f"value_json.rgb[{i}]" + "}}", "retain": True, @@ -119,14 +120,13 @@ class Light: def on_message(self, client, userdata, message): data = message.payload.decode() + print(message.topic, data) match message.topic.rsplit("/", maxsplit=2): case [self.base_topic, i, "command"]: - match data.split(","): - case ["OFF"]: - self.switch = False - case [*rgb]: - self.set_color(int(i), list(map(int, rgb))) + self.switch = False + case [self.base_topic, i, "rgb"]: + self.set_color(int(i), list(map(int, data.split(",")))) case [self.base_topic, i, "effect"]: self.low_light = data == "Low Light" case [self.base_topic, i, "value"]: @@ -251,7 +251,7 @@ class Light: if text: return [ self.color[self._i] if x else [0, 0, 0] - for x in self.font.draw(text).crop(8, 8, yoff=-2).todata(3) + for x in self.font.draw(text).crop(8, 8).todata(3) ] return [self.color[self._i]] * 64 @@ -267,11 +267,12 @@ class Light: self.switch = True def toggle(self, event): + if event.action == ACTION_HELD: + if not self._toggled: + self.low_light = not self.low_light + self._toggled = True if event.action == ACTION_RELEASED: - if not self.switch: - self.low_light = False - self.switch = True - elif self.low_light: - self.switch = False + if self._toggled: + self._toggled = False else: - self.low_light = True + self.switch = not self.switch