Skip to content

Commit f75c8db

Browse files
authored
Merge pull request #58 from AuroreVgn/master
Update lnetatmo.py - class HomeStatus for Home+ Control devices #57
2 parents 73e5ea4 + ff8f5e8 commit f75c8db

File tree

2 files changed

+103
-9
lines changed

2 files changed

+103
-9
lines changed

lnetatmo.py

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def getParameter(key, default):
8787
_GETHOMEDATA_REQ = _BASE_URL + "api/gethomedata"
8888
_GETCAMERAPICTURE_REQ = _BASE_URL + "api/getcamerapicture"
8989
_GETEVENTSUNTIL_REQ = _BASE_URL + "api/geteventsuntil"
90+
_HOME_STATUS = _BASE_URL + "api/homestatus" # Used for Home+ Control Devices
9091

9192

9293
#TODO# Undocumented (but would be very usefull) API : Access currently forbidden (403)
@@ -172,7 +173,7 @@ class ClientAuth:
172173
def __init__(self, clientId=_CLIENT_ID,
173174
clientSecret=_CLIENT_SECRET,
174175
refreshToken=_REFRESH_TOKEN):
175-
176+
176177
self._clientId = clientId
177178
self._clientSecret = clientSecret
178179
self._accessToken = None
@@ -226,6 +227,59 @@ class UserInfo:
226227
pass
227228

228229

230+
class HomeStatus:
231+
"""
232+
List all Home+Control devices (Smarther thermostat, Socket, Cable Output, Centralized fan, Micromodules, ......)
233+
234+
Args:
235+
authData (clientAuth): Authentication information with a working access Token
236+
home : Home name or id of the home who's thermostat belongs to
237+
"""
238+
def __init__(self, authData, home_id):
239+
240+
self.getAuthToken = authData.accessToken
241+
postParams = {
242+
"access_token" : self.getAuthToken,
243+
"home_id": home_id
244+
}
245+
resp = postRequest("home_status", _HOME_STATUS, postParams)
246+
self.resp = resp
247+
self.rawData = resp['body']['home']
248+
if not self.rawData : raise NoHome("No home %s found" % home_id)
249+
self.rooms = self.rawData['rooms']
250+
self.modules = self.rawData['modules']
251+
252+
def getRoomsId(self):
253+
return [room['id'] for room in self.rooms]
254+
255+
def getListRoomParam(self, room_id):
256+
for room in self.rooms:
257+
if(room['id'] == room_id):
258+
return [param for param in room]
259+
return None
260+
261+
def getRoomParam(self, room_id, param):
262+
for room in self.rooms:
263+
if(room['id'] == room_id and param in room):
264+
return room[param]
265+
return None
266+
267+
def getModulesId(self):
268+
return [module['id'] for module in self.modules]
269+
270+
def getListModuleParam(self, module_id):
271+
for module in self.modules:
272+
if(module['id'] == module_id):
273+
return [param for param in module]
274+
return None
275+
276+
def getModuleParam(self, module_id, param):
277+
for module in self.modules:
278+
if(module['id'] == module_id and param in module):
279+
return module[param]
280+
return None
281+
282+
229283
class ThermostatData:
230284
"""
231285
List the Thermostat and temperature modules
@@ -260,7 +314,7 @@ def __init__(self, authData, home=None):
260314

261315
def getThermostat(self, name=None):
262316
if ['name'] != name: return None
263-
else: return
317+
else: return
264318
return self.thermostat[self.defaultThermostatId]
265319

266320
def moduleNamesList(self, name=None, tid=None):
@@ -688,7 +742,7 @@ def presenceUrl(self, camera=None, home=None, cid=None, setting=None):
688742
if camera["type"] != "NOC": return None # Not a presence camera
689743
vpnUrl, localUrl = self.cameraUrls(cid=camera["id"])
690744
return localUrl
691-
745+
692746
def presenceLight(self, camera=None, home=None, cid=None, setting=None):
693747
url = self.presenceUrl(home=home, camera=camera) or self.cameraById(cid=cid)
694748
if not url or setting not in ("on", "off", "auto"): return None
@@ -749,7 +803,7 @@ def filter_home_data(rawData, home):
749803
def cameraCommand(cameraUrl, commande, parameters=None, timeout=3):
750804
url = cameraUrl + ( commande % parameters if parameters else commande)
751805
return postRequest("Camera", url, timeout=timeout)
752-
806+
753807
def postRequest(topic, url, params=None, timeout=10):
754808
if PYTHON3:
755809
req = urllib.request.Request(url)
@@ -816,7 +870,7 @@ def getStationMinMaxTH(station=None, module=None, home=None):
816870
result[m] = (r[0], lastD[m]['Temperature'], r[1])
817871
else:
818872
if time.time()-lastD[module]['When'] > 3600 : result = ["-", "-"]
819-
else :
873+
else :
820874
result = [lastD[module]['Temperature'], lastD[module]['Humidity']]
821875
result.extend(devList.MinMaxTH(module))
822876
return result
@@ -827,15 +881,15 @@ def getStationMinMaxTH(station=None, module=None, home=None):
827881
if __name__ == "__main__":
828882

829883
from sys import exit, stdout, stderr
830-
884+
831885
logging.basicConfig(format='%(name)s - %(levelname)s: %(message)s', level=logging.INFO)
832886

833887
if not _CLIENT_ID or not _CLIENT_SECRET or not _REFRESH_TOKEN :
834888
stderr.write("Library source missing identification arguments to check lnetatmo.py (user/password/etc...)")
835889
exit(1)
836890

837891
authorization = ClientAuth() # Test authentication method
838-
892+
839893
try:
840894
weatherStation = WeatherStationData(authorization) # Test DEVICELIST
841895
except NoDevice:

usage.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,17 +422,57 @@ Methods :
422422
* **getLiveSnapshot** (camera=None, home=None, cid=None) : Get a jpeg of current live view of the camera
423423
* Input : camera name and optional home name or cameraID to lookup (str)
424424
* Output : jpeg binary content
425+
426+
427+
#### 4-6 HomeStatus class ####
428+
429+
430+
Constructor
431+
432+
```python
433+
homeStatus = lnetatmo.HomeStatus( authorization, home_id )
434+
```
435+
436+
Requires :
437+
- an authorization object (ClientAuth instance)
438+
- home_id which can be found in https://dev.netatmo.com/apidocumentation/control by using "GET homesdata"
439+
440+
Return : a homeStatus object. This object contains most administration properties of Home+ Control devices such as Smarther thermostat, Socket, Cable Output, Centralized fan, Micromodules, ...
441+
442+
Methods :
443+
444+
* **getRoomsId** : return all room ID
445+
* Output : list of IDs of every single room (only the one with Smarther thermostat)
446+
447+
* **getListRoomParam** : return every parameters of a room
448+
* Input : room ID
449+
* Output : list of parameters of a room
450+
451+
* **getRoomParam** : return a specific parameter for a specific room
452+
* Input : room ID and parameter
453+
* Output : value
454+
455+
* **getModulesId** : return all module IDs
456+
* Output : list of IDs of every single module (socket, cable outlet, ...)
457+
458+
* **getListModuleParam** : return every parameters of a module
459+
* Input : module ID
460+
* Output : list of parameters of a module
461+
462+
* **getModuleParam** : return a specific parameter for a specific module
463+
* Input : module ID and parameter
464+
* Output : value
425465

426466

427-
#### 4-5 Utilities functions ####
467+
#### 4-7 Utilities functions ####
428468

429469

430470
* **toTimeString** (timestamp) : Convert a Netatmo time stamp to a readable date/time format.
431471
* **toEpoch**( dateString) : Convert a date string (form YYYY-MM-DD_HH:MM:SS) to timestamp
432472
* **todayStamps**() : Return a couple of epoch time (start, end) for the current day
433473

434474

435-
#### 4-6 All-in-One function ####
475+
#### 4-8 All-in-One function ####
436476

437477

438478
If you just need the current temperature and humidity reported by a sensor with associated min and max values on the last 24 hours, you can get it all with only one call that handle all required steps including authentication :

0 commit comments

Comments
 (0)