mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-15 21:36:36 +00:00
Love: cleanup (#1358)
* Love: cleanup * Lint * Fix CopyPaste * Change config key names for history * Restore old card size * Add function description * Use createErrorCardConfig
This commit is contained in:
parent
d40dea6d3b
commit
07d76739b4
@ -1,20 +1,21 @@
|
|||||||
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
||||||
|
|
||||||
import computeDomain from '../../../common/entity/compute_domain.js';
|
|
||||||
|
|
||||||
import '../../../cards/ha-camera-card.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 {
|
class HuiCameraPreviewCard extends PolymerElement {
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
hass: {
|
hass: {
|
||||||
type: Object,
|
type: Object,
|
||||||
observer: '_hassChanged',
|
observer: '_hassChanged'
|
||||||
},
|
},
|
||||||
config: {
|
config: {
|
||||||
type: Object,
|
type: Object,
|
||||||
observer: '_configChanged',
|
observer: '_configChanged'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -25,50 +26,35 @@ class HuiCameraPreviewCard extends PolymerElement {
|
|||||||
|
|
||||||
_configChanged(config) {
|
_configChanged(config) {
|
||||||
this._entityId = null;
|
this._entityId = null;
|
||||||
|
|
||||||
if (this.lastChild) {
|
if (this.lastChild) {
|
||||||
this.removeChild(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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let error = null;
|
const entityId = config.entity;
|
||||||
let cardConfig;
|
if (!(entityId in this.hass.states)) {
|
||||||
let tag;
|
return;
|
||||||
|
|
||||||
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';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
const element = document.createElement('ha-camera-card');
|
||||||
tag = 'hui-error-card';
|
element.stateObj = this.hass.states[entityId];
|
||||||
cardConfig = { error };
|
element.hass = this.hass;
|
||||||
}
|
|
||||||
const element = document.createElement(tag);
|
|
||||||
|
|
||||||
if (!error) {
|
|
||||||
element.stateObj = this.hass.states[entityId];
|
|
||||||
element.hass = this.hass;
|
|
||||||
}
|
|
||||||
|
|
||||||
element.config = cardConfig;
|
|
||||||
this.appendChild(element);
|
this.appendChild(element);
|
||||||
|
this._entityId = entityId;
|
||||||
}
|
}
|
||||||
|
|
||||||
_hassChanged(hass) {
|
_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 element = this.lastChild;
|
||||||
const stateObj = hass.states[this._entityId];
|
element.stateObj = hass.states[entityId];
|
||||||
element.stateObj = stateObj;
|
|
||||||
element.hass = hass;
|
element.hass = hass;
|
||||||
} else {
|
} else {
|
||||||
this._configChanged(this.config);
|
this._configChanged(this.config);
|
||||||
|
@ -21,17 +21,6 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
|
|||||||
ha-card {
|
ha-card {
|
||||||
padding: 16px;
|
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 {
|
.entities {
|
||||||
padding: 4px 0;
|
padding: 4px 0;
|
||||||
display: flex;
|
display: flex;
|
||||||
@ -62,10 +51,7 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
|
|||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<ha-card>
|
<ha-card header="[[config.title]]">
|
||||||
<div class="header">
|
|
||||||
<div class="name">[[_computeTitle(config)]]</div>
|
|
||||||
</div>
|
|
||||||
<div class="entities">
|
<div class="entities">
|
||||||
<template is="dom-repeat" items="[[_entities]]">
|
<template is="dom-repeat" items="[[_entities]]">
|
||||||
<template is="dom-if" if="[[_showEntity(item, hass.states)]]">
|
<template is="dom-if" if="[[_showEntity(item, hass.states)]]">
|
||||||
@ -100,10 +86,6 @@ class HuiGlanceCard extends LocalizeMixin(EventsMixin(PolymerElement)) {
|
|||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
_computeTitle(config) {
|
|
||||||
return config.title;
|
|
||||||
}
|
|
||||||
|
|
||||||
_computeEntities(config) {
|
_computeEntities(config) {
|
||||||
if (config && config.entities && Array.isArray(config.entities)) {
|
if (config && config.entities && Array.isArray(config.entities)) {
|
||||||
this._error = null;
|
this._error = null;
|
||||||
|
@ -5,6 +5,8 @@ import './hui-error-card.js';
|
|||||||
import '../../../components/state-history-charts.js';
|
import '../../../components/state-history-charts.js';
|
||||||
import '../../../data/ha-state-history-data.js';
|
import '../../../data/ha-state-history-data.js';
|
||||||
|
|
||||||
|
import createErrorCardConfig from '../common/create-error-card-config.js';
|
||||||
|
|
||||||
class HuiHistoryGraphCard extends PolymerElement {
|
class HuiHistoryGraphCard extends PolymerElement {
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
@ -13,7 +15,8 @@ class HuiHistoryGraphCard extends PolymerElement {
|
|||||||
type: Object,
|
type: Object,
|
||||||
observer: '_configChanged',
|
observer: '_configChanged',
|
||||||
},
|
},
|
||||||
_error: String,
|
_error: Object,
|
||||||
|
|
||||||
stateHistory: Object,
|
stateHistory: Object,
|
||||||
stateHistoryLoading: Boolean,
|
stateHistoryLoading: Boolean,
|
||||||
cacheConfig: {
|
cacheConfig: {
|
||||||
@ -37,24 +40,10 @@ class HuiHistoryGraphCard extends PolymerElement {
|
|||||||
ha-card {
|
ha-card {
|
||||||
padding: 16px;
|
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;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<template is="dom-if" if="[[!_error]]">
|
<template is="dom-if" if="[[!_error]]">
|
||||||
<ha-card>
|
<ha-card header=[[config.title]]>
|
||||||
<div class='header'>
|
|
||||||
<div class="name">[[config.title]]</div>
|
|
||||||
</div>
|
|
||||||
<ha-state-history-data
|
<ha-state-history-data
|
||||||
hass="[[hass]]"
|
hass="[[hass]]"
|
||||||
filter-type="recent-entity"
|
filter-type="recent-entity"
|
||||||
@ -74,7 +63,7 @@ class HuiHistoryGraphCard extends PolymerElement {
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<template is="dom-if" if="[[_error]]">
|
<template is="dom-if" if="[[_error]]">
|
||||||
<hui-error-card error="[[_error]]" config="[[config]]"></hui-error-card>
|
<hui-error-card config="[[_error]]"></hui-error-card>
|
||||||
</template>
|
</template>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
@ -84,12 +73,13 @@ class HuiHistoryGraphCard extends PolymerElement {
|
|||||||
this._error = null;
|
this._error = null;
|
||||||
|
|
||||||
this.cacheConfig = {
|
this.cacheConfig = {
|
||||||
refresh: config.refresh || 0,
|
|
||||||
cacheKey: config.entities,
|
cacheKey: config.entities,
|
||||||
hoursToShow: config.hours || 24
|
hoursToShow: config.hours_to_show || 24,
|
||||||
|
refresh: config.refresh_interval || 0
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
this._error = 'No entities configured.';
|
const error = 'Error in card configuration.';
|
||||||
|
this._error = createErrorCardConfig(error, config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,18 +18,7 @@ class HuiIframeCard extends PolymerElement {
|
|||||||
line-height: 0;
|
line-height: 0;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
.header {
|
#root {
|
||||||
@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: 20px 16px 12px 16px;
|
|
||||||
}
|
|
||||||
.header .name {
|
|
||||||
@apply --paper-font-common-nowrap;
|
|
||||||
}
|
|
||||||
.wrapper {
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
@ -42,26 +31,16 @@ class HuiIframeCard extends PolymerElement {
|
|||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<ha-card>
|
<ha-card header="[[config.title]]">
|
||||||
<template is="dom-if" if="[[_computeTitle(config)]]">
|
<div id="root">
|
||||||
<div class="header">
|
|
||||||
<div class="name">[[_computeTitle(config)]]</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
<div class="wrapper">
|
|
||||||
<iframe src="[[config.url]]"></iframe>
|
<iframe src="[[config.url]]"></iframe>
|
||||||
</div>
|
</div>
|
||||||
</ha-card>
|
</ha-card>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
_computeTitle(config) {
|
|
||||||
if (!config.url) return 'Error: URL not configured';
|
|
||||||
return config.title || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
_configChanged(config) {
|
_configChanged(config) {
|
||||||
this.shadowRoot.querySelector('.wrapper').style.paddingTop = config.aspect_ratio || '50%';
|
this.$.root.style.paddingTop = config.aspect_ratio || '50%';
|
||||||
}
|
}
|
||||||
|
|
||||||
getCardSize() {
|
getCardSize() {
|
||||||
|
@ -1,20 +1,21 @@
|
|||||||
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
||||||
|
|
||||||
import computeDomain from '../../../common/entity/compute_domain.js';
|
|
||||||
|
|
||||||
import '../../../cards/ha-media_player-card.js';
|
import '../../../cards/ha-media_player-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 HuiMediaControlCard extends PolymerElement {
|
class HuiMediaControlCard extends PolymerElement {
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
hass: {
|
hass: {
|
||||||
type: Object,
|
type: Object,
|
||||||
observer: '_hassChanged',
|
observer: '_hassChanged'
|
||||||
},
|
},
|
||||||
config: {
|
config: {
|
||||||
type: Object,
|
type: Object,
|
||||||
observer: '_configChanged',
|
observer: '_configChanged'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -25,50 +26,35 @@ class HuiMediaControlCard extends PolymerElement {
|
|||||||
|
|
||||||
_configChanged(config) {
|
_configChanged(config) {
|
||||||
this._entityId = null;
|
this._entityId = null;
|
||||||
|
|
||||||
if (this.lastChild) {
|
if (this.lastChild) {
|
||||||
this.removeChild(this.lastChild);
|
this.removeChild(this.lastChild);
|
||||||
}
|
}
|
||||||
const entityId = config && config.entity;
|
|
||||||
if (entityId && !(entityId in this.hass.states)) {
|
if (!validateEntityConfig(config, 'media_player')) {
|
||||||
|
const error = 'Error in card configuration.';
|
||||||
|
const element = createCardElement(createErrorCardConfig(error, config));
|
||||||
|
this.appendChild(element);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let error = null;
|
const entityId = config.entity;
|
||||||
let cardConfig;
|
if (!(entityId in this.hass.states)) {
|
||||||
let tag;
|
return;
|
||||||
|
|
||||||
if (entityId) {
|
|
||||||
if (computeDomain(entityId) === 'media_player') {
|
|
||||||
this._entityId = entityId;
|
|
||||||
tag = 'ha-media_player-card';
|
|
||||||
cardConfig = config;
|
|
||||||
} else {
|
|
||||||
error = 'Entity domain must be "media_player"';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
error = 'Entity not defined in card config';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
const element = document.createElement('ha-media_player-card');
|
||||||
tag = 'hui-error-card';
|
element.stateObj = this.hass.states[entityId];
|
||||||
cardConfig = { error };
|
element.hass = this.hass;
|
||||||
}
|
|
||||||
const element = document.createElement(tag);
|
|
||||||
|
|
||||||
if (!error) {
|
|
||||||
element.stateObj = this.hass.states[entityId];
|
|
||||||
element.hass = this.hass;
|
|
||||||
}
|
|
||||||
|
|
||||||
element.config = cardConfig;
|
|
||||||
this.appendChild(element);
|
this.appendChild(element);
|
||||||
|
this._entityId = entityId;
|
||||||
}
|
}
|
||||||
|
|
||||||
_hassChanged(hass) {
|
_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 element = this.lastChild;
|
||||||
const stateObj = hass.states[this._entityId];
|
element.stateObj = hass.states[entityId];
|
||||||
element.stateObj = stateObj;
|
|
||||||
element.hass = hass;
|
element.hass = hass;
|
||||||
} else {
|
} else {
|
||||||
this._configChanged(this.config);
|
this._configChanged(this.config);
|
||||||
|
@ -105,7 +105,7 @@ class HuiPictureElementsCard extends LocalizeMixin(EventsMixin(PolymerElement))
|
|||||||
el.domain = (element.service && element.domain) || 'homeassistant';
|
el.domain = (element.service && element.domain) || 'homeassistant';
|
||||||
el.service = (element.service && element.service.service) || '';
|
el.service = (element.service && element.service.service) || '';
|
||||||
el.serviceData = (element.service && element.service.data) || {};
|
el.serviceData = (element.service && element.service.data) || {};
|
||||||
el.innerText = element.text;
|
el.innerText = element.title;
|
||||||
}
|
}
|
||||||
el.classList.add('element');
|
el.classList.add('element');
|
||||||
if (element.style) {
|
if (element.style) {
|
||||||
|
@ -1,20 +1,21 @@
|
|||||||
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
||||||
|
|
||||||
import computeDomain from '../../../common/entity/compute_domain.js';
|
|
||||||
|
|
||||||
import '../../../cards/ha-plant-card.js';
|
import '../../../cards/ha-plant-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 HuiPlantStatusCard extends PolymerElement {
|
class HuiPlantStatusCard extends PolymerElement {
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
hass: {
|
hass: {
|
||||||
type: Object,
|
type: Object,
|
||||||
observer: '_hassChanged',
|
observer: '_hassChanged'
|
||||||
},
|
},
|
||||||
config: {
|
config: {
|
||||||
type: Object,
|
type: Object,
|
||||||
observer: '_configChanged',
|
observer: '_configChanged'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -25,50 +26,35 @@ class HuiPlantStatusCard extends PolymerElement {
|
|||||||
|
|
||||||
_configChanged(config) {
|
_configChanged(config) {
|
||||||
this._entityId = null;
|
this._entityId = null;
|
||||||
|
|
||||||
if (this.lastChild) {
|
if (this.lastChild) {
|
||||||
this.removeChild(this.lastChild);
|
this.removeChild(this.lastChild);
|
||||||
}
|
}
|
||||||
const entityId = config && config.entity;
|
|
||||||
if (entityId && !(entityId in this.hass.states)) {
|
if (!validateEntityConfig(config, 'plant')) {
|
||||||
|
const error = 'Error in card configuration.';
|
||||||
|
const element = createCardElement(createErrorCardConfig(error, config));
|
||||||
|
this.appendChild(element);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let error = null;
|
const entityId = config.entity;
|
||||||
let cardConfig;
|
if (!(entityId in this.hass.states)) {
|
||||||
let tag;
|
return;
|
||||||
|
|
||||||
if (entityId) {
|
|
||||||
if (computeDomain(entityId) === 'plant') {
|
|
||||||
this._entityId = entityId;
|
|
||||||
tag = 'ha-plant-card';
|
|
||||||
cardConfig = config;
|
|
||||||
} else {
|
|
||||||
error = 'Entity domain must be "plant"';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
error = 'Entity not defined in card config';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
const element = document.createElement('ha-plant-card');
|
||||||
tag = 'hui-error-card';
|
element.stateObj = this.hass.states[entityId];
|
||||||
cardConfig = { error };
|
element.hass = this.hass;
|
||||||
}
|
|
||||||
const element = document.createElement(tag);
|
|
||||||
|
|
||||||
if (!error) {
|
|
||||||
element.stateObj = this.hass.states[entityId];
|
|
||||||
element.hass = this.hass;
|
|
||||||
}
|
|
||||||
|
|
||||||
element.config = cardConfig;
|
|
||||||
this.appendChild(element);
|
this.appendChild(element);
|
||||||
|
this._entityId = entityId;
|
||||||
}
|
}
|
||||||
|
|
||||||
_hassChanged(hass) {
|
_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 element = this.lastChild;
|
||||||
const stateObj = hass.states[this._entityId];
|
element.stateObj = hass.states[entityId];
|
||||||
element.stateObj = stateObj;
|
|
||||||
element.hass = hass;
|
element.hass = hass;
|
||||||
} else {
|
} else {
|
||||||
this._configChanged(this.config);
|
this._configChanged(this.config);
|
||||||
|
@ -1,20 +1,21 @@
|
|||||||
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
|
||||||
|
|
||||||
import computeDomain from '../../../common/entity/compute_domain.js';
|
|
||||||
|
|
||||||
import '../../../cards/ha-weather-card.js';
|
import '../../../cards/ha-weather-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 HuiWeatherForecastCard extends PolymerElement {
|
class HuiWeatherForecastCard extends PolymerElement {
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
hass: {
|
hass: {
|
||||||
type: Object,
|
type: Object,
|
||||||
observer: '_hassChanged',
|
observer: '_hassChanged'
|
||||||
},
|
},
|
||||||
config: {
|
config: {
|
||||||
type: Object,
|
type: Object,
|
||||||
observer: '_configChanged',
|
observer: '_configChanged'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -25,50 +26,35 @@ class HuiWeatherForecastCard extends PolymerElement {
|
|||||||
|
|
||||||
_configChanged(config) {
|
_configChanged(config) {
|
||||||
this._entityId = null;
|
this._entityId = null;
|
||||||
|
|
||||||
if (this.lastChild) {
|
if (this.lastChild) {
|
||||||
this.removeChild(this.lastChild);
|
this.removeChild(this.lastChild);
|
||||||
}
|
}
|
||||||
const entityId = config && config.entity;
|
|
||||||
if (entityId && !(entityId in this.hass.states)) {
|
if (!validateEntityConfig(config, 'weather')) {
|
||||||
|
const error = 'Error in card configuration.';
|
||||||
|
const element = createCardElement(createErrorCardConfig(error, config));
|
||||||
|
this.appendChild(element);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let error = null;
|
const entityId = config.entity;
|
||||||
let cardConfig;
|
if (!(entityId in this.hass.states)) {
|
||||||
let tag;
|
return;
|
||||||
|
|
||||||
if (entityId) {
|
|
||||||
if (computeDomain(entityId) === 'weather') {
|
|
||||||
this._entityId = entityId;
|
|
||||||
tag = 'ha-weather-card';
|
|
||||||
cardConfig = config;
|
|
||||||
} else {
|
|
||||||
error = 'Entity domain must be "weather"';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
error = 'Entity not defined in card config';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
const element = document.createElement('ha-weather-card');
|
||||||
tag = 'hui-error-card';
|
element.stateObj = this.hass.states[entityId];
|
||||||
cardConfig = { error };
|
element.hass = this.hass;
|
||||||
}
|
|
||||||
const element = document.createElement(tag);
|
|
||||||
|
|
||||||
if (!error) {
|
|
||||||
element.stateObj = this.hass.states[entityId];
|
|
||||||
element.hass = this.hass;
|
|
||||||
}
|
|
||||||
|
|
||||||
element.config = cardConfig;
|
|
||||||
this.appendChild(element);
|
this.appendChild(element);
|
||||||
|
this._entityId = entityId;
|
||||||
}
|
}
|
||||||
|
|
||||||
_hassChanged(hass) {
|
_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 element = this.lastChild;
|
||||||
const stateObj = hass.states[this._entityId];
|
element.stateObj = hass.states[entityId];
|
||||||
element.stateObj = stateObj;
|
|
||||||
element.hass = hass;
|
element.hass = hass;
|
||||||
} else {
|
} else {
|
||||||
this._configChanged(this.config);
|
this._configChanged(this.config);
|
||||||
|
16
src/panels/lovelace/common/validate-entity-config.js
Normal file
16
src/panels/lovelace/common/validate-entity-config.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// check for valid value of config.entity with optinal entity dommain check
|
||||||
|
import computeDomain from '../../../common/entity/compute_domain.js';
|
||||||
|
|
||||||
|
export default function validateEntityConfig(config, domain = null) {
|
||||||
|
const entity = config && config.entity;
|
||||||
|
|
||||||
|
if (!entity || typeof entity !== 'string') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (domain) {
|
||||||
|
return computeDomain(entity) === domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user