Add topic for color change without switch on, add toggle on joystick click
This commit is contained in:
parent
e7ca999711
commit
5b7f4c29e1
1 changed files with 60 additions and 25 deletions
|
@ -32,28 +32,39 @@ class Display:
|
||||||
"availability_topic": self.availability_topic,
|
"availability_topic": self.availability_topic,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.main_light = Light("LED", self.uid, "led", self.sense, **options)
|
self.main_light = Light(
|
||||||
|
"LED", self.uid, "led", self.sense, mqttc=self.mqttc, **options
|
||||||
|
)
|
||||||
|
|
||||||
def publish_discovery(self):
|
def publish_discovery(self):
|
||||||
self.main_light.publish_discovery(self.mqttc)
|
self.main_light.publish_discovery()
|
||||||
|
|
||||||
def publish_online(self):
|
def publish_online(self):
|
||||||
self.subscribe()
|
self.subscribe()
|
||||||
self.mqttc.publish(self.availability_topic, "online", retain=True)
|
self.mqttc.publish(self.availability_topic, "online", retain=True)
|
||||||
|
|
||||||
def subscribe(self):
|
def subscribe(self):
|
||||||
self.main_light.subscribe(self.mqttc)
|
self.main_light.subscribe()
|
||||||
|
|
||||||
def on_message(self, *args, **kwargs):
|
def on_message(self, *args, **kwargs):
|
||||||
self.main_light.on_message(*args, **kwargs)
|
self.main_light.on_message(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class Light:
|
class Light:
|
||||||
def __init__(self, name, parent_uid, slug, sense, n=3, **kwargs):
|
_colors = {
|
||||||
|
"Bleu": [0, 0, 255],
|
||||||
|
"Blanc": [255, 255, 255],
|
||||||
|
"Rouge": [255, 0, 0],
|
||||||
|
"Verte": [0, 255, 0],
|
||||||
|
"Jaune": [255, 255, 0],
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, name, parent_uid, slug, sense, mqttc, n=6, **kwargs):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.parent_uid = parent_uid
|
self.parent_uid = parent_uid
|
||||||
self.slug = slug
|
self.slug = slug
|
||||||
self.sense = sense
|
self.sense = sense
|
||||||
|
self.mqttc = mqttc
|
||||||
self.options = kwargs
|
self.options = kwargs
|
||||||
|
|
||||||
self._switch = False
|
self._switch = False
|
||||||
|
@ -68,17 +79,18 @@ class Light:
|
||||||
|
|
||||||
self.sense.stick.direction_right = self.switch_screen
|
self.sense.stick.direction_right = self.switch_screen
|
||||||
self.sense.stick.direction_left = self.switch_screen_rev
|
self.sense.stick.direction_left = self.switch_screen_rev
|
||||||
|
self.sense.stick.direction_middle = self.toggle
|
||||||
|
|
||||||
def publish_discovery(self, mqttc):
|
def publish_discovery(self):
|
||||||
for i in range(self._n):
|
for i in range(self._n):
|
||||||
mqttc.publish(
|
self.mqttc.publish(
|
||||||
self.get_discovery_topic(i),
|
self.get_discovery_topic(i),
|
||||||
json.dumps(
|
json.dumps(
|
||||||
{
|
{
|
||||||
"command_topic": self.command_topic(i, "command"),
|
"command_topic": self.command_topic(i, "command"),
|
||||||
"effect_command_topic": self.command_topic(i, "effect"),
|
"effect_command_topic": self.command_topic(i, "effect"),
|
||||||
"effect_list": ["Low Light", "Normal"],
|
"effect_list": ["Low Light", "Normal"],
|
||||||
"effect_state_topie": self.state_topic,
|
"effect_state_topic": self.state_topic,
|
||||||
"effect_value_template": "{{ value_json.effect }}",
|
"effect_value_template": "{{ value_json.effect }}",
|
||||||
"icon": "mdi:dots-grid",
|
"icon": "mdi:dots-grid",
|
||||||
"name": f"{self.name} {i}",
|
"name": f"{self.name} {i}",
|
||||||
|
@ -95,14 +107,15 @@ class Light:
|
||||||
),
|
),
|
||||||
retain=True,
|
retain=True,
|
||||||
)
|
)
|
||||||
self.publish_state(mqttc)
|
self.publish_state()
|
||||||
|
|
||||||
def subscribe(self, mqttc):
|
def subscribe(self):
|
||||||
for i in range(self._n):
|
for i in range(self._n):
|
||||||
mqttc.subscribe(self.command_topic(i, "command"))
|
self.mqttc.subscribe(self.command_topic(i, "command"))
|
||||||
mqttc.subscribe(self.command_topic(i, "effect"))
|
self.mqttc.subscribe(self.command_topic(i, "effect"))
|
||||||
mqttc.subscribe(self.command_topic(i, "rgb"))
|
self.mqttc.subscribe(self.command_topic(i, "rgb"))
|
||||||
mqttc.subscribe(self.command_topic(i, "value"))
|
self.mqttc.subscribe(self.command_topic(i, "value"))
|
||||||
|
self.mqttc.subscribe(self.command_topic(i, "action_color"))
|
||||||
|
|
||||||
def on_message(self, client, userdata, message):
|
def on_message(self, client, userdata, message):
|
||||||
data = message.payload.decode()
|
data = message.payload.decode()
|
||||||
|
@ -115,19 +128,20 @@ class Light:
|
||||||
case [*rgb]:
|
case [*rgb]:
|
||||||
self.set_color(int(i), list(map(int, rgb)))
|
self.set_color(int(i), list(map(int, rgb)))
|
||||||
case [self.base_topic, i, "effect"]:
|
case [self.base_topic, i, "effect"]:
|
||||||
self.sense.low_light = data == "Low Light"
|
self.low_light = data == "Low Light"
|
||||||
case [self.base_topic, i, "value"]:
|
case [self.base_topic, i, "value"]:
|
||||||
self.set_value(int(i), data)
|
self.set_value(int(i), data)
|
||||||
|
case [self.base_topic, i, "action_color"]:
|
||||||
|
self.set_color(int(i), self._colors.get(data, [0, 0, 0]), False)
|
||||||
case _:
|
case _:
|
||||||
return
|
return
|
||||||
self.publish_state(client)
|
|
||||||
|
|
||||||
def publish_state(self, mqttc):
|
def publish_state(self):
|
||||||
mqttc.publish(
|
self.mqttc.publish(
|
||||||
self.state_topic,
|
self.state_topic,
|
||||||
json.dumps(
|
json.dumps(
|
||||||
{
|
{
|
||||||
"effect": "Low Light" if self.sense.low_light else "Normal",
|
"effect": "Low Light" if self.low_light else "Normal",
|
||||||
"rgb": self.rgb,
|
"rgb": self.rgb,
|
||||||
"state": self.state,
|
"state": self.state,
|
||||||
}
|
}
|
||||||
|
@ -164,6 +178,7 @@ class Light:
|
||||||
self.update_value()
|
self.update_value()
|
||||||
else:
|
else:
|
||||||
self.sense.clear()
|
self.sense.clear()
|
||||||
|
self.publish_state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
|
@ -173,12 +188,13 @@ class Light:
|
||||||
def color(self):
|
def color(self):
|
||||||
return self._color
|
return self._color
|
||||||
|
|
||||||
def set_color(self, i, value):
|
def set_color(self, i, value, switch=True):
|
||||||
self._color[i] = value
|
self._color[i] = value
|
||||||
if not self.switch:
|
if switch and not self.switch:
|
||||||
self.switch = True
|
self.switch = True
|
||||||
if i == self._i:
|
if i == self._i:
|
||||||
self.display_value()
|
self.display_value()
|
||||||
|
self.publish_state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def rgb(self):
|
def rgb(self):
|
||||||
|
@ -188,6 +204,15 @@ class Light:
|
||||||
def value(self):
|
def value(self):
|
||||||
return f"{self._pres[self._i]} {self._values[self._i]}"
|
return f"{self._pres[self._i]} {self._values[self._i]}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def low_light(self):
|
||||||
|
return self.sense.low_light
|
||||||
|
|
||||||
|
@low_light.setter
|
||||||
|
def low_light(self, value):
|
||||||
|
self.sense.low_light = value
|
||||||
|
self.publish_state()
|
||||||
|
|
||||||
def set_value(self, i, value):
|
def set_value(self, i, value):
|
||||||
match value.split():
|
match value.split():
|
||||||
case [val]:
|
case [val]:
|
||||||
|
@ -198,6 +223,7 @@ class Light:
|
||||||
self._values[i] = val
|
self._values[i] = val
|
||||||
if i == self._i:
|
if i == self._i:
|
||||||
self.display_value()
|
self.display_value()
|
||||||
|
self.publish_state()
|
||||||
|
|
||||||
def update_value(self):
|
def update_value(self):
|
||||||
if not self.switch:
|
if not self.switch:
|
||||||
|
@ -207,17 +233,16 @@ class Light:
|
||||||
self.display_value()
|
self.display_value()
|
||||||
return
|
return
|
||||||
|
|
||||||
self.timer.cancel()
|
|
||||||
|
|
||||||
pixels = self.to_pixels(self._pres[self._i])
|
pixels = self.to_pixels(self._pres[self._i])
|
||||||
self.sense.set_pixels(pixels)
|
self.sense.set_pixels(pixels)
|
||||||
|
|
||||||
self.timer = Timer(0.25, self.display_value, kwargs=dict(timer=True))
|
self.timer = Timer(0.5, self.display_value, kwargs=dict(timer=True))
|
||||||
self.timer.start()
|
self.timer.start()
|
||||||
|
|
||||||
def display_value(self, timer=False):
|
def display_value(self, timer=False):
|
||||||
if (not timer and self.timer.is_alive()) or not self.switch:
|
if (not timer and self.timer.is_alive()) or not self.switch:
|
||||||
return
|
return
|
||||||
|
self.timer.cancel()
|
||||||
|
|
||||||
pixels = self.to_pixels(self._values[self._i])
|
pixels = self.to_pixels(self._values[self._i])
|
||||||
self.sense.set_pixels(pixels)
|
self.sense.set_pixels(pixels)
|
||||||
|
@ -234,9 +259,19 @@ class Light:
|
||||||
def switch_screen(self, event):
|
def switch_screen(self, event):
|
||||||
if event.action == ACTION_RELEASED:
|
if event.action == ACTION_RELEASED:
|
||||||
self._i = (self._i + 1) % self._n
|
self._i = (self._i + 1) % self._n
|
||||||
self.update_value()
|
self.switch = True
|
||||||
|
|
||||||
def switch_screen_rev(self, event):
|
def switch_screen_rev(self, event):
|
||||||
if event.action == ACTION_RELEASED:
|
if event.action == ACTION_RELEASED:
|
||||||
self._i = (self._i - 1) % self._n
|
self._i = (self._i - 1) % self._n
|
||||||
self.update_value()
|
self.switch = True
|
||||||
|
|
||||||
|
def toggle(self, event):
|
||||||
|
if event.action == ACTION_RELEASED:
|
||||||
|
if not self.switch:
|
||||||
|
self.low_light = False
|
||||||
|
self.switch = True
|
||||||
|
elif self.low_light:
|
||||||
|
self.switch = False
|
||||||
|
else:
|
||||||
|
self.low_light = True
|
||||||
|
|
Loading…
Reference in a new issue