Skip to content

Commit

Permalink
Get State History support from Store
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Feb 6, 2015
1 parent 029379c commit e3643b1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 55 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/frontend/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
""" DO NOT MODIFY. Auto-generated by build_frontend script """
VERSION = "d7b5242370bf59018a6a4a2163d7daea"
VERSION = "b08c8203bd0a78ae1b1b2eec9f62f8b4"
22 changes: 11 additions & 11 deletions homeassistant/components/frontend/www_static/frontend.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<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>
<style>
:host {
Expand All @@ -17,7 +17,7 @@
<script>
Polymer({
apiLoaded: false,
stateData: null,
stateHistory: null,

googleApiLoaded: function() {
google.load("visualization", "1", {
Expand All @@ -29,32 +29,12 @@
});
},

stateObjChanged: function(oldVal, newVal) {
// update data if we get a new stateObj
if (!oldVal || (newVal && oldVal.entity_id === newVal.entity_id)) {
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));
stateHistoryChanged: function() {
this.drawChart();
},

drawChart: function() {
if (!this.apiLoaded || this.stateData === null) {
if (!this.apiLoaded || !this.stateHistory) {
return;
}

Expand Down Expand Up @@ -82,8 +62,12 @@
stateTimeToDate(tillState.last_changed)]);
};

// this.stateData is a list of lists of sorted state objects
this.stateData.forEach(function(stateInfo) {
// people can pass in history of 1 entityId or a collection.
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 prevRow = null;
Expand All @@ -101,20 +85,19 @@
}.bind(this));

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
// instead of inside our Shadow DOM.
enableInteractivity: false,

timeline: {
showRowLabels: this.stateData.length > 1
showRowLabels: stateHistory.length > 1
},

hAxis: {
format: 'H:mm'
},
// colors: ['#CCC', '#03a9f4']
});
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div>
<state-card-content stateObj="{{stateObj}}" class='title-card'>
</state-card-content>
<state-timeline stateObj="{{stateObj}}"></state-timeline>
<state-timeline stateHistory="{{stateHistory}}"></state-timeline>
<more-info-content stateObj="{{stateObj}}"></more-info-content>
</div>
</ha-action-dialog>
Expand All @@ -26,11 +26,39 @@
<script>
Polymer({
stateObj: {},
stateHistory: null,

observe: {
'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
* hide or show elements. We will reposition the dialog.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
<polymer-element name="partial-history" attributes="narrow togglePanel">
<template>
<style>
state-timeline {
.content {
background-color: white;
}

.content.wide {
padding: 8px;
}
</style>
<partial-base narrow="{{narrow}}" togglePanel="{{togglePanel}}">
<span header-title>History</span>
Expand All @@ -19,32 +23,37 @@
on-click="{{handleRefreshClick}}"></paper-icon-button>
</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>
</template>
<script>
Polymer({
stateHistory: null,

ready: function() {
this.$.timeline.fetchData();
this.stateHistoryStoreChanged = this.stateHistoryStoreChanged.bind(this);

window.hass.stateHistoryStore.addChangeListener(this.stateHistoryStoreChanged);

// this.stateStoreChanged = this.stateStoreChanged.bind(this);
// window.hass.stateStore.addChangeListener(this.stateStoreChanged);
if (window.hass.stateHistoryStore.isStale()) {
window.hass.stateHistoryActions.fetchAll();
}

// this.stateStoreChanged();
this.stateHistoryStoreChanged();
},

detached: function() {
// window.hass.stateStore.removeChangeListener(this.stateStoreChanged);
window.hass.stateHistoryStore.removeChangeListener(this.stateHistoryStoreChanged);
},

stateStoreChanged: function() {
// this.states = _.filter(window.hass.stateStore.all(), function(state) {
// return state.domain !== 'group';
// });
stateHistoryStoreChanged: function() {
this.stateHistory = window.hass.stateHistoryStore.all();
},

handleRefreshClick: function() {
this.$.timeline.fetchData();
window.hass.stateHistoryActions.fetchAll();
},
});
</script>
Expand Down

0 comments on commit e3643b1

Please sign in to comment.