@@ -4,7 +4,7 @@ import { compress, decompress, log } from './utils';
4
4
import localForage from 'localforage' ;
5
5
import { HassEntity } from 'home-assistant-js-websocket' ;
6
6
import { DateRange } from 'moment-range' ;
7
- import { DEFAULT_HOURS_TO_SHOW , moment } from './const' ;
7
+ import { HOUR_24 , moment } from './const' ;
8
8
import parse from 'parse-duration' ;
9
9
import SparkMD5 from 'spark-md5' ;
10
10
@@ -23,7 +23,9 @@ export default class GraphEntry {
23
23
24
24
private _cache = true ;
25
25
26
- private _hoursToShow : number ;
26
+ // private _hoursToShow: number;
27
+
28
+ private _graphSpan : number ;
27
29
28
30
private _useCompress = false ;
29
31
@@ -43,7 +45,7 @@ export default class GraphEntry {
43
45
44
46
private _md5Config : string ;
45
47
46
- constructor ( entity : string , index : number , hoursToShow : number , cache : boolean , config : ChartCardSeriesConfig ) {
48
+ constructor ( entity : string , index : number , graphSpan : number , cache : boolean , config : ChartCardSeriesConfig ) {
47
49
const aggregateFuncMap = {
48
50
avg : this . _average ,
49
51
max : this . _maximum ,
@@ -58,19 +60,19 @@ export default class GraphEntry {
58
60
this . _cache = cache ;
59
61
this . _entityID = entity ;
60
62
this . _history = undefined ;
61
- this . _hoursToShow = hoursToShow ;
63
+ this . _graphSpan = graphSpan ;
62
64
this . _config = config ;
63
65
const now = new Date ( ) ;
64
66
const now2 = new Date ( now ) ;
65
67
this . _func = aggregateFuncMap [ config . group_by . func ] ;
66
- now2 . setHours ( now2 . getHours ( ) - DEFAULT_HOURS_TO_SHOW ) ;
68
+ now2 . setTime ( now2 . getTime ( ) - HOUR_24 ) ;
67
69
this . _timeRange = moment . range ( now , now2 ) ;
68
70
this . _realEnd = new Date ( ) ;
69
71
this . _realStart = new Date ( ) ;
70
72
// Valid because tested during init;
71
73
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
72
74
this . _groupByDurationMs = parse ( this . _config . group_by . duration ) ! ;
73
- this . _md5Config = SparkMD5 . hash ( `${ this . _hoursToShow } ${ JSON . stringify ( this . _config ) } ` ) ;
75
+ this . _md5Config = SparkMD5 . hash ( `${ this . _graphSpan } ${ JSON . stringify ( this . _config ) } ` ) ;
74
76
}
75
77
76
78
set hass ( hass : HomeAssistant ) {
@@ -129,7 +131,7 @@ export default class GraphEntry {
129
131
130
132
let history = this . _cache ? await this . _getCache ( this . _entityID , this . _useCompress ) : undefined ;
131
133
132
- if ( history && history . hours_to_show === this . _hoursToShow ) {
134
+ if ( history && history . span === this . _graphSpan ) {
133
135
const currDataIndex = history . data . findIndex ( ( item ) => item && new Date ( item [ 0 ] ) . getTime ( ) > start . getTime ( ) ) ;
134
136
if ( currDataIndex !== - 1 ) {
135
137
// skip initial state when fetching recent/not-cached data
@@ -160,14 +162,14 @@ export default class GraphEntry {
160
162
return [ new Date ( item . last_changed ) . getTime ( ) , ! Number . isNaN ( stateParsed ) ? stateParsed : null ] ;
161
163
} ) ;
162
164
if ( history ?. data . length ) {
163
- history . hours_to_show = this . _hoursToShow ;
165
+ history . span = this . _graphSpan ;
164
166
history . last_fetched = new Date ( ) ;
165
167
if ( history . data . length !== 0 ) {
166
168
history . data . push ( ...newStateHistory ) ;
167
169
}
168
170
} else {
169
171
history = {
170
- hours_to_show : this . _hoursToShow ,
172
+ span : this . _graphSpan ,
171
173
last_fetched : new Date ( ) ,
172
174
data : newStateHistory ,
173
175
} ;
0 commit comments