-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-of-use.js
141 lines (130 loc) · 3.32 KB
/
time-of-use.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Translate day of week number to name
*
* @param {Number} - day of week 0-6
*
* @return {String} - day of the week name
*/
const getDayofWeek = (dow) => {
switch (dow) {
case 0:
return 'Sunday';
case 1:
return 'Monday';
case 2:
return 'Tuesday';
case 3:
return 'Wednesday';
case 4:
return 'Thursday';
case 5:
return 'Friday';
case 6:
return 'Saturday';
}
}
/**
* Get current date (mm/dd/yyyy)
*
* @return {String}
*/
const getCurrentDate = (date) => {
return `${getDayofWeek(date.getDay())} ${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`;
}
/**
* SDGE Tiers by day and hour of day
*/
const tiers = {
'TOU-DR1': {
'weekday': {
'super-off-peak': [0,1,2,3,4,5],
'off-peak': [6,7,8,9,10,11,12,13,14,15,21,22,23],
'on-peak': [16,17,18,19,20]
},
'weekend': {
'super-off-peak': [0,1,2,3,4,5,6,7,8,9,10,11,12,13],
'off-peak': [14,15,21,22,23],
'on-peak': [16,17,18,19,20]
}
},
'TOU-DR2': {
'weekday': {
'off-peak': [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,22,23],
'on-peak': [17,18,19,20,21]
},
'weekend': {
'off-peak': [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,22,23],
'on-peak': [17,18,19,20,21]
}
}
}
/**
* Calculate current SDGE time of use
*
* @param {String} plan - sdge plan
* @param {Number} time - hour of day
* @param {Number} day - day of the week
*
* @return {String} - SDGE time of use
*/
const calculateTimeofUse = (plan, time, day) => {
if ( day === 0 || day === 6 ) {
for (const tier in tiers[plan].weekend) {
if ( tiers[plan].weekend[tier].includes(time) ) {
return tier;
}
}
} else {
for (const tier in tiers[plan].weekday) {
if ( tiers[plan].weekday[tier].includes(time) ) {
return tier;
}
}
}
}
class TimeOfUse extends HTMLElement {
set hass(hass) {
if (!this.content) {
const card = document.createElement('ha-card');
card.style.color = '#FFFFFF';
card.header = 'SDGE Time of Use';
card.style.color = '#FFFFFF';
this.content = document.createElement('div');
this.content.style.padding = '0 16px 16px';
card.appendChild(this.content);
this.appendChild(card);
// Energy plan
const energyPlan = this.config.plan;
// Time of use
const now = new Date();
const currentDate = getCurrentDate(now);
const timeOfUse = calculateTimeofUse(energyPlan, now.getHours(), now.getDay());
if ( timeOfUse === 'super-off-peak' ) {
card.style.background = '#b1bf4a';
} else if ( timeOfUse === 'off-peak' ) {
card.style.background = '#40ae49';
} else {
card.style.background = '#f68d1e';
}
this.content.innerHTML = `
<div>
${currentDate}
<br>
<h1 style="text-transform: uppercase; text-align: center;">${timeOfUse}</h1>
</div>
`;
}
}
setConfig(config) {
if (!config.plan) {
throw new Error('You need to define a plan');
}
this.config = config;
}
// The height of your card. Home Assistant uses this to automatically
// distribute all cards over the available columns.
getCardSize() {
return 3;
}
}
customElements.define('time-of-use', TimeOfUse);