Skip to content

Commit 7bd29fc

Browse files
committed
[New] Allow for request on any Netatmo API
1 parent 73e5ea4 commit 7bd29fc

File tree

10 files changed

+40
-5
lines changed

10 files changed

+40
-5
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ If you are using a single account with a single home and single weather station,
1313
If you have multiple homes or were supplying a station name in some method calls, you will have to adapt your code :
1414
- to supply a home name when looking for data for most class initializers
1515
- to use the new station name set by Netatmo (which is not your previously set value)
16-
1716

1817
>BREAKING CHANGE: Netatmo seems no longer (july 2023) to allow grant_type "password", even for an app credentials that belong to the same account than the home. They have added the capability of creating access_token/refresh_token couple from the dev page (the location where app are created). As a consequence, the username/password credentials can no longer be used and you must replace them with a new parameter REFRESH_TOKEN that you will get from the web interface. To get this token, you are required to specify the scope you want to allow to this token. Select all that apply for your library use.
1918

lnetatmo.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,13 @@ class WelcomeData(HomeData):
732732
warnings.warn("The 'WelcomeData' class was renamed 'HomeData' to handle new Netatmo Home capabilities",
733733
DeprecationWarning )
734734
pass
735-
736735
# Utilities routines
737736

737+
def rawAPI(authData, url, parameters={}):
738+
fullUrl = _BASE_URL + "api/" + url
739+
parameters["access_token"] = authData.accessToken
740+
resp = postRequest("rawAPI", fullUrl, parameters)
741+
return resp["body"] if "body" in resp else None
738742

739743
def filter_home_data(rawData, home):
740744
if home:
@@ -755,6 +759,8 @@ def postRequest(topic, url, params=None, timeout=10):
755759
req = urllib.request.Request(url)
756760
if params:
757761
req.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
762+
if "access_token" in params:
763+
req.add_header("Authorization","Bearer %s" % params.pop("access_token"))
758764
params = urllib.parse.urlencode(params).encode('utf-8')
759765
try:
760766
resp = urllib.request.urlopen(req, params, timeout=timeout) if params else urllib.request.urlopen(req, timeout=timeout)
@@ -767,7 +773,9 @@ def postRequest(topic, url, params=None, timeout=10):
767773
else:
768774
if params:
769775
params = urlencode(params)
770-
headers = {"Content-Type" : "application/x-www-form-urlencoded;charset=utf-8"}
776+
headers = {"Content-Type" : "application/x-www-form-urlencoded;charset=utf-8"}
777+
if "access_token" in params:
778+
headers["Authorization"] = "Bearer %s" % params.pop("access_token")
771779
req = urllib2.Request(url=url, data=params, headers=headers) if params else urllib2.Request(url)
772780
try:
773781
resp = urllib2.urlopen(req, timeout=timeout)
File renamed without changes.
File renamed without changes.

samples/rawAPIsample.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python3
2+
3+
# 2023-07 : ph.larduinat@wanadoo.fr
4+
# Library 3.2.0
5+
6+
# Direct call of Netatmo API with authentication token
7+
# Return a dictionary of body of Netatmo response
8+
9+
import lnetatmo
10+
11+
authorization = lnetatmo.ClientAuth()
12+
rawData = lnetatmo.rawAPI(authorization, "gethomesdata")
13+
14+
print("Home name : home_id")
15+
for h in rawData["homes"]:
16+
print(f"{h['name']} : {h['id']}")
17+
18+
print("Radio communication strength by home")
19+
for h in rawData["homes"]:
20+
print(f"\nFor {h['name']}:")
21+
modules = lnetatmo.rawAPI(authorization, "homestatus", {"home_id" : h['id']})['home']
22+
if not 'modules' in modules:
23+
print("No modules available")
24+
else:
25+
print([(m['id'],m['rf_strength']) for m in modules['modules'] if 'rf_strength' in m])
File renamed without changes.
File renamed without changes.
File renamed without changes.

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name='lnetatmo',
7-
version='3.1.0',
7+
version='3.2.0',
88
classifiers=[
99
'Development Status :: 5 - Production/Stable',
1010
'Intended Audience :: Developers',
@@ -17,7 +17,7 @@
1717
scripts=[],
1818
data_files=[],
1919
url='https://github.com/philippelt/netatmo-api-python',
20-
download_url='https://github.com/philippelt/netatmo-api-python/archive/v3.1.0.tar.gz',
20+
download_url='https://github.com/philippelt/netatmo-api-python/archive/v3.2.0.tar.gz',
2121
license='GPL V3',
2222
description='Simple API to access Netatmo weather station data from any python script.'
2323
)

usage.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Python Netatmo API programmers guide
1515
1616
>2023-07-12, Breaking changes due to deprecation of grant_type "password" for ALL apps
1717
18+
>2023-07-24, Adding rawAPI call to direct access the netatmo API when no additional support is provided by the library
19+
1820
No additional library other than standard Python library is required.
1921

2022
Both Python V2.7x and V3.x.x are supported without change.
@@ -427,6 +429,7 @@ Methods :
427429
#### 4-5 Utilities functions ####
428430

429431

432+
* **rawAPI** (authentication, APIkeyword, parameters) : Direct call an APIkeyword from Netatmo and return a dictionary with the raw response the APIkeywork is the path without the / before as specified in the documentation (eg. "gethomesdata" or "homestatus")
430433
* **toTimeString** (timestamp) : Convert a Netatmo time stamp to a readable date/time format.
431434
* **toEpoch**( dateString) : Convert a date string (form YYYY-MM-DD_HH:MM:SS) to timestamp
432435
* **todayStamps**() : Return a couple of epoch time (start, end) for the current day

0 commit comments

Comments
 (0)