Fix toggle if state = unavailable (#1512)

This commit is contained in:
c727 2018-07-24 10:06:54 +02:00 committed by Paulus Schoutsen
parent c375e5900b
commit 00bee73bf1

View File

@ -4,14 +4,31 @@ import { PolymerElement } from '@polymer/polymer/polymer-element.js';
import '../components/hui-generic-entity-row.js'; import '../components/hui-generic-entity-row.js';
import '../../../components/entity/ha-entity-toggle.js'; import '../../../components/entity/ha-entity-toggle.js';
class HuiToggleEntityRow extends PolymerElement { import computeStateDisplay from '../../../common/entity/compute_state_display.js';
import LocalizeMixin from '../../../mixins/localize-mixin.js';
/*
* @appliesMixin LocalizeMixin
*/
class HuiToggleEntityRow extends LocalizeMixin(PolymerElement) {
static get template() { static get template() {
return html` return html`
<hui-generic-entity-row <hui-generic-entity-row
hass="[[hass]]" hass="[[hass]]"
config="[[_config]]" config="[[_config]]"
> >
<ha-entity-toggle hass="[[hass]]" state-obj="[[_computeStateObj(hass.states, _config.entity)]]"></ha-entity-toggle> <template is="dom-if" if="[[_canToggle]]">
<ha-entity-toggle
hass="[[hass]]"
state-obj="[[_stateObj]]"
></ha-entity-toggle>
</template>
<template is="dom-if" if="[[!_canToggle]]">
<div>
[[_computeState(_stateObj)]]
</div>
</template>
</hui-generic-entity-row> </hui-generic-entity-row>
`; `;
} }
@ -19,7 +36,15 @@ class HuiToggleEntityRow extends PolymerElement {
static get properties() { static get properties() {
return { return {
hass: Object, hass: Object,
_config: Object _config: Object,
_stateObj: {
type: Object,
computed: '_computeStateObj(hass.states, _config.entity)'
},
_canToggle: {
type: Boolean,
computed: '_computeCanToggle(_stateObj.state)'
}
}; };
} }
@ -27,6 +52,14 @@ class HuiToggleEntityRow extends PolymerElement {
return states && entityId in states ? states[entityId] : null; return states && entityId in states ? states[entityId] : null;
} }
_computeCanToggle(state) {
return state === 'on' || state === 'off';
}
_computeState(stateObj) {
return stateObj && computeStateDisplay(this.localize, stateObj);
}
setConfig(config) { setConfig(config) {
if (!config || !config.entity) { if (!config || !config.entity) {
throw new Error('Entity not configured.'); throw new Error('Entity not configured.');