Get State History support from Store

This commit is contained in:
Paulus Schoutsen 2015-02-06 00:02:40 -08:00
parent 029379c092
commit e3643b1faf
5 changed files with 75 additions and 55 deletions

View file

@ -1,2 +1,2 @@
""" DO NOT MODIFY. Auto-generated by build_frontend script """ """ DO NOT MODIFY. Auto-generated by build_frontend script """
VERSION = "d7b5242370bf59018a6a4a2163d7daea" VERSION = "b08c8203bd0a78ae1b1b2eec9f62f8b4"

File diff suppressed because one or more lines are too long

View file

@ -2,7 +2,7 @@
<link rel="import" href="../bower_components/google-apis/google-jsapi.html"> <link rel="import" href="../bower_components/google-apis/google-jsapi.html">
<polymer-element name="state-timeline" attributes="stateObj api"> <polymer-element name="state-timeline" attributes="stateHistory">
<template> <template>
<style> <style>
:host { :host {
@ -17,7 +17,7 @@
<script> <script>
Polymer({ Polymer({
apiLoaded: false, apiLoaded: false,
stateData: null, stateHistory: null,
googleApiLoaded: function() { googleApiLoaded: function() {
google.load("visualization", "1", { google.load("visualization", "1", {
@ -29,32 +29,12 @@
}); });
}, },
stateObjChanged: function(oldVal, newVal) { stateHistoryChanged: function() {
// update data if we get a new stateObj
if (!oldVal || (newVal && oldVal.entity_id === newVal.entity_id)) {
this.drawChart(); this.drawChart();
} else {
this.fetchData();
}
},
fetchData: function() {
this.stateData = null;
var url = 'history/period';
if (this.stateObj) {
url += '?filter_entity_id=' + this.stateObj.entity_id;
}
window.hass.callApi('GET', url).then(function(stateData) {
this.stateData = stateData;
this.drawChart();
}.bind(this));
}, },
drawChart: function() { drawChart: function() {
if (!this.apiLoaded || this.stateData === null) { if (!this.apiLoaded || !this.stateHistory) {
return; return;
} }
@ -82,8 +62,12 @@
stateTimeToDate(tillState.last_changed)]); stateTimeToDate(tillState.last_changed)]);
}; };
// this.stateData is a list of lists of sorted state objects // people can pass in history of 1 entityId or a collection.
this.stateData.forEach(function(stateInfo) { var stateHistory = _.isArray(this.stateHistory[0]) ?
this.stateHistory : [this.stateHistory];
// stateHistory is a list of lists of sorted state objects
stateHistory.forEach(function(stateInfo) {
var baseState = new window.hass.stateModel(stateInfo[0]); var baseState = new window.hass.stateModel(stateInfo[0]);
var prevRow = null; var prevRow = null;
@ -101,20 +85,19 @@
}.bind(this)); }.bind(this));
chart.draw(dataTable, { chart.draw(dataTable, {
height: 55 + this.stateData.length * 42, height: 55 + stateHistory.length * 42,
// interactive properties require CSS, the JS api puts it on the document // interactive properties require CSS, the JS api puts it on the document
// instead of inside our Shadow DOM. // instead of inside our Shadow DOM.
enableInteractivity: false, enableInteractivity: false,
timeline: { timeline: {
showRowLabels: this.stateData.length > 1 showRowLabels: stateHistory.length > 1
}, },
hAxis: { hAxis: {
format: 'H:mm' format: 'H:mm'
}, },
// colors: ['#CCC', '#03a9f4']
}); });
}, },

View file

@ -17,7 +17,7 @@
<div> <div>
<state-card-content stateObj="{{stateObj}}" class='title-card'> <state-card-content stateObj="{{stateObj}}" class='title-card'>
</state-card-content> </state-card-content>
<state-timeline stateObj="{{stateObj}}"></state-timeline> <state-timeline stateHistory="{{stateHistory}}"></state-timeline>
<more-info-content stateObj="{{stateObj}}"></more-info-content> <more-info-content stateObj="{{stateObj}}"></more-info-content>
</div> </div>
</ha-action-dialog> </ha-action-dialog>
@ -26,11 +26,39 @@
<script> <script>
Polymer({ Polymer({
stateObj: {}, stateObj: {},
stateHistory: null,
observe: { observe: {
'stateObj.attributes': 'reposition' 'stateObj.attributes': 'reposition'
}, },
ready: function() {
this.stateHistoryStoreChanged = this.stateHistoryStoreChanged.bind(this);
window.hass.stateHistoryStore.addChangeListener(this.stateHistoryStoreChanged);
},
detached: function() {
window.hass.stateHistoryStore.removeChangeListener(this.stateHistoryStoreChanged);
},
stateHistoryStoreChanged: function() {
if (this.stateObj.entityId) {
this.stateHistory = window.hass.stateHistoryStore.get(this.stateObj.entityId);
} else {
this.stateHistory = null;
}
},
stateObjChanged: function() {
if (this.stateObj.entityId &&
window.hass.stateHistoryStore.isStale(this.stateObj.entityId)) {
window.hass.stateHistoryActions.fetch(this.stateObj.entityId);
}
this.stateHistoryStoreChanged();
},
/** /**
* Whenever the attributes change, the more info component can * Whenever the attributes change, the more info component can
* hide or show elements. We will reposition the dialog. * hide or show elements. We will reposition the dialog.

View file

@ -7,9 +7,13 @@
<polymer-element name="partial-history" attributes="narrow togglePanel"> <polymer-element name="partial-history" attributes="narrow togglePanel">
<template> <template>
<style> <style>
state-timeline { .content {
background-color: white; background-color: white;
} }
.content.wide {
padding: 8px;
}
</style> </style>
<partial-base narrow="{{narrow}}" togglePanel="{{togglePanel}}"> <partial-base narrow="{{narrow}}" togglePanel="{{togglePanel}}">
<span header-title>History</span> <span header-title>History</span>
@ -19,32 +23,37 @@
on-click="{{handleRefreshClick}}"></paper-icon-button> on-click="{{handleRefreshClick}}"></paper-icon-button>
</span> </span>
<state-timeline id='timeline'></state-timeline> <div flex class="{{ {content: true, narrow: narrow, wide: !narrow} | tokenList }}">
<state-timeline stateHistory="{{stateHistory}}"></state-timeline>
</div>
</partial-base> </partial-base>
</template> </template>
<script> <script>
Polymer({ Polymer({
stateHistory: null,
ready: function() { ready: function() {
this.$.timeline.fetchData(); this.stateHistoryStoreChanged = this.stateHistoryStoreChanged.bind(this);
// this.stateStoreChanged = this.stateStoreChanged.bind(this); window.hass.stateHistoryStore.addChangeListener(this.stateHistoryStoreChanged);
// window.hass.stateStore.addChangeListener(this.stateStoreChanged);
// this.stateStoreChanged(); if (window.hass.stateHistoryStore.isStale()) {
window.hass.stateHistoryActions.fetchAll();
}
this.stateHistoryStoreChanged();
}, },
detached: function() { detached: function() {
// window.hass.stateStore.removeChangeListener(this.stateStoreChanged); window.hass.stateHistoryStore.removeChangeListener(this.stateHistoryStoreChanged);
}, },
stateStoreChanged: function() { stateHistoryStoreChanged: function() {
// this.states = _.filter(window.hass.stateStore.all(), function(state) { this.stateHistory = window.hass.stateHistoryStore.all();
// return state.domain !== 'group';
// });
}, },
handleRefreshClick: function() { handleRefreshClick: function() {
this.$.timeline.fetchData(); window.hass.stateHistoryActions.fetchAll();
}, },
}); });
</script> </script>