1- # Copyright (c) 2015 RainMachine, Green Electronics LLC
2- # All rights reserved.
3- # Authors: Nicu Pavel <npavel@mini-box.com>
4- # Ciprian Misaila <ciprian.misaila@mini-box.com>
5-
1+ # Copyright (c) 2023 RainMachine, Green Electronics LLC
62from RMParserFramework .rmParser import RMParser
73from RMUtilsFramework .rmLogging import log
84from RMUtilsFramework .rmTimeUtils import rmCurrentDayTimestamp
117from urllib import urlencode
128
139class PersonalNetatmo (RMParser ):
14-
1510 parserName = "PersonalNetatmo Parser"
1611 parserDescription = "Weather observations from NetAtmo personal weather station"
1712 parserForecast = False
@@ -43,7 +38,6 @@ class PersonalNetatmo(RMParser):
4338 accessTokenExpiration = 0
4439 accessToken = None
4540 refreshToken = None
46-
4741 jsonData = None
4842
4943 def perform (self ):
@@ -64,8 +58,6 @@ def perform(self):
6458 self .username = self .params ["username" ]
6559 self .password = self .params ["password" ]
6660 self .clientOauth ()
67-
68- # Try to refresh the access token first if a previous one exists
6961 if self .accessToken is not None :
7062 self .renewAccesTokenIfNeeded ()
7163
@@ -79,28 +71,17 @@ def perform(self):
7971 return
8072
8173 self .getData ()
82-
8374 tsStartOfDayUTC = rmCurrentDayTimestamp ()
84-
8575 if len (self .jsonData ["body" ]["devices" ]) == 0 :
8676 self .lastKnownError = "No NetAtmo devices found"
8777 log .error (self .lastKnownError )
8878 return
89-
90- # Get available modules from returned json
9179 self .buildAvailableModules ()
92-
93- # Build the list of user specified modules
9480 specifiedModules = []
9581 if self .params ["useSpecifiedModules" ]:
9682 modulesString = self .params ["specificModules" ]
9783 specifiedModules = modulesString .split (',' )
9884 specifiedModules = [item .strip () for item in specifiedModules ]
99-
100- # Only use the first reported device which should be user device if user didn't specify modules
101- # Otherwise we will have multiple reports that we need to collate data from multiple devices
102- # and multiple modules
103-
10485 if self .params ["useSpecifiedModules" ]:
10586 for device in self .jsonData ["body" ]["devices" ]:
10687 self .getDeviceData (device , specifiedModules )
@@ -111,7 +92,6 @@ def buildAvailableModules(self):
11192 self .params ["_availableModules" ] = []
11293 for device in self .jsonData ["body" ]["devices" ]:
11394 if "modules" not in device :
114- log .debug ("Device doesn't have modules" )
11595 continue
11696
11797 modules = device ["modules" ]
@@ -144,8 +124,6 @@ def getDeviceData(self, device, specifiedModules):
144124 modules = device ["modules" ]
145125 minRH = 0
146126 maxRH = 0
147- # minPress = 0
148- # maxPress = 0
149127 maxTemp = 0
150128 minTemp = 0
151129 rain = 0
@@ -155,7 +133,6 @@ def getDeviceData(self, device, specifiedModules):
155133 tsRain = None
156134 idxTemp = 0
157135 idxRH = 0
158- # idxPress = 0
159136 idxWind = 0
160137 idxRain = 0
161138 for module in modules :
@@ -210,10 +187,6 @@ def getDeviceData(self, device, specifiedModules):
210187 maxRH += recordedMaxRH
211188 minRH += recordedMinRH
212189 idxRH += 1
213- # if(recordedMaxPress is not None and recordedMinPress is not None):
214- # maxPress += recordedMaxPress
215- # minPress += recordedMinPress
216- # idxPress += 1
217190 except Exception , e :
218191 log .error ("Error reading temperature module: %s" % e )
219192
@@ -225,9 +198,6 @@ def getDeviceData(self, device, specifiedModules):
225198 self .addValue (RMParser .dataType .MAXRH , tsTemp , maxRH / idxRH )
226199 self .addValue (RMParser .dataType .MINRH , tsTemp , minRH / idxRH )
227200
228- # if idxPress > 0 and tsTemp is not None:
229- # self.addValue(RMParser.dataType.PRESSURE, tsTemp, (maxPress + minPress)/(2*idxRH))
230-
231201 if idxWind and tsWind is not None :
232202 self .addValue (RMParser .dataType .WIND , tsWind , wind / idxWind )
233203 if idxRain > 0 and tsRain is not None :
@@ -305,19 +275,10 @@ def postRequest(self, url, params):
305275 req = urllib2 .Request (url = url , data = params , headers = headers )
306276
307277 try :
308- log .info ("Getting data from %s" % url )
309278 response = urllib2 .urlopen (req )
310- log .debug ("%s?%s" % (response .geturl (), params ))
311279 return json .loads (response .read ())
312- except urllib2 .URLError , e :
313- log .debug (e )
314- if hasattr (ssl , '_create_unverified_context' ): #for mac os only in order to ignore invalid certificates
315- try :
316- context = ssl ._create_unverified_context ()
317- response = urllib2 .urlopen (req , context = context )
318- return json .loads (response .read ())
319- except Exception , e :
320- log .exception (e )
280+ except Exception , e :
281+ log .exception (e )
321282 return None
322283
323284
@@ -342,7 +303,3 @@ def __toInt(self, value):
342303 return int (value )
343304 except :
344305 return None
345-
346- if __name__ == "__main__" :
347- p = PersonalNetatmo ()
348- p .perform ()
0 commit comments