Skip to content

Commit 5b1a660

Browse files
committed
feat: refresh token authentication
Adds support for refresh token based authentication
1 parent d646c22 commit 5b1a660

12 files changed

+146
-156
lines changed

Config.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ function Config:setAccessToken(token)
5353
self.token = token
5454
end
5555

56+
function Config:getRefreshToken()
57+
return self.rtoken
58+
end
59+
5660
function Config:getTimeoutInterval()
5761
return tonumber(self.interval) * 60000
5862
end
@@ -76,6 +80,7 @@ function Config:init()
7680
self.type = tostring(self.app:getVariable('Type'))
7781
self.interval = self.app:getVariable('Interval')
7882
self.token = self.app:getVariable('AccessToken')
83+
self.rtoken = self.app:getVariable('RefreshToken')
7984

8085
local storedClientID = Globals:get('netatmo_client_id')
8186
local storedClientSecret = Globals:get('netatmo_client_secret')

Netatmo.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function Netatmo:new(config)
1414
self.module_id = config:getModuleID()
1515
self.access_token = config:getAccessToken()
1616
self.token = Globals:get('netatmo_atoken', '')
17+
self.refresh_token = config:getRefreshToken()
1718
self.http = HTTPClient:new({})
1819
return self
1920
end
@@ -209,6 +210,13 @@ function Netatmo:auth(callback)
209210
end
210211
return
211212
end
213+
if string.len(self.access_token) > 10 then
214+
if callback ~= nil then
215+
Netatmo:setToken(self.access_token)
216+
callback({})
217+
end
218+
return
219+
end
212220
local data = {
213221
["grant_type"] = 'password',
214222
["scope"] = 'read_station',
@@ -217,6 +225,14 @@ function Netatmo:auth(callback)
217225
["username"] = self.user,
218226
["password"] = self.pass,
219227
}
228+
if string.len(self.refresh_token) > 10 then
229+
data = {
230+
["grant_type"] = 'refresh_token',
231+
["refresh_token"] = self.refresh_token,
232+
["client_id"] = self.client_id,
233+
["client_secret"] = self.client_secret,
234+
}
235+
end
220236
local fail = function(response)
221237
QuickApp:error('Unable to authenticate')
222238
if self.access_token == self.token then

Netatmo_Unified_Sensor-CO2.fqa

Lines changed: 14 additions & 19 deletions
Large diffs are not rendered by default.

Netatmo_Unified_Sensor-Humidity.fqa

Lines changed: 14 additions & 19 deletions
Large diffs are not rendered by default.

Netatmo_Unified_Sensor-Noise.fqa

Lines changed: 14 additions & 19 deletions
Large diffs are not rendered by default.

Netatmo_Unified_Sensor-Pressure.fqa

Lines changed: 14 additions & 19 deletions
Large diffs are not rendered by default.

Netatmo_Unified_Sensor-Rain.fqa

Lines changed: 14 additions & 19 deletions
Large diffs are not rendered by default.

Netatmo_Unified_Sensor-Temperature.fqa

Lines changed: 14 additions & 19 deletions
Large diffs are not rendered by default.

Netatmo_Unified_Sensor-Wind.fqa

Lines changed: 14 additions & 19 deletions
Large diffs are not rendered by default.

Netatmo_Unified_Sensor.fqa

Lines changed: 13 additions & 18 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ Data updates every 5 minutes by default.
1111

1212
`ClientSecret` - Netatmo client secret
1313

14-
`Username` - Netatmo username
15-
16-
`Password` - Netatmo password
14+
`RefreshToken` - Refresh token
1715

1816
### Optional values
1917

@@ -28,6 +26,16 @@ Other sensors (because they are using generic type - Multilevel sensor type) all
2826

2927
`AccessToken` - Allows to set own access token and bypass credentials authentication.
3028

29+
## Installation
30+
31+
To acquire required parameters, you need to go to Netatmo Connect site and create new application. Once that's done, you will be able to get client ID and client secret. To get refresh token, you need to use a Token generator (section below you get client id and client secret).
32+
From generated token you need to use a Refresh Token value.
33+
This should allow you to run quick application in your Fibaro Home Center device.
34+
3135
## Integration
3236

33-
This quick application integrates with other Netatmo dedicated quick apps for devices. It will automatically populate configuration to new virtual Netatmo devices.
37+
This quick application integrates with other Netatmo dedicated quick apps for devices. It will automatically populate configuration to new virtual Netatmo devices.
38+
39+
## Support
40+
41+
Due to horrible user experience with Fibaro Marketplace, for better communication I recommend to contact with me through GitHub or create an issue in the repository.

main.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
--[[
22
Netatmo Unified Sensor
33
@author ikubicki
4+
@version 1.1.0
45
]]
56
function QuickApp:onInit()
67
self.config = Config:new(self)
@@ -56,7 +57,7 @@ function QuickApp:searchEvent()
5657
self:debug(self.i18n:get('searching-devices'))
5758
self:updateView("button2_1", "text", self.i18n:get('searching-devices'))
5859
local searchDevicesCallback = function(stations)
59-
QuickApp:debug(json.encode(stations))
60+
-- QuickApp:debug(json.encode(stations))
6061
-- printing results
6162
for _, station in pairs(stations) do
6263
QuickApp:trace(string.format(self.i18n:get('search-row-station'), station.name, station.id))

0 commit comments

Comments
 (0)