From c375e5900b504d4eddc344684897659a700f0381 Mon Sep 17 00:00:00 2001 From: c727 Date: Tue, 24 Jul 2018 08:42:33 +0200 Subject: [PATCH] Fix toggle-able for group row elements (#1507) --- .../lovelace/common/create-row-element.js | 3 +- .../entity-rows/hui-group-entity-row.js | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/panels/lovelace/entity-rows/hui-group-entity-row.js diff --git a/src/panels/lovelace/common/create-row-element.js b/src/panels/lovelace/common/create-row-element.js index 32a8a65cc0..db8147e9e8 100644 --- a/src/panels/lovelace/common/create-row-element.js +++ b/src/panels/lovelace/common/create-row-element.js @@ -1,6 +1,7 @@ import fireEvent from '../../../common/dom/fire_event.js'; import '../entity-rows/hui-cover-entity-row.js'; +import '../entity-rows/hui-group-entity-row.js'; import '../entity-rows/hui-input-number-entity-row.js'; import '../entity-rows/hui-input-select-entity-row.js'; import '../entity-rows/hui-input-text-entity-row.js'; @@ -25,7 +26,7 @@ const DOMAIN_TO_ELEMENT_TYPE = { automation: 'toggle', cover: 'cover', fan: 'toggle', - group: 'toggle', + group: 'group', input_boolean: 'toggle', input_number: 'input-number', input_select: 'input-select', diff --git a/src/panels/lovelace/entity-rows/hui-group-entity-row.js b/src/panels/lovelace/entity-rows/hui-group-entity-row.js new file mode 100644 index 0000000000..23430a9edc --- /dev/null +++ b/src/panels/lovelace/entity-rows/hui-group-entity-row.js @@ -0,0 +1,76 @@ +import { html } from '@polymer/polymer/lib/utils/html-tag.js'; +import { PolymerElement } from '@polymer/polymer/polymer-element.js'; + +import '../components/hui-generic-entity-row.js'; +import '../../../components/entity/ha-entity-toggle.js'; + +import computeStateDisplay from '../../../common/entity/compute_state_display.js'; +import LocalizeMixin from '../../../mixins/localize-mixin.js'; + +const DOMAINS_TOGGLE = new Set([ + 'fan', + 'input_boolean', + 'light', + 'switch' +]); + +/* + * @appliesMixin LocalizeMixin + */ +class HuiGroupEntityRow extends LocalizeMixin(PolymerElement) { + static get template() { + return html` + + + + + `; + } + + static get properties() { + return { + hass: Object, + _config: Object, + _stateObj: { + type: Object, + computed: '_computeStateObj(hass.states, _config.entity)' + }, + _canToggle: { + type: Boolean, + computed: '_computeCanToggle(_stateObj.attributes.entity_id)' + } + }; + } + + setConfig(config) { + if (!config || !config.entity) { + throw new Error('Entity not configured.'); + } + this._config = config; + } + + _computeStateObj(states, entityId) { + return states && entityId in states ? states[entityId] : null; + } + + _computeCanToggle(entityIds) { + return entityIds.some(entityId => DOMAINS_TOGGLE.has(entityId.split('.', 1)[0])); + } + + _computeState(stateObj) { + return computeStateDisplay(this.localize, stateObj); + } +} +customElements.define('hui-group-entity-row', HuiGroupEntityRow);