Debounce color picker calls.

This commit is contained in:
Paulus Schoutsen 2016-01-02 12:49:31 -08:00
parent 4da3ecaad4
commit a604a2f527

View File

@ -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);
},
});