From a604a2f527b3f485d347c75631d46c3390e4b2b4 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 2 Jan 2016 12:49:31 -0800 Subject: [PATCH] Debounce color picker calls. --- src/more-infos/more-info-light.js | 33 ++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/more-infos/more-info-light.js b/src/more-infos/more-info-light.js index bed13985b6..0801234a1e 100644 --- a/src/more-infos/more-info-light.js +++ b/src/more-infos/more-info-light.js @@ -8,6 +8,13 @@ require('../components/ha-color-picker'); const { serviceActions } = hass; const ATTRIBUTE_CLASSES = ['brightness', 'rgb_color', 'color_temp']; +function pickColor(entityId, color) { + serviceActions.callService('light', 'turn_on', { + entity_id: entityId, + rgb_color: [color.r, color.g, color.b], + }); +} + export default new Polymer({ is: 'more-info-light', @@ -67,13 +74,29 @@ export default new Polymer({ }); }, + /** + * Called when a new color has been picked. We will not respond to every + * color pick event but have a pause between requests. + */ colorPicked(ev) { - const color = ev.detail.rgb; + if (this.skipColorPicked) { + this.colorChanged = true; + return; + } - serviceActions.callService('light', 'turn_on', { - entity_id: this.stateObj.entityId, - rgb_color: [color.r, color.g, color.b], - }); + this.color = ev.detail.rgb; + + pickColor(this.stateObj.entityId, this.color); + + this.colorChanged = false; + this.skipColorPicked = true; + + this.colorDebounce = setTimeout(() => { + if (this.colorChanged) { + pickColor(this.stateObj.entityId, this.color); + } + this.skipColorPicked = false; + }, 500); }, });