@@ -18,7 +18,7 @@ function Netatmo:new(config)
1818 return self
1919end
2020
21- function Netatmo :searchDevices (types , callback )
21+ function Netatmo :searchDevices (types , callback , fallback )
2222 if # types < 1 then types = nil end
2323 local buildModule = function (module )
2424 return {
@@ -58,12 +58,12 @@ function Netatmo:searchDevices(types, callback)
5858 end
5959 end
6060 local authCallback = function (response )
61- self :getStationsData (getStationsDataCallback )
61+ self :getStationsData (getStationsDataCallback , fallback )
6262 end
63- self :auth (authCallback )
63+ self :auth (authCallback , fallback )
6464end
6565
66- function Netatmo :getSensorData (types , moduleID , callback )
66+ function Netatmo :getSensorData (types , moduleID , callback , fallback )
6767 if callback == nil then
6868 callback = function () end
6969 end
@@ -97,7 +97,7 @@ function Netatmo:getSensorData(types, moduleID, callback)
9797 end
9898 end
9999 local authCallback = function (response )
100- self :getStationsData (getStationsDataCallback )
100+ self :getStationsData (getStationsDataCallback , fallback )
101101 end
102102 local searchDevicesCallback = function (stations )
103103 if self .device_id == " " then
@@ -124,13 +124,13 @@ function Netatmo:getSensorData(types, moduleID, callback)
124124 end
125125 end
126126 if string.len (moduleID ) < 10 or self .device_id == " " then
127- self :searchDevices (types , searchDevicesCallback )
127+ self :searchDevices (types , searchDevicesCallback , fallback )
128128 else
129129 searchDevicesCallback ({{modules = {{id = moduleID }}}})
130130 end
131131end
132132
133- function Netatmo :getWeatherData (callback )
133+ function Netatmo :getWeatherData (callback , fallback )
134134 local getStationsDataCallback = function (devices )
135135 local device = devices [1 ]
136136 local weatherData = {
@@ -157,12 +157,12 @@ function Netatmo:getWeatherData(callback)
157157 end
158158 end
159159 local authCallback = function (response )
160- self :getStationsData (getStationsDataCallback )
160+ self :getStationsData (getStationsDataCallback , fallback )
161161 end
162- self :auth (authCallback )
162+ self :auth (authCallback , fallback )
163163end
164164
165- function Netatmo :getStationsData (callback , attempt )
165+ function Netatmo :getStationsData (callback , fallback , attempt )
166166 if attempt == nil then
167167 attempt = 0
168168 end
@@ -171,19 +171,22 @@ function Netatmo:getStationsData(callback, attempt)
171171 return
172172 end
173173 if response .status == 401 or response .status == 403 then
174- self . config :setAccessToken (' ' )
174+ self :setAccessToken (' ' )
175175 attempt = 2
176176 end
177177 if attempt < 3 then
178178 attempt = attempt + 1
179179 fibaro .setTimeout (3000 , function ()
180180 QuickApp :debug (' Netatmo:getStationData - Retry attempt #' .. attempt )
181181 local authCallback = function (response )
182- self :getStationsData (callback , attempt )
182+ self :getStationsData (callback , fallback , attempt )
183183 end
184- self :auth (authCallback )
184+ self :auth (authCallback , fallback )
185185 end )
186186 end
187+ if fallback ~= nil then
188+ fallback (response )
189+ end
187190 end
188191 local success = function (response )
189192 if response .status > 299 then
@@ -205,9 +208,10 @@ function Netatmo:getStationsData(callback, attempt)
205208 self .http :get (url , success , fail , headers )
206209end
207210
208- function Netatmo :auth (callback )
211+ function Netatmo :auth (callback , fallback )
209212 if string.len (self :getAccessToken ()) > 10 then
210213 -- QuickApp:debug('Already authenticated')
214+ self :syncAccessToken ()
211215 if callback ~= nil then
212216 callback ({})
213217 end
@@ -225,8 +229,8 @@ function Netatmo:auth(callback)
225229 if self .access_token ~= " " and response .status == 401 then
226230 self :setAccessToken (' ' )
227231 end
228- if callback ~= nil then
229- callback (response )
232+ if fallback ~= nil then
233+ fallback (response )
230234 end
231235 end
232236 if string.len (self .refresh_token ) < 10 then
@@ -280,3 +284,9 @@ function Netatmo:setRefreshToken(refresh_token)
280284 self .refresh_token = refresh_token
281285 self .config :setRefreshToken (refresh_token )
282286end
287+
288+ function Netatmo :syncAccessToken ()
289+ if self .access_token then
290+ self .config :syncAccessToken (self .access_token )
291+ end
292+ end
0 commit comments