Skip to content

Commit

Permalink
feat: refresh token authentication
Browse files Browse the repository at this point in the history
Adds support for refresh token based authentication
  • Loading branch information
ikubicki committed Nov 11, 2023
1 parent d646c22 commit 5b1a660
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 156 deletions.
5 changes: 5 additions & 0 deletions Config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ function Config:setAccessToken(token)
self.token = token
end

function Config:getRefreshToken()
return self.rtoken
end

function Config:getTimeoutInterval()
return tonumber(self.interval) * 60000
end
Expand All @@ -76,6 +80,7 @@ function Config:init()
self.type = tostring(self.app:getVariable('Type'))
self.interval = self.app:getVariable('Interval')
self.token = self.app:getVariable('AccessToken')
self.rtoken = self.app:getVariable('RefreshToken')

local storedClientID = Globals:get('netatmo_client_id')
local storedClientSecret = Globals:get('netatmo_client_secret')
Expand Down
16 changes: 16 additions & 0 deletions Netatmo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function Netatmo:new(config)
self.module_id = config:getModuleID()
self.access_token = config:getAccessToken()
self.token = Globals:get('netatmo_atoken', '')
self.refresh_token = config:getRefreshToken()
self.http = HTTPClient:new({})
return self
end
Expand Down Expand Up @@ -209,6 +210,13 @@ function Netatmo:auth(callback)
end
return
end
if string.len(self.access_token) > 10 then
if callback ~= nil then
Netatmo:setToken(self.access_token)
callback({})
end
return
end
local data = {
["grant_type"] = 'password',
["scope"] = 'read_station',
Expand All @@ -217,6 +225,14 @@ function Netatmo:auth(callback)
["username"] = self.user,
["password"] = self.pass,
}
if string.len(self.refresh_token) > 10 then
data = {
["grant_type"] = 'refresh_token',
["refresh_token"] = self.refresh_token,
["client_id"] = self.client_id,
["client_secret"] = self.client_secret,
}
end
local fail = function(response)
QuickApp:error('Unable to authenticate')
if self.access_token == self.token then
Expand Down
33 changes: 14 additions & 19 deletions Netatmo_Unified_Sensor-CO2.fqa

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions Netatmo_Unified_Sensor-Humidity.fqa

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions Netatmo_Unified_Sensor-Noise.fqa

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions Netatmo_Unified_Sensor-Pressure.fqa

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions Netatmo_Unified_Sensor-Rain.fqa

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions Netatmo_Unified_Sensor-Temperature.fqa

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions Netatmo_Unified_Sensor-Wind.fqa

Large diffs are not rendered by default.

31 changes: 13 additions & 18 deletions Netatmo_Unified_Sensor.fqa

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ Data updates every 5 minutes by default.

`ClientSecret` - Netatmo client secret

`Username` - Netatmo username

`Password` - Netatmo password
`RefreshToken` - Refresh token

### Optional values

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

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

## Installation

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).
From generated token you need to use a Refresh Token value.
This should allow you to run quick application in your Fibaro Home Center device.

## Integration

This quick application integrates with other Netatmo dedicated quick apps for devices. It will automatically populate configuration to new virtual Netatmo devices.
This quick application integrates with other Netatmo dedicated quick apps for devices. It will automatically populate configuration to new virtual Netatmo devices.

## Support

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.
3 changes: 2 additions & 1 deletion main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--[[
Netatmo Unified Sensor
@author ikubicki
@version 1.1.0
]]
function QuickApp:onInit()
self.config = Config:new(self)
Expand Down Expand Up @@ -56,7 +57,7 @@ function QuickApp:searchEvent()
self:debug(self.i18n:get('searching-devices'))
self:updateView("button2_1", "text", self.i18n:get('searching-devices'))
local searchDevicesCallback = function(stations)
QuickApp:debug(json.encode(stations))
-- QuickApp:debug(json.encode(stations))
-- printing results
for _, station in pairs(stations) do
QuickApp:trace(string.format(self.i18n:get('search-row-station'), station.name, station.id))
Expand Down

0 comments on commit 5b1a660

Please sign in to comment.