Fix bug effecting Scenes

This commit is contained in:
imonlinux
2025-10-12 20:24:14 -07:00
committed by GitHub
parent c067f6de99
commit 24c0d30540
+17 -12
View File
@@ -60,14 +60,15 @@ class LedController(EventHandler):
coro = getattr(self, action_method_name)(*args) coro = getattr(self, action_method_name)(*args)
self.current_task = asyncio.run_coroutine_threadsafe(coro, self.loop) self.current_task = asyncio.run_coroutine_threadsafe(coro, self.loop)
def _apply_state_effect(self, state_name: str): def _apply_state_effect(self, state_name: str, publish_state: bool = True):
config = self.configs.get(state_name, self.configs["idle"]) config = self.configs.get(state_name, self.configs["idle"])
_LOGGER.debug(f"Applying effect for state '{state_name}': {config['effect']}") _LOGGER.debug(f"Applying effect for state '{state_name}': {config['effect']}")
self.leds.brightness = config["brightness"] self.leds.brightness = config["brightness"]
self.run_action(config["effect"], config["color"]) self.run_action(config["effect"], config["color"])
config["state_name"] = state_name if publish_state:
self.state.event_bus.publish("publish_state_to_mqtt", config) config["state_name"] = state_name
self.state.event_bus.publish("publish_state_to_mqtt", config)
# --- Animations --- # --- Animations ---
async def startup_sequence(self): await self.blink(_GREEN, 1) async def startup_sequence(self): await self.blink(_GREEN, 1)
@@ -128,6 +129,7 @@ class LedController(EventHandler):
def _update_config(self, state_name: str, data: dict, apply: bool): def _update_config(self, state_name: str, data: dict, apply: bool):
config = self.configs[state_name] config = self.configs[state_name]
changed = False changed = False
is_retained = data.get("retained", False)
new_effect = data.get("effect") new_effect = data.get("effect")
if new_effect is not None and config["effect"] != new_effect: if new_effect is not None and config["effect"] != new_effect:
@@ -144,27 +146,30 @@ class LedController(EventHandler):
new_brightness = data.get("brightness") new_brightness = data.get("brightness")
if new_brightness is not None: if new_brightness is not None:
new_brightness_float = new_brightness / 255.0 new_brightness_float = new_brightness / 255.0
# --- THIS IS THE FIX ---
# Compare floats with a tolerance, not for exact equality
if abs(config["brightness"] - new_brightness_float) > 0.001: if abs(config["brightness"] - new_brightness_float) > 0.001:
config["brightness"] = new_brightness_float config["brightness"] = new_brightness_float
changed = True changed = True
# --- THIS IS THE FIX ---
if not changed: if not changed:
# If it's a retained message for the idle state on startup, we still need to apply it once.
# But since nothing changed, we can assume it's already correct.
if is_retained and state_name == "idle":
self._apply_state_effect("idle", publish_state=False) # Apply visuals, but DO NOT publish back
return return
is_retained = data.get("retained", False) # If we get here, the configuration has changed.
if is_retained: if is_retained:
if state_name == "idle": if state_name == "idle":
self._apply_state_effect("idle") self._apply_state_effect("idle", publish_state=False) # Apply visuals, but DO NOT publish back
return # For other retained messages, we just update config silently.
elif apply:
if apply: self._apply_state_effect(state_name) # Apply visuals AND publish back
self._apply_state_effect(state_name)
else: else:
# Config was updated for a non-active state, just publish it back to confirm.
config["state_name"] = state_name config["state_name"] = state_name
self.state.event_bus.publish("publish_state_to_mqtt", config) self.state.event_bus.publish("publish_state_to_mqtt", config)
@subscribe @subscribe
def set_idle_effect(self, data: dict): self._update_config("idle", data, True) def set_idle_effect(self, data: dict): self._update_config("idle", data, True)