Allow toggling groups

This commit is contained in:
Paulus Schoutsen
2015-09-23 23:17:23 -07:00
parent 1ba7060719
commit f2def04044
8 changed files with 111 additions and 69 deletions

View File

@@ -0,0 +1,10 @@
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../bower_components/paper-toggle-button/paper-toggle-button.html">
<dom-module id="ha-entity-toggle">
<template>
<paper-toggle-button class='self-center'
checked="[[toggleChecked]]"
on-change="toggleChanged"></paper-toggle-button>
</template>
</dom-module>

View File

@@ -0,0 +1,63 @@
import { serviceActions } from '../../util/home-assistant-js-instance';
import Polymer from '../../polymer';
export default new Polymer({
is: 'ha-entity-toggle',
properties: {
stateObj: {
type: Object,
observer: 'stateObjChanged',
},
toggleChecked: {
type: Boolean,
value: false,
},
},
ready() {
this.forceStateChange();
},
toggleChanged(ev) {
const newVal = ev.target.checked;
if (newVal && this.stateObj.state === 'off') {
this.turn_on();
} else if (!newVal && this.stateObj.state !== 'off') {
this.turn_off();
}
},
stateObjChanged(newVal) {
if (newVal) {
this.updateToggle(newVal);
}
},
updateToggle(stateObj) {
this.toggleChecked = stateObj && stateObj.state !== 'off';
},
forceStateChange() {
this.updateToggle(this.stateObj);
},
turn_on() {
// 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.
serviceActions.callTurnOn(this.stateObj.entityId).then(() => this.forceStateChange());
},
turn_off() {
// 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.
serviceActions.callTurnOff(this.stateObj.entityId).then(() => this.forceStateChange());
},
});