mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-09 10:59:50 +00:00
Allow toggling groups
This commit is contained in:
10
src/components/entity/ha-entity-toggle.html
Normal file
10
src/components/entity/ha-entity-toggle.html
Normal 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>
|
||||
63
src/components/entity/ha-entity-toggle.js
Normal file
63
src/components/entity/ha-entity-toggle.js
Normal 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());
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user