Love: Rework history graph (#1335)

* Love: Rework history graph

* Lint
This commit is contained in:
c727 2018-06-26 16:51:34 +02:00 committed by Paulus Schoutsen
parent 9fd1db0493
commit e3d7220c0b

View File

@ -1,21 +1,29 @@
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
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-history_graph-card.js';
import './hui-error-card.js'; import './hui-error-card.js';
import '../../../components/state-history-charts.js';
import '../../../data/ha-state-history-data.js';
class HuiHistoryGraphCard extends PolymerElement { class HuiHistoryGraphCard extends PolymerElement {
static get properties() { static get properties() {
return { return {
hass: { hass: Object,
type: Object,
observer: '_hassChanged',
},
config: { config: {
type: Object, type: Object,
observer: '_configChanged', observer: '_configChanged',
} },
_error: String,
stateHistory: Object,
stateHistoryLoading: Boolean,
cacheConfig: {
type: Object,
value: {
refresh: 0,
cacheKey: null,
hoursToShow: 24,
},
},
}; };
} }
@ -23,55 +31,65 @@ class HuiHistoryGraphCard extends PolymerElement {
return 4; return 4;
} }
_configChanged(config) { static get template() {
this._entityId = null; return html`
if (this.lastChild) { <style>
this.removeChild(this.lastChild); ha-card {
} padding: 16px;
const entityId = config && config.entity; }
if (entityId && !(entityId in this.hass.states)) { .header {
return; @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>
let error = null; <template is="dom-if" if="[[!_error]]">
let cardConfig; <ha-card>
let tag; <div class='header'>
<div class="name">[[config.title]]</div>
</div>
<ha-state-history-data
hass="[[hass]]"
filter-type="recent-entity"
entity-id="[[config.entities]]"
data="{{stateHistory}}"
is-loading="{{stateHistoryLoading}}"
cache-config="[[cacheConfig]]"
></ha-state-history-data>
<state-history-charts
hass="[[hass]]"
history-data="[[stateHistory]]"
is-loading-data="[[stateHistoryLoading]]"
up-to-now
no-single
></state-history-charts>
</ha-card>
</template>
if (entityId) { <template is="dom-if" if="[[_error]]">
if (computeDomain(entityId) === 'history_graph') { <hui-error-card error="[[_error]]" config="[[config]]"></hui-error-card>
this._entityId = entityId; </template>
tag = 'ha-history_graph-card'; `;
cardConfig = config;
} else {
error = 'Entity domain must be "history_graph"';
}
} else {
error = 'Entity not defined in card config';
}
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;
this.appendChild(element);
} }
_hassChanged(hass) { _configChanged(config) {
if (this.lastChild && this._entityId && this._entityId in hass.states) { if (config.entities && Array.isArray(config.entities)) {
const element = this.lastChild; this._error = null;
const stateObj = hass.states[this._entityId];
element.stateObj = stateObj; this.cacheConfig = {
element.hass = hass; refresh: config.refresh || 0,
cacheKey: config.entities,
hoursToShow: config.hours || 24
};
} else { } else {
this._configChanged(this.config); this._error = 'No entities configured.';
} }
} }
} }