sahilchaddha / homebridge-magichome-platform

Homebridge Plugin for MagicHome LED Strips with Preset Scene Automation Support (Cycle Color/Fade/Strobe)
MIT License
47 stars 12 forks source link

Debounce setToCurrentColor to avoid random/unexpected final color #34

Open cheung31 opened 3 years ago

cheung31 commented 3 years ago

Issue

Should address:

Overview

@sahilchaddha @sahilchaddha93

Add LightBulb#debouncedSetToCurrentColor method on 250ms delay as the light bulb service receives possibly 3 set events in succession (hue, saturation, brightness). LightBulb#setHue, LightBulb#setSaturation, and LightBulb#setBrightness now call LightBulb#debouncedSetToCurrentColor. Prior to this change, I found the service would clobber the device with flux_led.py command invocations. What we want is the 1-3 successive set* events to update LightBulb#color, and a single LightBulb#sendCommand invocation. Debouncing addresses this.

In my specific case, this fixed a scene that changed the light's brightness/color. I observed 3 flux_led.py commands with the same timestamp, and after looking through source code, realized it's probably 3-successive calls to setToCurrentColor caused by hue, saturation, brightness changes. We still want those 3 methods to mutate the LightBulb#color state. But want to debounce the actual LightBulb#sendCommand/LightBulb#executeCommand invocation as to not clobber the device, resulting in a random color depending on which of the 3 successive events wins the race condition.