From 691b80c08a0f2765afd92b76eb5b98baa88c4c71 Mon Sep 17 00:00:00 2001 From: c727 Date: Thu, 21 Jun 2018 15:37:24 +0200 Subject: [PATCH] Lovelace: Add picture card (#1308) * Lovelace: Add scene card * Fix radius * Lint * Add acitvate text * Change on-click name * Requested changes * Remove navigate * 2 * Lint * Fix Cannot read property '_entityDisplay' of undefined --- src/panels/lovelace/hui-picture-card.js | 144 ++++++++++++++++++++++++ src/panels/lovelace/hui-view.js | 2 + 2 files changed, 146 insertions(+) create mode 100644 src/panels/lovelace/hui-picture-card.js diff --git a/src/panels/lovelace/hui-picture-card.js b/src/panels/lovelace/hui-picture-card.js new file mode 100644 index 0000000000..5eb1dbcb10 --- /dev/null +++ b/src/panels/lovelace/hui-picture-card.js @@ -0,0 +1,144 @@ +import { html } from '@polymer/polymer/lib/utils/html-tag.js'; +import { PolymerElement } from '@polymer/polymer/polymer-element.js'; + +import '../../components/ha-card.js'; + +import { STATES_ON } from '../../common/const.js'; +import computeDomain from '../../common/entity/compute_domain.js'; +import computeStateDisplay from '../../common/entity/compute_state_display.js'; +import computeStateName from '../../common/entity/compute_state_name.js'; + +import LocalizeMixin from '../../mixins/localize-mixin.js'; + +const DOMAINS_NO_STATE = ['scene', 'script', 'weblink']; + +/* + * @appliesMixin LocalizeMixin + */ +class HuiPictureCard extends LocalizeMixin(PolymerElement) { + static get template() { + return html` + + + + +
+
[[_computeTitle(config.entity, hass.states)]]
+
[[_computeState(config.entity, hass.states)]]
+
+ +
+ `; + } + + static get properties() { + return { + hass: Object, + config: { + type: Object, + observer: '_configChanged' + }, + _error: String + }; + } + + getCardSize() { + return 3; + } + + _configChanged(config) { + if (config && config.entity && config.image) { + this._error = null; + } else { + this._error = 'Error in card configuration.'; + } + } + + _computeClass(entityId, states) { + return DOMAINS_NO_STATE.includes(computeDomain(entityId)) || + STATES_ON.includes(states[entityId].state) ? '' : 'state-off'; + } + + _computeTitle(entityId, states) { + return entityId && entityId in states && computeStateName(states[entityId]); + } + + _computeState(entityId, states) { + const domain = computeDomain(entityId); + switch (domain) { + case 'scene': + return this.localize('ui.card.scene.activate'); + case 'script': + return this.localize('ui.card.script.execute'); + case 'weblink': + return 'Open'; + default: + return computeStateDisplay(this.localize, states[entityId]); + } + } + + _cardClicked() { + const entityId = this.config.entity; + const domain = computeDomain(entityId); + if (domain === 'weblink') { + window.open(this.hass.states[entityId].state); + } else { + const isOn = STATES_ON.includes(this.hass.states[entityId].state); + let service; + switch (domain) { + case 'lock': + service = isOn ? 'unlock' : 'lock'; + break; + case 'cover': + service = isOn ? 'close' : 'open'; + break; + default: + service = isOn ? 'turn_off' : 'turn_on'; + } + this.hass.callService(domain, service, { entity_id: entityId }); + } + } +} + +customElements.define('hui-picture-card', HuiPictureCard); diff --git a/src/panels/lovelace/hui-view.js b/src/panels/lovelace/hui-view.js index 09facc3659..9dad500ad2 100644 --- a/src/panels/lovelace/hui-view.js +++ b/src/panels/lovelace/hui-view.js @@ -7,6 +7,7 @@ import './hui-entity-filter-card.js'; import './hui-glance-card'; import './hui-history-graph-card.js'; import './hui-media-control-card.js'; +import './hui-picture-card.js'; import './hui-picture-glance-card'; import './hui-plant-status-card.js'; import './hui-weather-forecast-card'; @@ -21,6 +22,7 @@ const VALID_TYPES = [ 'glance', 'history-graph', 'media-control', + 'picture', 'picture-glance', 'plant-status', 'weather-forecast'