From 07d76739b4ae3f4198b6842dd334b237ba4d4530 Mon Sep 17 00:00:00 2001 From: c727 Date: Thu, 28 Jun 2018 21:53:56 +0200 Subject: [PATCH] Love: cleanup (#1358) * Love: cleanup * Lint * Fix CopyPaste * Change config key names for history * Restore old card size * Add function description * Use createErrorCardConfig --- .../lovelace/cards/hui-camera-preview-card.js | 58 +++++++------------ src/panels/lovelace/cards/hui-glance-card.js | 20 +------ .../lovelace/cards/hui-history-graph-card.js | 30 ++++------ src/panels/lovelace/cards/hui-iframe-card.js | 29 ++-------- .../lovelace/cards/hui-media-control-card.js | 58 +++++++------------ .../cards/hui-picture-elements-card.js | 2 +- .../lovelace/cards/hui-plant-status-card.js | 58 +++++++------------ .../cards/hui-weather-forecast-card.js | 58 +++++++------------ .../lovelace/common/validate-entity-config.js | 16 +++++ 9 files changed, 120 insertions(+), 209 deletions(-) create mode 100644 src/panels/lovelace/common/validate-entity-config.js diff --git a/src/panels/lovelace/cards/hui-camera-preview-card.js b/src/panels/lovelace/cards/hui-camera-preview-card.js index 501ee74be6..75c0908b62 100644 --- a/src/panels/lovelace/cards/hui-camera-preview-card.js +++ b/src/panels/lovelace/cards/hui-camera-preview-card.js @@ -1,20 +1,21 @@ import { PolymerElement } from '@polymer/polymer/polymer-element.js'; -import computeDomain from '../../../common/entity/compute_domain.js'; - import '../../../cards/ha-camera-card.js'; -import './hui-error-card.js'; + +import createCardElement from '../common/create-card-element.js'; +import createErrorCardConfig from '../common/create-error-card-config.js'; +import validateEntityConfig from '../common/validate-entity-config.js'; class HuiCameraPreviewCard extends PolymerElement { static get properties() { return { hass: { type: Object, - observer: '_hassChanged', + observer: '_hassChanged' }, config: { type: Object, - observer: '_configChanged', + observer: '_configChanged' } }; } @@ -25,50 +26,35 @@ class HuiCameraPreviewCard extends PolymerElement { _configChanged(config) { this._entityId = null; + if (this.lastChild) { this.removeChild(this.lastChild); } - const entityId = config && config.entity; - if (entityId && !(entityId in this.hass.states)) { + + if (!validateEntityConfig(config, 'camera')) { + const error = 'Error in card configuration.'; + const element = createCardElement(createErrorCardConfig(error, config)); + this.appendChild(element); return; } - let error = null; - let cardConfig; - let tag; - - if (entityId) { - if (computeDomain(entityId) === 'camera') { - this._entityId = entityId; - tag = 'ha-camera-card'; - cardConfig = config; - } else { - error = 'Entity domain must be "camera"'; - } - } else { - error = 'Entity not defined in card config'; + const entityId = config.entity; + if (!(entityId in this.hass.states)) { + return; } - if (error) { - tag = 'hui-error-card'; - cardConfig = { error }; - } - const element = document.createElement(tag); - - if (!error) { - element.stateObj = this.hass.states[entityId]; - element.hass = this.hass; - } - - element.config = cardConfig; + const element = document.createElement('ha-camera-card'); + element.stateObj = this.hass.states[entityId]; + element.hass = this.hass; this.appendChild(element); + this._entityId = entityId; } _hassChanged(hass) { - if (this.lastChild && this._entityId && this._entityId in hass.states) { + const entityId = this._entityId; + if (entityId && entityId in hass.states) { const element = this.lastChild; - const stateObj = hass.states[this._entityId]; - element.stateObj = stateObj; + element.stateObj = hass.states[entityId]; element.hass = hass; } else { this._configChanged(this.config); diff --git a/src/panels/lovelace/cards/hui-glance-card.js b/src/panels/lovelace/cards/hui-glance-card.js index 8e2c121561..95c7cc8186 100644 --- a/src/panels/lovelace/cards/hui-glance-card.js +++ b/src/panels/lovelace/cards/hui-glance-card.js @@ -21,17 +21,6 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) { ha-card { padding: 16px; } - .header { - @apply --paper-font-headline; - /* overwriting line-height +8 because entity-toggle can be 40px height, - compensating this with reduced padding */ - line-height: 40px; - color: var(--primary-text-color); - padding: 4px 0 12px; - } - .header .name { - @apply --paper-font-common-nowrap; - } .entities { padding: 4px 0; display: flex; @@ -62,10 +51,7 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) { } - -
-
[[_computeTitle(config)]]
-
+