-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetatmo.pm
456 lines (382 loc) · 11.6 KB
/
Netatmo.pm
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
##########################################################################################
# Netatmo.pm
# API for access your Netatmo data at https://dev.netatmo.com
#
# Soren Juul Moller, Jan 2020.
package NetatmoUtils;
use strict;
use warnings;
use JSON;
sub load_json($) {
my ($jsonfile) = @_;
if (open(my $fd, '<', $jsonfile)) {
local $/;
my $hash = from_json(<$fd>);
close($fd);
return $hash;
}
return undef;
}
sub store_json($$) {
my ($jsonfile, $hash) = @_;
if (open(my $fd, '>', $jsonfile)) {
print $fd to_json($hash, { utf8 => 1, pretty => 1});
close($fd);
return $hash;
}
return undef;
}
##########################################################################################
# Netatmo Connection
##########################################################################################
package NetatmoConnection;
use strict;
use warnings;
use YAML ();
use JSON;
use URI;
use LWP::UserAgent;
use Carp;
my $API = "https://api.netatmo.net";
#
# Establish session
# Login using values from configuration file or arguments
#
sub new {
my ($class, %args) = @_;
my $self = {};
# Defaults
$self->{cachedir} = '/var/run/netatmo';
my $conffile = '/usr/local/etc/netatmo.conf';
# Load config file if any
$conffile = $args{conffile} if defined $args{conffile};
if (-f $conffile) {
my $conf = YAML::LoadFile($conffile);
$self->{$_} = $conf->{$_} foreach keys %$conf;
}
# Override with args
$self->{$_} = $args{$_} foreach keys %args;
foreach (qw(client_id client_secret username password)) {
croak("$_ is not defined") unless defined $self->{$_};
}
mkdir($self->{cachedir}) unless -d $self->{cachedir};
# Create HTTP client
$self->{'_ua'} = LWP::UserAgent->new(
agent => "check_netatmo/0.5",
ssl_opts => { verify_hostname => 0 }
);
$self->{_ua}->default_header('accept' => 'application/json');
# Get a access token (possibly cached)
my $access_token = get_token($self);
if (defined $access_token) {
$self->{access_token} = $access_token;
$self->{_ua}->default_header('Authorization' => 'Bearer '.$access_token);
} else {
delete $self->{access_token};
}
bless $self, $class;
}
# Do a cached login.
# Returns access_token on success, undef on failire.
#
sub get_token {
my ($self) = @_;
my $content;
# Load cache if it exists
my $token = NetatmoUtils::load_json($self->{cachedir}.'/token.json');
# If we got a refresh_token
if (defined $token->{refresh_token}) {
# Return token if not exipred
return $token->{access_token} if ($token->{timestamp} + $token->{expires_in} - $self->{cachettl} > time());
# else try refresh token
my $res = $self->{_ua}->post(
"$API/oauth2/token",
[
grant_type => 'refresh_token',
client_id => $self->{client_id},
client_secret => $self->{client_secret},
refresh_token => $token->{refresh_token}
]
);
if ($res->is_success) {
$content = $res->decoded_content if $res->is_success;
} else {
$self->{error} = $res->status_line;
}
}
# else do a login
if (!defined $content) {
my $res = $self->{_ua}->post(
"$API/oauth2/token",
[
grant_type => 'password',
client_id => $self->{client_id},
client_secret => $self->{client_secret},
username => $self->{username},
password => $self->{password},
scope => 'read_station read_thermostat'
]
);
if ($res->is_success) {
$content = $res->decoded_content;
} else {
$self->{error} = $res->status_line;
}
}
return undef unless defined $content;
$token = from_json($content);
$token->{timestamp} = time();
NetatmoUtils::store_json($self->{cachedir}.'/token.json', $token);
return $token->{access_token};
}
sub getWeatherStation {
my ($self, %opts) = @_;
return NetatmoWeatherStation->new(connection => $self, %opts);
}
sub getEnergy {
my ($self) = @_;
return NetatmoEnergy->new(connection => $self);
}
##########################################################################################
# Netatmo Weather
##########################################################################################
package NetatmoWSmodule;
use strict;
use warnings;
use Carp;
sub new {
my ($class, %args) = @_;
croak "module must be specified" unless defined $args{module};
my $self = $args{module};
bless $self, $class;
}
sub dashboard_data { shift->{dashboard_data} }
######################################
package NetatmoWSdevice;
use strict;
use warnings;
use Carp;
sub new {
my ($class, %args) = @_;
croak "device must be specified" unless defined $args{device};
my $self = $args{device};
bless $self, $class;
}
sub data_type { @{shift->{data_type}} }
sub place { shift->{place} }
sub dashboard_data { shift->{dashboard_data} }
sub user { shift->{user} }
sub modules { map { NetatmoWSmodule->new(module => $_) } @{shift->{modules}} };
sub moduleByName($) {
my ($self, $name) = @_;
foreach (@{$self->{modules}}) {
return NetatmoWSmodule->new(module => $_) if $_->{module_name} eq $name;
}
return undef;
}
######################################
package NetatmoWeatherStation;
use strict;
use warnings;
use Carp;
use JSON;
use URI;
sub new {
my ($class, %args) = @_;
my $self = { map { $_ => $args{$_} } keys %args };
croak "Must specify connection" unless defined $args{connection};
my ($json, $cachefile);
if (defined $self->{connection}->{cachedir}) {
$cachefile = $self->{connection}->{cachedir}.'/weatherstation.json';
my $cachettl = $self->{connection}->{cachettl};
if (-s $cachefile) {
my $mtime = (stat($cachefile))[9];
if (time() - $mtime < $cachettl) {
$json = NetatmoUtils::load_json($cachefile);
}
}
}
if (!defined $json) {
$self->{_ua} = $self->{connection}->{_ua};
my $url = URI->new("$API/api/getstationsdata");
$url->query_form(get_favorites => $args{get_favorites}) if defined $args{get_favorites};
my $res = $self->{_ua}->get($url);
if ($res->is_success) {
$json = from_json($res->decoded_content);
NetatmoUtils::store_json($cachefile, $json) if defined $cachefile;
} else {
$self->{error} = $res->status_line;
}
}
if (defined $json) {
$self->{devices} = $json->{body}->{devices};
$self->{user} = $json->{body}->{user};
$self->{status} = $json->{status};
}
bless $self, $class;
}
sub devices {
my ($self) = @_;
return map { NetatmoWSdevice->new(device => $_) } @{$self->{devices}};
}
sub deviceByID($) {
my ($self, $value) = @_;
foreach (@{$self->{devices}}) {
return NetatmoWSdevice->new(device => $_) if $_->{'_id'} eq $value;
}
return undef;
}
sub status { shift->{status} }
##########################################################################################
# Netatmo Energy
##########################################################################################
package NetatmoEnergy;
use strict;
use warnings;
use Carp;
use JSON;
use URI;
sub new {
my ($class, %args) = @_;
my $self = { map { $_ => $args{$_} } keys %args };
croak "connection must be specified" unless defined $args{connection};
my ($cachefile, $json);
if (defined $self->{connection}->{cachedir}) {
$cachefile = $self->{connection}->{cachedir}.'/homesdata.json';
my $cachettl = $self->{connection}->{cachettl};
if (-s $cachefile) {
my $mtime = (stat($cachefile))[9];
if (time() - $mtime < $cachettl) {
$json = NetatmoUtils::load_json($cachefile);
}
}
}
if (!defined $json) {
my $url = URI->new("$API/api/homesdata");
my $res = $self->{connection}->{_ua}->get($url);
if ($res->is_success) {
$json = from_json($res->decoded_content);
NetatmoUtils::store_json($cachefile, $json) if defined $cachefile;
} else {
$self->{error} = $res->status_line;
}
}
if (defined $json) {
$self->{homes} = $json->{body}->{homes};
$self->{user} = $json->{body}->{user};
$self->{status} = $json->{status};
}
bless $self, $class;
}
sub homes {
my ($self) = @_;
map { NetatmoEnergyHome->new(home => $_, homes => $self) } @{$self->{homes}};
}
######################################
package NetatmoEnergyHome;
use strict;
use warnings;
use Carp;
sub new {
my ($class, %args) = @_;
croak "home must be specfied" unless defined $args{home};
croak "homes must be specfied" unless defined $args{homes};
my $self = $args{home};
$self->{homes} = $args{homes};
bless $self, $class;
}
sub rooms { my $s=shift; map { NetatmoEnergyRoom->new(room => $_, home => $s) } @{$s->{rooms}} }
sub modules { map { NetatmoEnergyModule->new(module => $_) } @{shift->{module}} }
sub therm_schedules { map { NetatmoEnergySchedule->new(schedule => $_) } @{shift->{therm_schedules}} }
sub schedules { map { NetatmoEnergySchedule->new(schedule => $_) } @{shift->{schedules}} }
######################################
package NetatmoEnergyRoom;
use strict;
use warnings;
use Carp;
use URI;
use JSON;
sub new {
my ($class, %args) = @_;
croak "room must be specified" unless defined $args{room};
croak "home must be specified" unless defined $args{home};
my $self = $args{room};
$self->{home} = $args{home};
bless $self, $class;
}
sub getroommeasure {
my ($self, %opts) = @_;
my ($cachefile, $json);
my $connection = $self->{home}->{homes}->{connection};
if (defined $connection->{cachedir}) {
$cachefile = $connection->{cachedir}.'/roommeasure-'.$self->{id}.'.json';
my $cachettl = $connection->{cachettl};
if (-s $cachefile) {
my $mtime = (stat($cachefile))[9];
if (time() - $mtime < $cachettl) {
$json = NetatmoUtils::load_json($cachefile);
}
}
}
if (!defined $json) {
my $ua = $connection->{_ua};
$opts{scale} = '1hour' unless defined $opts{scale};
$opts{type} = 'temperature,sp_temperature' unless defined $opts{type};
$opts{limit} = 24 unless defined $opts{limit};
my $url = URI->new("$API/api/getroommeasure");
$url->query_form(home_id => $self->{home}->{id}, room_id => $self->{id}, %opts);
my $res = $ua->get($url);
if ($res->is_success) {
$json = from_json($res->decoded_content);
NetatmoUtils::store_json($cachefile, $json) if defined $cachefile;
} else {
$self->{error} = $res->status_line;
return [];
}
}
my @temps = ();
foreach my $dataset (@{$json->{body}}) {
my $t = $dataset->{beg_time};
while (my $dstemps = shift(@{$dataset->{value}})) {
push(@temps, [$t, @{$dstemps}]);
$t += $dataset->{step_time};
}
}
return \@temps;
}
######################################
package NetatmoEnergyModule;
use strict;
use warnings;
use Carp;
sub new {
my ($class, %args) = @_;
croak "module must be specified" unless defined $args{module};
my $self = $args{module};
bless $self, $class;
}
package NetatmoEnergySchedule;
use strict;
use warnings;
use Carp;
sub new {
my ($class, %args) = @_;
croak "schedule must be specified" unless defined $args{schedule};
my $self = $args{schedule};
bless $self, $class;
}
sub timetable { @{shift->{timetable}} }
sub zones { @{shift->{zones}} }
1;