forked from jabesq/netatmo-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
243 lines (212 loc) · 9.1 KB
/
Copy pathauth.py
File metadata and controls
243 lines (212 loc) · 9.1 KB
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
import logging
from time import sleep
from typing import Callable, Dict, Optional, Tuple, Union
import requests
from oauthlib.oauth2 import LegacyApplicationClient, TokenExpiredError
from requests_oauthlib import OAuth2Session
from .exceptions import ApiError
from .helpers import _BASE_URL, ERRORS
LOG = logging.getLogger(__name__)
# Common definitions
_AUTH_REQ = _BASE_URL + "oauth2/token"
_AUTH_URL = _BASE_URL + "oauth2/authorize"
_WEBHOOK_URL_ADD = _BASE_URL + "api/addwebhook"
_WEBHOOK_URL_DROP = _BASE_URL + "api/dropwebhook"
# Possible scops
ALL_SCOPES = [
"read_station",
"read_camera",
"access_camera",
"write_camera",
"read_presence",
"access_presence",
"write_presence",
"read_homecoach",
"read_smokedetector",
"read_thermostat",
"write_thermostat",
]
class NetatmOAuth2:
"""
Handle authentication with OAuth2
:param client_id: Application client ID delivered by Netatmo on dev.netatmo.com
:param client_secret: Application client secret delivered by Netatmo on dev.netatmo.com
:param redirect_uri: Redirect URI where to the authorization server will redirect with an authorization code
:param token: Authorization token
:param token_updater: Callback when the token is updated
:param scope:
read_station: to retrieve weather station data (Getstationsdata, Getmeasure)
read_camera: to retrieve Welcome data (Gethomedata, Getcamerapicture)
access_camera: to access the camera, the videos and the live stream
write_camera: to set home/away status of persons (Setpersonsaway, Setpersonshome)
read_thermostat: to retrieve thermostat data (Getmeasure, Getthermostatsdata)
write_thermostat: to set up the thermostat (Syncschedule, Setthermpoint)
read_presence: to retrieve Presence data (Gethomedata, Getcamerapicture)
access_presence: to access the live stream, any video stored on the SD card and to retrieve Presence's lightflood status
read_homecoach: to retrieve Home Coache data (Gethomecoachsdata)
read_smokedetector: to retrieve the smoke detector status (Gethomedata)
Several value can be used at the same time, ie: 'read_station read_camera'
"""
def __init__(
self,
client_id: str = None,
client_secret: str = None,
redirect_uri: Optional[str] = None,
token: Optional[Dict[str, str]] = None,
token_updater: Optional[Callable[[str], None]] = None,
scope: Optional[str] = "read_station",
):
self.client_id = client_id
self.client_secret = client_secret
self.redirect_uri = redirect_uri
self.token_updater = token_updater
if token:
self.scope = " ".join(token["scope"])
else:
self.scope = " ".join(ALL_SCOPES) if not scope else scope
self.extra = {
"client_id": self.client_id,
"client_secret": self.client_secret,
}
self._oauth = OAuth2Session(
client_id=self.client_id,
token=token,
token_updater=self.token_updater,
redirect_uri=self.redirect_uri,
scope=self.scope,
)
def refresh_tokens(self) -> Dict[str, Union[str, int]]:
"""Refresh and return new tokens."""
token = self._oauth.refresh_token(_AUTH_REQ, **self.extra)
if self.token_updater is not None:
self.token_updater(token)
return token
def post_request(
self, url: str, params: Optional[Dict[str, str]] = None, timeout: int = 5
):
"""Wrapper for post requests."""
if not params:
params = {}
if "json" in params:
json_params = params.pop("json")
else:
json_params = None
if "http://" in url:
try:
resp = requests.post(url, data=params, timeout=timeout)
except requests.exceptions.ChunkedEncodingError:
LOG.debug("Encoding error when connecting to '%s'", url)
else:
def query(url, params, timeout, retries):
if retries == 0:
LOG.error("Too many retries")
return
try:
if json_params:
rsp = self._oauth.post(
url=url, json=json_params, timeout=timeout
)
else:
rsp = self._oauth.post(url=url, data=params, timeout=timeout)
return rsp
except (
TokenExpiredError,
requests.exceptions.ReadTimeout,
requests.exceptions.ConnectionError,
):
self._oauth.token = self.refresh_tokens()
# Sleep for 1 sec to prevent authentication related
# timeouts after a token refresh.
sleep(1)
return query(url, params, timeout, retries - 1)
resp = query(url, params, timeout, 3)
if resp is None:
LOG.debug("Resp is None - %s", resp)
elif not resp.ok:
LOG.debug("The Netatmo API returned %s", resp.status_code)
LOG.debug("Netato API error: %s", resp.content)
if resp.status_code == 404:
raise ApiError(
f"{resp.status_code} - "
f"{ERRORS[resp.status_code]} - "
f"when accessing '{url}'"
)
else:
raise ApiError(
f"{resp.status_code} - "
f"{ERRORS[resp.status_code]} - "
f"{resp.json()['error']['message']} "
f"({resp.json()['error']['code']}) "
f"when accessing '{url}'"
)
try:
return (
resp.json()
if "application/json" in resp.headers.get("content-type")
else resp.content
)
except TypeError:
LOG.debug("Invalid response %s", resp)
return None
def get_authorization_url(self, state: Optional[str] = None) -> Tuple[str, str]:
return self._oauth.authorization_url(_AUTH_URL, state)
def request_token(
self, authorization_response: Optional[str] = None, code: Optional[str] = None
) -> Dict[str, str]:
"""
Generic method for fetching a Netatmo access token.
:param authorization_response: Authorization response URL, the callback
URL of the request back to you.
:param code: Authorization code
:return: A token dict
"""
return self._oauth.fetch_token(
_AUTH_REQ,
authorization_response=authorization_response,
code=code,
client_secret=self.client_secret,
include_client_id=True,
)
def addwebhook(self, webhook_url):
postParams = {"url": webhook_url}
resp = self.post_request(_WEBHOOK_URL_ADD, postParams)
LOG.debug("addwebhook: %s", resp)
def dropwebhook(self):
postParams = {"app_types": "app_security"}
resp = self.post_request(_WEBHOOK_URL_DROP, postParams)
LOG.debug("dropwebhook: %s", resp)
class ClientAuth(NetatmOAuth2):
"""
Request authentication and keep access token available through token method. Renew it automatically if necessary
Args:
clientId (str): Application clientId delivered by Netatmo on dev.netatmo.com
clientSecret (str): Application Secret key delivered by Netatmo on dev.netatmo.com
username (str)
password (str)
scope (Optional[str]):
read_station: to retrieve weather station data (Getstationsdata, Getmeasure)
read_camera: to retrieve Welcome data (Gethomedata, Getcamerapicture)
access_camera: to access the camera, the videos and the live stream
write_camera: to set home/away status of persons (Setpersonsaway, Setpersonshome)
read_thermostat: to retrieve thermostat data (Getmeasure, Getthermostatsdata)
write_thermostat: to set up the thermostat (Syncschedule, Setthermpoint)
read_presence: to retrieve Presence data (Gethomedata, Getcamerapicture)
access_presence: to access the live stream, any video stored on the SD card and to retrieve Presence's lightflood status
read_homecoach: to retrieve Home Coache data (Gethomecoachsdata)
read_smokedetector: to retrieve the smoke detector status (Gethomedata)
Several value can be used at the same time, ie: 'read_station read_camera'
"""
def __init__(
self, clientId, clientSecret, username, password, scope="read_station"
):
self._clientId = clientId
self._clientSecret = clientSecret
self._oauth = OAuth2Session(client=LegacyApplicationClient(client_id=clientId))
self._oauth.fetch_token(
token_url=_AUTH_REQ,
username=username,
password=password,
client_id=clientId,
client_secret=clientSecret,
scope=scope,
)