mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-15 05:16:34 +00:00
Display history charts up to end of selected time (#254)
* Display history charts up to end time * Back into JS world
This commit is contained in:
parent
57512211d7
commit
abbdc6f055
@ -36,7 +36,8 @@
|
|||||||
<ha-state-history-data
|
<ha-state-history-data
|
||||||
hass='[[hass]]'
|
hass='[[hass]]'
|
||||||
filter-type='[[_filterType]]'
|
filter-type='[[_filterType]]'
|
||||||
filter-value='[[_computeFilterDate(_currentDate, _periodIndex)]]'
|
start-time='[[_computeStartTime(_currentDate)]]'
|
||||||
|
end-time='[[_computeEndTime(_currentDate, _periodIndex)]]'
|
||||||
data='{{stateHistoryInput}}'
|
data='{{stateHistoryInput}}'
|
||||||
is-loading='{{isLoadingData}}'
|
is-loading='{{isLoadingData}}'
|
||||||
></ha-state-history-data>
|
></ha-state-history-data>
|
||||||
@ -148,16 +149,19 @@ Polymer({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
_computeFilterDate: function (_currentDate, periodIndex) {
|
_computeStartTime: function (_currentDate) {
|
||||||
if (!_currentDate) return undefined;
|
if (!_currentDate) return undefined;
|
||||||
var parts = _currentDate.split('-');
|
var parts = _currentDate.split('-');
|
||||||
parts[1] = parseInt(parts[1]) - 1;
|
parts[1] = parseInt(parts[1]) - 1;
|
||||||
var startTime = new Date(parts[0], parts[1], parts[2]);
|
return new Date(parts[0], parts[1], parts[2]);
|
||||||
|
},
|
||||||
|
|
||||||
|
_computeEndTime: function (_currentDate, periodIndex) {
|
||||||
|
var startTime = this._computeStartTime(_currentDate);
|
||||||
var endTime = new Date(startTime);
|
var endTime = new Date(startTime);
|
||||||
endTime.setDate(
|
endTime.setDate(
|
||||||
startTime.getDate() + this._computeFilterDays(periodIndex));
|
startTime.getDate() + this._computeFilterDays(periodIndex));
|
||||||
|
return endTime;
|
||||||
return startTime.toISOString() + '?end_time=' + endTime.toISOString();
|
|
||||||
},
|
},
|
||||||
|
|
||||||
_computeFilterDays: function (periodIndex) {
|
_computeFilterDays: function (periodIndex) {
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
var DATE_CACHE = {};
|
var DATE_CACHE = {};
|
||||||
var RECENT_CACHE = {};
|
var RECENT_CACHE = {};
|
||||||
|
|
||||||
function computeHistory(stateHistory) {
|
function computeHistory(stateHistory, endTime) {
|
||||||
var lineChartDevices = {};
|
var lineChartDevices = {};
|
||||||
var timelineDevices = [];
|
var timelineDevices = [];
|
||||||
var unitStates;
|
var unitStates;
|
||||||
@ -23,6 +23,14 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Duplicate the last state to the selected endTime so the chart expands
|
||||||
|
// to the end of the selected time. (If no additional state changes were
|
||||||
|
// found, the state was still the last state as of the end time.
|
||||||
|
var lastState = Object.assign({}, stateInfo[stateInfo.length - 1]);
|
||||||
|
lastState.last_changed = endTime;
|
||||||
|
lastState.last_updated = endTime;
|
||||||
|
stateInfo.push(lastState);
|
||||||
|
|
||||||
stateWithUnit = stateInfo.find(
|
stateWithUnit = stateInfo.find(
|
||||||
function (state) { return 'unit_of_measurement' in state.attributes; });
|
function (state) { return 'unit_of_measurement' in state.attributes; });
|
||||||
|
|
||||||
@ -59,7 +67,15 @@
|
|||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
|
|
||||||
filterValue: {
|
startTime: {
|
||||||
|
type: Date,
|
||||||
|
},
|
||||||
|
|
||||||
|
endTime: {
|
||||||
|
type: Date,
|
||||||
|
},
|
||||||
|
|
||||||
|
entityId: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -79,24 +95,26 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
observers: [
|
observers: [
|
||||||
'filterChanged(filterType, filterValue)',
|
'filterChanged(filterType, entityId, startTime, endTime)',
|
||||||
],
|
],
|
||||||
|
|
||||||
hassChanged: function (newHass, oldHass) {
|
hassChanged: function (newHass, oldHass) {
|
||||||
if (!oldHass && this.filterType && this.filterValue) {
|
if (!oldHass) {
|
||||||
this.filterChanged(this.filterType, this.filterValue);
|
this.filterChanged(this.filterType, this.entityId, this.startTime, this.endTime);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
filterChanged: function (filterType, filterValue) {
|
filterChanged: function (filterType, entityId, startTime, endTime) {
|
||||||
if (!this.hass) return;
|
if (!this.hass) return;
|
||||||
|
|
||||||
var data;
|
var data;
|
||||||
|
|
||||||
if (filterType === 'date') {
|
if (filterType === 'date') {
|
||||||
data = this.getDate(filterValue);
|
if (startTime === undefined || endTime === undefined) return;
|
||||||
|
data = this.getDate(startTime, endTime);
|
||||||
} else if (filterType === 'recent-entity') {
|
} else if (filterType === 'recent-entity') {
|
||||||
data = this.getRecent(filterValue);
|
if (entityId === undefined) return;
|
||||||
|
data = this.getRecent(entityId);
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -124,7 +142,7 @@
|
|||||||
|
|
||||||
var prom = this.hass.callApi('GET', url).then(
|
var prom = this.hass.callApi('GET', url).then(
|
||||||
function (stateHistory) {
|
function (stateHistory) {
|
||||||
return computeHistory(stateHistory);
|
return computeHistory(stateHistory, Date.now());
|
||||||
},
|
},
|
||||||
function () {
|
function () {
|
||||||
RECENT_CACHE[entityId] = false;
|
RECENT_CACHE[entityId] = false;
|
||||||
@ -140,20 +158,21 @@
|
|||||||
return prom;
|
return prom;
|
||||||
},
|
},
|
||||||
|
|
||||||
getDate: function (date) {
|
getDate: function (startTime, endTime) {
|
||||||
if (!DATE_CACHE[date]) {
|
var filter = startTime.toISOString() + '?end_time=' + endTime.toISOString();
|
||||||
DATE_CACHE[date] = this.hass.callApi('GET', 'history/period/' + date).then(
|
if (!DATE_CACHE[filter]) {
|
||||||
|
DATE_CACHE[filter] = this.hass.callApi('GET', 'history/period/' + filter).then(
|
||||||
function (stateHistory) {
|
function (stateHistory) {
|
||||||
return computeHistory(stateHistory);
|
return computeHistory(stateHistory, endTime);
|
||||||
},
|
},
|
||||||
function () {
|
function () {
|
||||||
DATE_CACHE[date] = false;
|
DATE_CACHE[filter] = false;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return DATE_CACHE[date];
|
return DATE_CACHE[filter];
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
@ -61,7 +61,7 @@
|
|||||||
<ha-state-history-data
|
<ha-state-history-data
|
||||||
hass='[[hass]]'
|
hass='[[hass]]'
|
||||||
filter-type='[[_filterType]]'
|
filter-type='[[_filterType]]'
|
||||||
filter-value='[[stateObj.entity_id]]'
|
entity-id='[[stateObj.entity_id]]'
|
||||||
data='{{stateHistory}}'
|
data='{{stateHistory}}'
|
||||||
is-loading='{{stateHistoryLoading}}'
|
is-loading='{{stateHistoryLoading}}'
|
||||||
></ha-state-history-data>
|
></ha-state-history-data>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user