-
Notifications
You must be signed in to change notification settings - Fork 1
/
netatmo.py
executable file
·851 lines (722 loc) · 30.6 KB
/
netatmo.py
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#!/usr/bin/env python3
import os
import json
import logging
from io import BytesIO
from platform import python_version_tuple
from getpass import getpass
from time import time
from pwd import getpwall
import requests
__version__ = '0.1.1'
logger = logging.getLogger('netatmo')
logging.basicConfig(format='[*] %(levelname)s : %(module)s : %(message)s', level=getattr(logging, 'WARNING'))
PY_VERSION = [int(i) for i in python_version_tuple()]
if PY_VERSION[0] != 3 and PY_VERSION[1] < 4:
raise RuntimeError('Python 3.4 or higher is required. Aborted')
#######################
# MODULE EXCEPTIONS #
#######################
class NetatmoError(Exception):
def __init__(self, message=None):
if message:
Exception.__init__(self, message)
else:
Exception.__init__(self)
class APIError(NetatmoError):
def __init__(self, message=None):
NetatmoError.__init__(self, message)
class ScopeError(NetatmoError):
def __init__(self, scope):
self.message = 'Could not find \'' + scope + '\' in your scope'
NetatmoError.__init__(self, self.message)
class ConfigError(NetatmoError):
def __init__(self, error):
if error == 'file':
self.message = 'Could not find .pynetatmo.conf in your home directory'
elif error == 'key':
self.message = 'Your configuration file appears to be invalid. Please check {home}.pynetatmo.conf'.format(home=HOME)
else:
self.message = None
NetatmoError.__init__(self, self.message)
###################
# CONFIGURATION #
###################
for p in getpwall():
HOME = p.pw_dir
try:
CONF = None
with open(os.path.join(HOME, '.pynetatmo.conf'), 'r') as f:
CONF = json.load(f)
logger.debug('Configuration loaded')
break
except:
pass
if not CONF:
HOME = os.getenv('HOME') + '/'
configure = input('Configuration file not found.\nWould you like to be guided through the configuration steps (otherwise you will have to create the JSON file on your own)? [y/n] ')
if configure.upper() == 'Y':
with open(os.path.join(HOME, '.pynetatmo.conf'), 'w') as f:
try:
CONF = dict()
CONF['user'] = input('User: ')
CONF['password'] = getpass()
CONF['client_id'] = input('Client ID: ')
CONF['client_secret'] = input('Client Secret: ')
CONF['scope'] = input('Scope: ')
json.dump(CONF, f, indent=4)
logger.debug('Configuration file created')
except KeyboardInterrupt:
os.remove(os.path.join(HOME, '.pynetatmo.conf'))
logger.error('\nAborted')
exit(1)
else:
logger.error('You can\'t use this module without a configuration file. Aborted')
exit(1)
########################
# NETATMO BASE CLASS #
########################
class Netatmo(object):
def __init__(self, log_level):
logging.basicConfig(format='[*] %(levelname)s : %(module)s : %(message)s', level=getattr(logging, log_level))
self.__BASE_URL = 'https://api.netatmo.com'
self.__access_token = None
self.__refresh_token = None
self.__expires_in = None
self.__timestamp = None
self._auth()
logger.debug('Netatmo.__init__ completed')
@property
def access_token(self):
return self.__access_token
@property
def refresh_token(self):
return self.__refresh_token
@property
def scope(self):
return self.__scope
def __str__(self):
string = '••Netatmo Object••\n\n'
for k in self.__dict__:
string += k + ' :: ' + str(self.__dict__[k]) + '\n'
return string
def _api_call(self, resource, payload):
logger.debug('API call : %s : %s', resource, payload)
try:
response = requests.post(self.__BASE_URL + resource, data=payload)
response.raise_for_status()
try:
return response.json()
except:
return response.text
except requests.exceptions.HTTPError as error:
raise APIError(error.response.text)
def _auth(self):
logger.debug('Authorizing...')
try:
payload = {
'grant_type': 'password',
'username': CONF['user'],
'password': CONF['password'],
'client_id': CONF['client_id'],
'client_secret': CONF['client_secret'],
'scope': CONF['scope']
}
data = self._api_call('/oauth2/token', payload)
self.__timestamp = time()
self.__expires_in = data['expires_in']
self.__access_token = data['access_token']
self.__refresh_token = data['refresh_token']
self.__scope = data['scope']
logger.debug('Your access token is: ' + self.__access_token)
logger.debug('Your refresh token is: ' + self.__refresh_token)
logger.debug('Your scopes are: ' + str(self.__scope))
logger.debug('Authorization completed')
except KeyError:
raise ConfigError('key')
def _refresh(self):
logger.debug('Refreshing...')
try:
payload = {
'grant_type': 'refresh_token',
'refresh_token': self.__refresh_token,
'client_id': CONF['client_id'],
'client_secret': CONF['client_secret']
}
data = self._api_call('/oauth2/token', payload)
self.__timestamp = time()
self.__access_token = data['access_token']
self.__refresh_token = data['refresh_token']
self.__expires_in = data['expires_in']
except KeyError:
raise ConfigError('key')
def _check_token_validity(self):
if time() - self.__timestamp < self.__expires_in:
self._refresh()
####################
# THERMOSTAT API #
####################
class Thermostat(Netatmo):
def __init__(self, device_id, log_level='WARNING'):
Netatmo.__init__(self, log_level)
self.__class_scope = ['read_thermostat', 'write_thermostat']
for scope in self.__class_scope:
if scope not in self.scope:
raise ScopeError(scope)
self.__device_id = device_id
self.__cache = None
self.__cache_timestamp = None
self.__CACHE_VALIDITY = 600 # seconds = 10 minutes
self.get_thermostats_data()
logger.debug('Thermostat.__init__ completed')
@property
def device_id(self):
return self.__device_id
@property
def temperature(self):
return self.get_thermostats_data()['devices'][0]['modules'][0]['measured']['temperature']
@property
def set_temperature(self):
return self.get_thermostats_data()['devices'][0]['modules'][0]['measured']['setpoint_temp']
@property
def relay_cmd(self):
return self.get_thermostats_data()['devices'][0]['modules'][0]['therm_relay_cmd']
def __str__(self):
string = '••Netatmo Thermostat Object••\n\n'
for k in self.__dict__:
string += k + ' :: ' + str(self.__dict__[k]) + '\n'
return string
def get_thermostats_data(self):
logger.debug('Checking cache...')
if self.__cache and time() - self.__cache_timestamp < self.__CACHE_VALIDITY:
return self.__cache
logger.debug('Cache is invalid: getting thermostat data from the api...')
self._check_token_validity()
payload = {
'access_token': self.access_token,
'device_id': self.device_id
}
data = self._api_call('/api/getthermostatsdata', payload)['body']
self.__cache = data
self.__cache_timestamp = time()
return data
def get_module_ids(self):
logger.debug('Getting modules\' id...')
modules_ids = list()
for device in self.get_thermostats_data()['devices']:
if device['_id'] == self.device_id:
for module in device['modules']:
modules_ids.append(module['_id'])
return modules_ids
def set_therm_point(self, module_id, setpoint_mode, setpoint_endtime=None, setpoint_temp=None):
logger.debug('Setting thermal point...')
allowed_setpoint_modes = ['program', 'away', 'hg', 'manual', 'off', 'max']
self._check_token_validity()
payload = {
'access_token': self.access_token,
'device_id': self.device_id,
'module_id': module_id,
'setpoint_mode': setpoint_mode,
}
if setpoint_endtime:
payload['setpoint_endtime'] = setpoint_endtime
if setpoint_temp:
payload['setpoint_temp'] = setpoint_temp
if setpoint_mode in allowed_setpoint_modes:
return self._api_call('/api/setthermpoint', payload)
logger.error('Invalid choice for setpoint_mode. Choose from ' + str(allowed_setpoint_modes))
return False
def switch_schedule(self, module_id, schedule_id):
logger.debug('Switching schedule...')
self._check_token_validity()
payload = {
'access_token': self.access_token,
'device_id': self.device_id,
'module_id': module_id,
'schedule_id': schedule_id
}
return self._api_call('/api/switchschedule', payload)
def create_new_schedule(self, module_id, zones, timetable, name):
logger.debug('Creating new schedule...')
self._check_token_validity()
payload = {
'access-token': self.access_token,
'device_id': self.device_id,
'module_id': module_id,
'zones': zones,
'timetable': timetable,
'name': name
}
return self._api_call('/api/createnewschedule', payload)
def sync_schedule(self, module_id, zones, timetable):
logger.debug('Creating new schedule...')
self._check_token_validity()
payload = {
'access-token': self.access_token,
'device_id': self.device_id,
'module_id': module_id,
'zones': zones,
'timetable': timetable
}
return self._api_call('/api/syncschedule', payload)
#################
# WEATHER API #
#################
class Weather(Netatmo):
def __init__(self, device_id=None, get_favorites=False, log_level='WARNING'):
Netatmo.__init__(self, log_level)
self.__class_scope = ['read_station']
for scope in self.__class_scope:
if scope not in self.scope:
raise ScopeError(scope)
self.__device_id = device_id
self.get_favorites = get_favorites
self.__stations = None
self.__cache = None
self.__cache_timestamp = None
self.__CACHE_VALIDITY = 600 # seconds = 10 minutes
logger.debug('Weather.__init__ completed')
@property
def stations(self):
if not self.__stations:
self.__stations = [self.Station(self, device) for device in self.get_stations_data()['body']['devices']]
return self.__stations
@property
def my_station(self):
for station in self.stations:
if station.id == self.device_id:
return station
return None
@property
def device_id(self):
return self.__device_id
def __str__(self):
string = '••Netatmo Weather Object••\n\n'
for k in self.__dict__:
string += k + ' :: ' + str(self.__dict__[k]) + '\n'
return string
def get_stations_data(self):
logger.debug('Checking cache...')
if self.__cache and time() - self.__cache_timestamp < self.__CACHE_VALIDITY:
return self.__cache
logger.debug('Cache is invalid: getting stations data from the api...')
self._check_token_validity()
payload = {
'access_token': self.access_token,
'get_favorites': str(self.get_favorites).lower()
}
if self.device_id:
payload['device_id'] = self.device_id
data = self._api_call('/api/getstationsdata', payload)
self.__cache = data
self.__cache_timestamp = time()
return data
def get_station_from_id(self, ID):
for device in self.stations:
if device.id == ID:
return device
return None
def get_stations_from_name(self, name):
stations = dict()
for device in self.stations:
if device.name == name:
stations[name] = device
if not stations:
return None
elif len(stations) == 1:
return stations[name]
return stations
class Station(object):
def __init__(self, weather, raw_data):
self.__weather = weather # Weather class that created the current Station instance
self.__raw_data = raw_data
self.__name = raw_data['station_name']
self.__id = raw_data['_id']
self.__data_type = list(set([t for module in raw_data['modules'] for t in module['data_type']] + raw_data['data_type']))
self.__data_type.sort()
self.__modules = raw_data['modules']
logger.debug('Station.__init__ completed')
@property
def name(self):
return self.__name
@property
def id(self):
return self.__id
@property
def data_type(self):
return self.__data_type
@property
def modules(self):
m = list(set([module['module_name'] for module in self.__raw_data['modules']] + [self.__raw_data['module_name']]))
m.sort()
return m
@property
def temperature(self):
if 'Temperature' in self.data_type:
if self.refresh():
temperatures = dict()
if 'Temperature' in self.__raw_data['dashboard_data']:
if 'module_name' in self.__raw_data:
module_name = self.__raw_data['module_name']
else:
module_name = self.name
temperatures[module_name] = self.__raw_data['dashboard_data']['Temperature']
for module in self.__modules:
if 'Temperature' in module['dashboard_data']:
if 'module_name' in module:
module_name = module['module_name']
else:
module_name = self.name
temperatures[module_name] = module['dashboard_data']['Temperature']
if temperatures:
return temperatures
return None
@property
def humidity(self):
if 'Humidity' in self.data_type:
if self.refresh():
humidities = dict()
if 'Humidity' in self.__raw_data['dashboard_data']:
if 'module_name' in self.__raw_data:
module_name = self.__raw_data['module_name']
else:
module_name = self.name
humidities[module_name] = self.__raw_data['dashboard_data']['Humidity']
for module in self.__modules:
if 'Humidity' in module['dashboard_data']:
if 'module_name' in module:
module_name = module['module_name']
else:
module_name = self.name
humidities[module_name] = module['dashboard_data']['Humidity']
if humidities:
return humidities
return None
@property
def pressure(self):
if 'Pressure' in self.data_type:
if self.refresh():
pressures = dict()
if 'Pressure' in self.__raw_data['dashboard_data']:
if 'module_name' in self.__raw_data:
module_name = self.__raw_data['module_name']
else:
module_name = self.name
pressures[module_name] = self.__raw_data['dashboard_data']['Pressure']
for module in self.__modules:
if 'Pressure' in module['dashboard_data']:
if 'module_name' in module:
module_name = module['module_name']
else:
module_name = self.name
pressures[module_name] = module['dashboard_data']['Pressure']
if pressures:
return pressures
return None
@property
def noise(self):
if 'Noise' in self.data_type:
if self.refresh():
noises = dict()
if 'Noise' in self.__raw_data['dashboard_data']:
if 'module_name' in self.__raw_data:
module_name = self.__raw_data['module_name']
else:
module_name = self.name
noises[module_name] = self.__raw_data['dashboard_data']['Noise']
for module in self.__modules:
if 'Noise' in module['dashboard_data']:
if 'module_name' in module:
module_name = module['module_name']
else:
module_name = self.name
noises[module_name] = module['dashboard_data']['Noise']
if noises:
return noises
return None
@property
def co2(self):
if 'CO2' in self.data_type:
if self.refresh():
co2s = dict()
if 'CO2' in self.__raw_data['dashboard_data']:
if 'module_name' in self.__raw_data:
module_name = self.__raw_data['module_name']
else:
module_name = self.name
co2s[module_name] = self.__raw_data['dashboard_data']['CO2']
for module in self.__modules:
if 'CO2' in module['dashboard_data']:
if 'module_name' in module:
module_name = module['module_name']
else:
module_name = self.name
co2s[module_name] = module['dashboard_data']['CO2']
if co2s:
return co2s
return None
@property
def rain(self):
if 'Rain' in self.data_type:
if self.refresh():
rains = dict()
if 'Rain' in self.__raw_data['dashboard_data']:
if 'module_name' in self.__raw_data:
module_name = self.__raw_data['module_name']
else:
module_name = self.name
rains[module_name] = self.__raw_data['dashboard_data']['Rain']
for module in self.__modules:
if 'Rain' in module['dashboard_data']:
if 'module_name' in module:
module_name = module['module_name']
else:
module_name = self.name
rains[module_name] = module['dashboard_data']['Rain']
if rains:
return rains
return None
@property
def wind_strength(self):
if 'Wind' in self.data_type:
if self.refresh():
winds = dict()
if 'WindStrength' in self.__raw_data['dashboard_data']:
if 'module_name' in self.__raw_data:
module_name = self.__raw_data['module_name']
else:
module_name = self.name
winds[module_name] = self.__raw_data['dashboard_data']['WindStrength']
for module in self.__modules:
if 'WindStrength' in module['dashboard_data']:
if 'module_name' in module:
module_name = module['module_name']
else:
module_name = self.name
winds[module_name] = module['dashboard_data']['WindStrength']
if winds:
return winds
return None
@property
def wind_angle(self):
if 'Wind' in self.data_type:
if self.refresh():
winds = dict()
if 'WindAngle' in self.__raw_data['dashboard_data']:
if 'module_name' in self.__raw_data:
module_name = self.__raw_data['module_name']
else:
module_name = self.name
winds[module_name] = self.__raw_data['dashboard_data']['WindAngle']
for module in self.__modules:
if 'WindAngle' in module['dashboard_data']:
if 'module_name' in module:
module_name = module['module_name']
else:
module_name = self.name
winds[module_name] = module['dashboard_data']['WindAngle']
if winds:
return winds
return None
def __str__(self):
string = '••Netatmo Weather.Station Object••\n\n'
for k in self.__dict__:
if k != 'raw_data':
string += k + ' :: ' + str(self.__dict__[k]) + '\n'
return string
def refresh(self):
name = self.name
weather = self.__weather
for device in self.__weather.get_stations_data()['body']['devices']:
if device['station_name'] == name:
self.__init__(weather, device)
return True
return False
##################
# SECURITY API #
##################
class Security(Netatmo):
class _NoDevice(NetatmoError):
def __init__(self, message=None):
NetatmoError.__init__(self, message)
class Camera(object):
def __init__(self, source_dictionary):
self.__dict__.update(source_dictionary)
def __str__(self):
string = '••Netatmo Secutiry.Camera Object••\n\n'
for k in self.__dict__:
string += k + ' :: ' + str(self.__dict__[k]) + '\n'
return string
class Person(object):
def __init__(self, source_dictionary):
self.__dict__.update(source_dictionary)
def __str__(self):
string = '••Netatmo Security.Person Object••\n\n'
for k in self.__dict__:
string += k + ' :: ' + str(self.__dict__[k]) + '\n'
return string
class Event(object):
def __init__(self, source_dictionary):
self.__dict__.update(source_dictionary)
def __str__(self):
string = '••Netatmo Security.Event Object••\n\n'
for k in self.__dict__:
string += k + ' :: ' + str(self.__dict__[k]) + '\n'
return string
@property
def cameras(self):
return self.get_cameras()
@property
def events(self):
return self.get_events()
@property
def persons(self):
return self.get_persons()
def __init__(self, name, log_level='WARNING'):
from PIL import Image
Netatmo.__init__(self, log_level)
self.__class_scope = ['read_camera', 'access_camera', 'write_camera']
for scope in self.__class_scope:
if scope not in self.scope:
raise ScopeError(scope)
self.name = name
self.home_id, self.place = self._get_home_info()
def __str__(self):
string = '••Netatmo Security Object••\n\n'
for k in self.__dict__:
string += k + ' :: ' + str(self.__dict__[k]) + '\n'
return string
def _get_home_info(self):
logger.debug('Getting home info (home_id, place)...')
data = self.get_home_data()
return (data['id'], data['place'])
def get_home_data(self, size=15, home_id=None):
logger.debug('Getting home data...')
self._check_token_validity()
params = {
'access_token': self.access_token,
'home_id': home_id,
'size': size
}
try:
response = requests.post('https://api.netatmo.com/api/gethomedata', params=params)
response.raise_for_status()
data = response.json()['body']['homes']
logger.debug('Request completed')
response.connection.close()
data = [h for h in data if h['name'] == self.name]
if not data:
raise self._NoDevice('No device with the name provided')
return data[0]
except requests.exceptions.HTTPError as error:
raise APIError(error.response.text)
def get_cameras(self):
return [self.Camera(c) for c in self.get_home_data()['cameras']]
def get_events(self, numbers_of_events=15):
return [self.Event(e) for e in self.get_home_data(numbers_of_events)['events']]
def get_persons(self, name=None, pseudo=False):
#if type(pseudo) != bool:
if not isinstance(pseudo, bool):
raise TypeError('\'pseudo\' must be a boolean value')
if name != None:
#if type(name) != str:
if not isinstance(name, str):
raise TypeError('\'name\' must be a string')
return [self.Person(c) for c in self.get_home_data()['persons'] if 'pseudo' in c.keys() and c['pseudo'] == name][0]
if pseudo:
return [self.Person(c) for c in self.get_home_data()['persons'] if 'pseudo' in c.keys()]
return [self.Person(c) for c in self.get_home_data()['persons']]
def get_camera_picture(self, event, show=False):
#if type(event) == Security.Event:
if isinstance(event, Security.Event):
if event.type not in ['movement', 'person']:
raise TypeError('The input event must be a movement or a \'person seen event\'. Only these have related screenshots')
#if type(event) not in [Security.Event, Security.Person]:
if not isinstance(event, (Security.Event, Security.Person)):
raise TypeError('The input must be an event or a person object')
logger.debug('Getting event related image...')
try:
self._check_token_validity()
base_url = 'https://api.netatmo.com/api/getcamerapicture?'
try:
url = base_url + 'image_id=' + str(event.snapshot['id']) + '&key=' + str(event.snapshot['key'])
except AttributeError:
url = base_url + 'image_id=' + str(event.face['id']) + '&key=' + str(event.face['key'])
response = requests.get(url)
response.raise_for_status()
img = Image.open(BytesIO(response.content))
response.connection.close()
logger.debug('Request completed')
if show:
img.show()
return img
except requests.exceptions.HTTPError as error:
raise APIError(error.response.text)
def get_events_until(self, event):
#if type(event) is not Security.Event:
if not isinstance(event, Security.Event):
raise TypeError('Input must be an event obj')
logger.debug('Getting events...')
self._check_token_validity()
params = {
'access_token': self.access_token,
'home_id': self.home_id,
'event_id': event.id
}
try:
response = requests.post('https://api.netatmo.com/api/geteventsuntil', params=params)
response.raise_for_status()
data = response.json()['body']['events_list']
response.connection.close()
return [self.Event(e) for e in data]
except requests.exceptions.HTTPError as error:
raise APIError(error.response.text)
def set_person_away(self, person=None):
#if type(person) not in [Security.Person, None]:
if not isinstance(person, (Security.Person, None)):
raise TypeError('The input must be a Security.Person object or None if you want to set all people away')
self._check_token_validity()
params = {
'access_token': self.access_token,
'home_id': self.home_id
}
if person != None:
params['person_id'] = person.id
logger.debug('Setting person status...')
try:
response = requests.post('https://api.netatmo.com/api/setpersonsaway', params=params)
response.raise_for_status()
data = response.text
response.connection.close()
return data
except requests.exceptions.HTTPError as error:
raise APIError(error.response.text)
def Addwebhook(self, url):
self._check_token_validity()
params = {
'access_token': self.access_token,
'url': url,
'app_types': 'app_security'
}
try:
response = requests.post('https://api.netatmo.com/api/addwebhook', params=params)
response.raise_for_status()
data = response.text
response.connection.close()
print(data)
except requests.exceptions.HTTPError as error:
raise APIError(error.response.text)
def Dropwebhook(self):
self._check_token_validity()
params = {
'access_token': self.access_token,
'app_types': 'app_security'
}
try:
response = requests.post('https://api.netatmo.com/api/dropwebhook', params=params)
response.raise_for_status()
data = response.text
response.connection.close()
print(data)
except requests.exceptions.HTTPError as error:
raise APIError(error.response.text)