Make sure toggle is always up-to date (#783)

* Make sure toggle is always upto date

* Move logic to observer
This commit is contained in:
Andrey 2018-01-10 02:44:53 +02:00 committed by Paulus Schoutsen
parent c06be58a33
commit 48b0857edb

View File

@ -41,12 +41,10 @@ class HaEntityToggle extends Polymer.Element {
static get properties() { static get properties() {
return { return {
hass: { hass: Object,
type: Object,
},
stateObj: { stateObj: {
type: Object, type: Object,
observer: 'stateObjObserver'
}, },
toggleChecked: { toggleChecked: {
@ -73,7 +71,7 @@ class HaEntityToggle extends Polymer.Element {
} }
toggleChanged(ev) { toggleChanged(ev) {
var newVal = ev.target.checked; const newVal = ev.target.checked;
if (newVal && !this.isOn) { if (newVal && !this.isOn) {
this.callService(true); this.callService(true);
@ -102,7 +100,15 @@ class HaEntityToggle extends Polymer.Element {
} }
computeIsOn(stateObj) { computeIsOn(stateObj) {
return stateObj && window.hassUtil.OFF_STATES.indexOf(stateObj.state) === -1; return stateObj && !window.hassUtil.OFF_STATES.includes(stateObj.state);
}
stateObjObserver(newVal, oldVal) {
if (!oldVal || !newVal) return;
if (this.computeIsOn(newVal) === this.computeIsOn(oldVal)) {
// stateObj changed but isOn is the same. Make sure toggle is in the right position.
this.forceStateChange();
}
} }
// We call updateToggle after a successful call to re-sync the toggle // We call updateToggle after a successful call to re-sync the toggle
@ -110,10 +116,9 @@ class HaEntityToggle extends Polymer.Element {
// result in the entity to be turned on. Since the state is not changing, // result in the entity to be turned on. Since the state is not changing,
// the resync is not called automatic. // the resync is not called automatic.
callService(turnOn) { callService(turnOn) {
var stateDomain = window.hassUtil.computeDomain(this.stateObj); const stateDomain = window.hassUtil.computeDomain(this.stateObj);
var serviceDomain; let serviceDomain;
var service; let service;
var currentState;
if (stateDomain === 'lock') { if (stateDomain === 'lock') {
serviceDomain = 'lock'; serviceDomain = 'lock';
@ -126,20 +131,20 @@ class HaEntityToggle extends Polymer.Element {
service = turnOn ? 'turn_on' : 'turn_off'; service = turnOn ? 'turn_on' : 'turn_off';
} }
currentState = this.stateObj; const currentState = this.stateObj;
this.hass.callService( this.hass.callService(
serviceDomain, service, serviceDomain, service,
{ entity_id: this.stateObj.entity_id } { entity_id: this.stateObj.entity_id }
) )
.then(function () { .then(() => {
setTimeout(function () { setTimeout(() => {
// If after 2 seconds we have not received a state update // If after 2 seconds we have not received a state update
// reset the switch to it's original state. // reset the switch to it's original state.
if (this.stateObj === currentState) { if (this.stateObj === currentState) {
this.forceStateChange(); this.forceStateChange();
} }
}.bind(this), 2000); }, 2000);
}.bind(this)); });
} }
} }