-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.ts
203 lines (160 loc) · 9.91 KB
/
app.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import Homey from 'homey';
import moment from 'moment-timezone';
import {holidays, Holiday} from '@balmli/homey-public-holidays'
import {UtilityBillApi} from '@balmli/homey-utility-prices-client'
module.exports = class HeatingControllerApp extends Homey.App {
utilityBillApi?: UtilityBillApi;
async onInit() {
moment.tz.setDefault(this.homey.clock.getTimezone());
this.utilityBillApi = new UtilityBillApi({logger: this.log, homey: this.homey});
await this._initFlows();
this.log('HeatingControllerApp is running...');
}
async _initFlows() {
this.homey.flow.getConditionCard('is_public_holiday')
.registerRunListener((args) => this.check(args, {'public': true}));
this.homey.flow.getConditionCard('is_bank_holiday')
.registerRunListener((args) => this.check(args, {'bank': true}));
this.homey.flow.getConditionCard('is_observance_holiday')
.registerRunListener((args) => this.check(args, {'observance': true}));
this.homey.flow.getConditionCard('is_holiday')
.registerRunListener((args) => this.check(args, {'public': true, 'bank': true, 'observance': true}));
this.homey.flow.getConditionCard('is_workingday')
.registerRunListener((args) => {
const theDay = holidays.calcDate(moment().toDate(), args.condition);
return theDay.getDay() >= 1 && theDay.getDay() <= 5 && !this.check(args, {
'public': true,
'bank': true,
'observance': true
});
});
this.homey.flow.getDeviceTriggerCard('high_x_hours_of_day')
.registerRunListener((args, state) => args.device.priceComparer.heatingOffHighPriceComparer(args, state));
this.homey.flow.getDeviceTriggerCard('low_x_hours_of_day')
.registerRunListener((args, state) => args.device.priceComparer.lowHoursComparer(args, state));
this.homey.flow.getConditionCard('is_home')
.registerRunListener((args, state) => args.device.getCapabilityValue('onoff'));
this.homey.flow.getConditionCard('is_home_override')
.registerRunListener((args, state) => args.device.getCapabilityValue('home_override'));
this.homey.flow.getConditionCard('is_night')
.registerRunListener((args, state) => args.device.getCapabilityValue('night'));
this.homey.flow.getConditionCard('is_at_work')
.registerRunListener((args, state) => args.device.getCapabilityValue('at_work'));
this.homey.flow.getConditionCard('is_heating_on')
.registerRunListener((args, state) => args.device.getCapabilityValue('heating'));
this.homey.flow.getConditionCard('current_price_below')
.registerRunListener(args => args.device._lastPrice && (args.price > args.device._lastPrice.price));
this.homey.flow.getConditionCard('current_price_among_x_highest_before_time')
.registerRunListener((args, state) => {
state.high_price = true;
return args.device.priceComparer.currentPriceAmongBeforeTimeComparer(args, state);
});
this.homey.flow.getConditionCard('current_price_among_x_lowest_before_time')
.registerRunListener((args, state) => {
state.high_price = false;
return args.device.priceComparer.currentPriceAmongBeforeTimeComparer(args, state);
});
this.homey.flow.getConditionCard('current_price_among_x_highest_next_y_hours')
.registerRunListener((args, state) => {
state.high_price = true;
return args.device.priceComparer.currentPriceAmongNextHoursComparer(args, state);
});
this.homey.flow.getConditionCard('current_price_among_x_lowest_next_y_hours')
.registerRunListener((args, state) => {
state.high_price = false;
return args.device.priceComparer.currentPriceAmongNextHoursComparer(args, state);
});
this.homey.flow.getConditionCard('high_x_hours_of_day_condition')
.registerRunListener((args, state) => {
state.high_price = true;
return args.device.priceComparer.highHoursComparer(args, state);
});
this.homey.flow.getConditionCard('low_x_hours_of_day_condition')
.registerRunListener((args, state) => {
state.low_price = true;
return args.device.priceComparer.lowHoursComparer(args, state);
});
this.homey.flow.getConditionCard('price_below_avg_condition')
.registerRunListener((args, state) => {
state.below = true;
return args.device.priceComparer.priceAvgComparer(args, state);
});
this.homey.flow.getConditionCard('price_below_avg_next_hours_condition')
.registerRunListener((args, state) => {
state.below = true;
return args.device.priceComparer.priceAvgComparer(args, state);
});
this.homey.flow.getConditionCard('price_above_avg_condition')
.registerRunListener((args, state) => {
state.below = false;
return args.device.priceComparer.priceAvgComparer(args, state);
});
this.homey.flow.getConditionCard('price_above_avg_next_hours_condition')
.registerRunListener((args, state) => {
state.below = false;
return args.device.priceComparer.priceAvgComparer(args, state);
});
this.homey.flow.getConditionCard('prices_among_lowest_condition')
.registerRunListener((args, state) => args.device.priceComparer.priceAmongLowestComparer(args, state));
this.homey.flow.getConditionCard('prices_among_highest_condition')
.registerRunListener((args, state) => args.device.priceComparer.priceAmongHighestComparer(args, state));
this.homey.flow.getConditionCard('price_lowest_among_in_period')
.registerRunListener((args, state) => args.device.priceComparer.priceLowestInPeriodComparer(args, state));
this.homey.flow.getConditionCard('price_highest_among_in_period')
.registerRunListener((args, state) => args.device.priceComparer.priceHighestInPeriodComparer(args, state));
this.homey.flow.getConditionCard('price_lowest_among_in_period_2')
.registerRunListener((args, state) => args.device.priceComparer.priceLowestInPeriodComparer(args, state));
this.homey.flow.getConditionCard('price_highest_among_in_period_2')
.registerRunListener((args, state) => args.device.priceComparer.priceHighestInPeriodComparer(args, state));
this.homey.flow.getConditionCard('price_lower_next_hours')
.registerRunListener((args, state) => args.device.priceComparer.priceLowerNextHoursComparer(args, state));
this.homey.flow.getConditionCard('price_higher_next_hours')
.registerRunListener((args, state) => args.device.priceComparer.priceHigherNextHoursComparer(args, state));
this.homey.flow.getConditionCard('prices_lowest_next_hours')
.registerRunListener((args, state) => args.device.priceComparer.priceLowestNextHoursComparer(args, state));
this.homey.flow.getConditionCard('prices_highest_next_hours')
.registerRunListener((args, state) => args.device.priceComparer.priceHighestNextHoursComparer(args, state));
this.homey.flow.getConditionCard('prices_lowest_next_hours_2')
.registerRunListener((args, state) => args.device.priceComparer.priceLowestNextHoursComparer(args, state));
this.homey.flow.getConditionCard('prices_highest_next_hours_2')
.registerRunListener((args, state) => args.device.priceComparer.priceHighestNextHoursComparer(args, state));
this.homey.flow.getConditionCard('price_diff_high_low')
.registerRunListener((args, state) => args.device.priceComparer.priceDiffHighLowComparer(args, state));
this.homey.flow.getConditionCard('price_diff_high_low2')
.registerRunListener((args, state) => args.device.priceComparer.priceDiffHighLowComparer2(args, state));
this.homey.flow.getActionCard('set_at_home_on')
.registerRunListener((args, state) => args.device.onActionSetAtHomeOn());
this.homey.flow.getActionCard('set_at_home_off')
.registerRunListener((args, state) => args.device.onActionSetAtHomeOff());
this.homey.flow.getActionCard('set_at_home_off_auto')
.registerRunListener((args, state) => args.device.onActionSetAtHomeOffAuto());
this.homey.flow.getActionCard('set_home_override_on')
.registerRunListener((args, state) => args.device.onActionSetHomeOverrideOn());
this.homey.flow.getActionCard('set_home_override_on_auto')
.registerRunListener((args, state) => args.device.onActionSetHomeOverrideOnAuto());
this.homey.flow.getActionCard('set_home_override_off')
.registerRunListener((args, state) => args.device.onActionSetHomeOverrideOff());
this.homey.flow.getActionCard('set_holiday_today')
.registerRunListener((args, state) => args.device.onActionSetHolidayToday(args));
this.homey.flow.getActionCard('clear_holiday_today')
.registerRunListener((args, state) => args.device.onActionClearHolidayToday());
this.homey.flow.getActionCard('fetch_prices')
.registerRunListener((args, state) => args.device.onFetchPrices());
}
check(args: any, types: any) {
if (!args.country || !args.condition) {
return false;
}
let hd;
try {
hd = holidays.isHoliday(args.country, moment().toDate(), args.condition);
} catch (err) {
console.error(err);
}
if (hd == false) {
return false;
}
const hdd = hd as Holiday;
return hdd.type in types;
}
}