Class: Ecobee::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/ecobee/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil, access_token_expire: nil, app_key: nil, callbacks: {}, log_file: nil, refresh_token: nil, scope: SCOPES[0], token_file: DEFAULT_FILES) ⇒ Token

Returns a new instance of Token.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ecobee/token.rb', line 23

def initialize(
  access_token: nil,
  access_token_expire: nil,
  app_key: nil,
  callbacks: {},
  log_file: nil,
  refresh_token: nil,
  scope: SCOPES[0],
  token_file: DEFAULT_FILES
)
  @access_token = access_token
  @access_token_expire = access_token_expire
  @app_key = app_key
  @callbacks = callbacks
  @refresh_token = refresh_token
  @scope = scope
  @token_file = expand_files token_file

  @authorization_thread, @pin, @status, @token_type = nil
  @poll_interval = DEFAULT_POLL_INTERVAL

  @http = Ecobee::HTTP.new(log_file: log_file, token: self)

  @refresh_pad = REFRESH_PAD + rand(REFRESH_PAD)

  config_load
  access_token()
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def access_token
  @access_token
end

#access_token_expireObject (readonly)

Returns the value of attribute access_token_expire.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def access_token_expire
  @access_token_expire
end

#app_keyObject (readonly)

Returns the value of attribute app_key.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def app_key
  @app_key
end

#callbacksObject (readonly)

Returns the value of attribute callbacks.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def callbacks
  @callbacks
end

#httpObject (readonly)

Returns the value of attribute http.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def http
  @http
end

#pinObject (readonly)

Returns the value of attribute pin.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def pin
  @pin
end

#resultObject (readonly)

Returns the value of attribute result.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def result
  @result
end

#scopeObject (readonly)

Returns the value of attribute scope.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def scope
  @scope
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def status
  @status
end

#token_fileObject (readonly)

Returns the value of attribute token_file.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def token_file
  @token_file
end

#token_typeObject (readonly)

Returns the value of attribute token_type.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def token_type
  @token_type
end

Instance Method Details

#access_token_expired?Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/ecobee/token.rb', line 83

def access_token_expired?
  return true unless @access_token_expire
  Time.now.to_i > @access_token_expire - @refresh_pad
end

#authorizationObject



88
89
90
# File 'lib/ecobee/token.rb', line 88

def authorization
  "#{@token_type} #{access_token}"
end

#config_loadObject



92
93
94
95
96
97
98
99
# File 'lib/ecobee/token.rb', line 92

def config_load
  # TODO: track file mod time & last_read to avoid excessive rereads
  config = config_read_our_section
  if @callbacks[:load].respond_to? :call
    config = @callbacks[:load].call(config)
  end
  config_load_to_memory config
end

#config_saveObject



101
102
103
104
105
106
107
# File 'lib/ecobee/token.rb', line 101

def config_save
  config = config_dump()
  if @callbacks[:save].respond_to? :call
    config = @callbacks[:save].call(config)
  end
  config_write_section config
end

#pin_is_validObject



109
110
111
112
113
# File 'lib/ecobee/token.rb', line 109

def pin_is_valid
  if @pin && @access_token && @access_token_expire
    @access_token_expire.to_i >= Time.now.to_i
  end
end

#pin_messageObject



115
116
117
118
# File 'lib/ecobee/token.rb', line 115

def pin_message
  "Log into Ecobee web portal, select My Apps widget, Add Application, " +
  "enter the PIN #{@pin || ''}"
end

#refresh_access_token(retries: 0) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ecobee/token.rb', line 120

def refresh_access_token(retries: 0)
  arg = sprintf("?grant_type=refresh_token&refresh_token=%s&client_id=%s",
                refresh_token,
                @app_key)
  result = @http.post(arg: arg,
                      no_auth: true,
                      resource_prefix: 'token',
                      validate_status: false)
  if result.key? 'error'
    if retries > 0 && result['error'] == 'invalid_grant'
      raise Ecobee::RetryAuthError.new
    end
    @access_token, @access_token_expire, @pin, @scope, @refresh_token = nil
    config_save
    raise Ecobee::AuthError.new("Result Error: (%s) %s" % [
                                result['error'],
                                result['error_description']])
  else
    result_load_to_memory result
  end 
end

#refresh_access_token_wrapperObject



142
143
144
145
146
147
148
149
150
151
# File 'lib/ecobee/token.rb', line 142

def refresh_access_token_wrapper
  retries = 3
  retries.times do |attempt|
    begin
      refresh_access_token(retries: retries - attempt)
    rescue Ecobee::RetryAuthError.new
      sleep 10
    end
  end
end

#refresh_tokenObject



153
154
155
156
# File 'lib/ecobee/token.rb', line 153

def refresh_token
  config_load
  @refresh_token
end

#register_callback(type, *callback, &block) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/ecobee/token.rb', line 158

def register_callback(type, *callback, &block)
  if block_given?
    puts "Registering #{type}"
    @callbacks[type] = block
  else
    @callbacks[type] = callback[0] if callback.length > 0
  end
end

#wait(timeout: nil) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/ecobee/token.rb', line 167

def wait(timeout: nil)
  if timeout
    Timeout::timeout(timeout) { wait(timeout: nil) }
  else
    sleep 0.01 while @status == :authorization_pending
  end
rescue Timeout::Error
ensure
  @status
end