mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-11 03:51:07 +00:00
More ES6->ES5 conversion
This commit is contained in:
@@ -1,104 +0,0 @@
|
||||
import Polymer from '../../polymer';
|
||||
|
||||
import OFF_STATES from '../../util/off-states';
|
||||
|
||||
export default new Polymer({
|
||||
is: 'ha-entity-toggle',
|
||||
|
||||
properties: {
|
||||
hass: {
|
||||
type: Object,
|
||||
},
|
||||
|
||||
stateObj: {
|
||||
type: Object,
|
||||
},
|
||||
|
||||
toggleChecked: {
|
||||
type: Boolean,
|
||||
value: false,
|
||||
},
|
||||
|
||||
isOn: {
|
||||
type: Boolean,
|
||||
computed: 'computeIsOn(stateObj)',
|
||||
observer: 'isOnChanged',
|
||||
},
|
||||
},
|
||||
|
||||
listeners: {
|
||||
tap: 'onTap',
|
||||
},
|
||||
|
||||
onTap(ev) {
|
||||
ev.stopPropagation();
|
||||
},
|
||||
|
||||
ready() {
|
||||
this.forceStateChange();
|
||||
},
|
||||
|
||||
toggleChanged(ev) {
|
||||
const newVal = ev.target.checked;
|
||||
|
||||
if (newVal && !this.isOn) {
|
||||
this.callService(true);
|
||||
} else if (!newVal && this.isOn) {
|
||||
this.callService(false);
|
||||
}
|
||||
},
|
||||
|
||||
isOnChanged(newVal) {
|
||||
this.toggleChecked = newVal;
|
||||
},
|
||||
|
||||
forceStateChange() {
|
||||
if (this.toggleChecked === this.isOn) {
|
||||
this.toggleChecked = !this.toggleChecked;
|
||||
}
|
||||
this.toggleChecked = this.isOn;
|
||||
},
|
||||
|
||||
turnOn() {
|
||||
this.callService(true);
|
||||
},
|
||||
|
||||
turnOff() {
|
||||
this.callService(false);
|
||||
},
|
||||
|
||||
computeIsOn(stateObj) {
|
||||
return stateObj && OFF_STATES.indexOf(stateObj.state) === -1;
|
||||
},
|
||||
|
||||
// We call updateToggle after a successful call to re-sync the toggle
|
||||
// with the state. It will be out of sync if our service call did not
|
||||
// result in the entity to be turned on. Since the state is not changing,
|
||||
// the resync is not called automatic.
|
||||
callService(turnOn) {
|
||||
let domain;
|
||||
let service;
|
||||
|
||||
if (this.stateObj.domain === 'lock') {
|
||||
domain = 'lock';
|
||||
service = turnOn ? 'lock' : 'unlock';
|
||||
} else if (this.stateObj.domain === 'garage_door') {
|
||||
domain = 'garage_door';
|
||||
service = turnOn ? 'open' : 'close';
|
||||
} else {
|
||||
domain = 'homeassistant';
|
||||
service = turnOn ? 'turn_on' : 'turn_off';
|
||||
}
|
||||
|
||||
const currentState = this.stateObj;
|
||||
this.hass.serviceActions.callService(domain, service,
|
||||
{ entity_id: this.stateObj.entityId })
|
||||
.then(() => setTimeout(() => {
|
||||
// If after 2 seconds we have not received a state update
|
||||
// reset the switch to it's original state.
|
||||
if (this.stateObj === currentState) {
|
||||
this.forceStateChange();
|
||||
}
|
||||
}, 2000));
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user