nest_api 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/nest_api/access_token.rb +57 -0
- data/lib/nest_api/api.rb +24 -0
- data/lib/nest_api/thermostat.rb +23 -0
- data/lib/nest_api.rb +17 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fccd97bb052f88030e825f3f78fc43519a115d3e
|
4
|
+
data.tar.gz: 2f822a60663033710c3fe796ee01195264889411
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd5ee37688492a63a42fb7508e4dae078f233ceef1a0b28fca15d151ccaf8c706f51e5d8552749bcf45b828c838d4725bac3403901b95c6c3e5e99c8d7c64b7c
|
7
|
+
data.tar.gz: 6e07fa67383f1efa0c8d5523a5de150305020702ff39a86f63fdb5d383300d5335cf6474b179ad4e87005bbcdbaa7e946fe90d6078437a32e0cfc971da18060b
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module NestApi
|
2
|
+
class AccessToken
|
3
|
+
def initialize(product_id, product_secret, file)
|
4
|
+
@product_id = product_id
|
5
|
+
@product_secret = product_secret
|
6
|
+
@file = file
|
7
|
+
end
|
8
|
+
|
9
|
+
def access_token
|
10
|
+
auth_code['access_token']
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def auth_code
|
16
|
+
@auth_code ||= retrieve_credentials
|
17
|
+
end
|
18
|
+
|
19
|
+
# Initiate authorization process to obtain a nest access code
|
20
|
+
def authorize
|
21
|
+
# Prompt user for pin code -- must be retrieved from online
|
22
|
+
puts "Please generate a pin code at #{NestApi::AUTH_URL}#{@product_id} and enter it here:"
|
23
|
+
pin = gets.strip
|
24
|
+
|
25
|
+
result = HTTParty.post("#{NestApi::TOKEN_URL}", query: {
|
26
|
+
code: pin,
|
27
|
+
client_id: @product_id,
|
28
|
+
client_secret: @product_secret,
|
29
|
+
grant_type: 'authorization_code'
|
30
|
+
})
|
31
|
+
|
32
|
+
File.open(@file, "w") { |file| file.write(result.to_json) }
|
33
|
+
|
34
|
+
@auth_code = result
|
35
|
+
end
|
36
|
+
|
37
|
+
def configuration_error
|
38
|
+
raise ConfigurationError, "The config file is either empty or corrupt. Please remove #{@file} from the filesystem and reauthenticate."
|
39
|
+
end
|
40
|
+
|
41
|
+
# Read saved nest credentials from a file
|
42
|
+
def retrieve_credentials
|
43
|
+
@auth_code = JSON.parse(File.read(@file))
|
44
|
+
|
45
|
+
if @auth_code['access_token'].nil? || @auth_code['access_token'].empty?
|
46
|
+
configuration_error
|
47
|
+
end
|
48
|
+
|
49
|
+
@auth_code
|
50
|
+
|
51
|
+
rescue Errno::ENOENT
|
52
|
+
authorize
|
53
|
+
rescue JSON::ParserError
|
54
|
+
configuration_error
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/nest_api/api.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module NestApi
|
2
|
+
class Api
|
3
|
+
def initialize(product_id: ENV.fetch("NEST_PRODUCT_ID"), product_secret: ENV.fetch("NEST_PRODUCT_SECRET"), file: NestApi::CONFIG_FILE)
|
4
|
+
@token = NestApi::AccessToken.new(product_id, product_secret, file)
|
5
|
+
end
|
6
|
+
|
7
|
+
# Retrieve a list of all thermostats registered to this account
|
8
|
+
def get_thermostat_list
|
9
|
+
create_thermostats(HTTParty.get("#{NestApi::API_URL}/devices/thermostats", query: { auth: @token.access_token }))
|
10
|
+
end
|
11
|
+
|
12
|
+
# Retrieve a single thermostat by name
|
13
|
+
def get_thermostat_by_name(name)
|
14
|
+
get_thermostat_list[name]
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
# Transform the nest api thermostats reponse into NestApi::Thermostat objects
|
20
|
+
def create_thermostats(thermostat_list)
|
21
|
+
thermostat_list.map { |device, info| [ info['name'], NestApi::Thermostat.new(device, @token) ] }.to_h
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module NestApi
|
2
|
+
class Thermostat
|
3
|
+
def initialize(device_id, token)
|
4
|
+
@id = device_id
|
5
|
+
@token = token
|
6
|
+
end
|
7
|
+
|
8
|
+
# Retrieve a property of a thermostat
|
9
|
+
# If no property_id is supplied, all properties of the thermostat will be returned
|
10
|
+
def get(property_id = '')
|
11
|
+
HTTParty.get("#{NestApi::API_URL}/devices/thermostats/#{@id}/#{property_id}", query: { auth: @token.access_token })
|
12
|
+
end
|
13
|
+
|
14
|
+
# Set a thermostat property to a new value
|
15
|
+
def update!(property_id:, value:)
|
16
|
+
HTTParty.put("#{NestApi::API_URL}/devices/thermostats/#{@id}",
|
17
|
+
query: { auth: @token.access_token },
|
18
|
+
body: { "#{property_id}" => value }.to_json,
|
19
|
+
headers: { 'Content-Type' => 'application/json'
|
20
|
+
})
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/nest_api.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'HTTParty'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
require_relative 'nest_api/access_token'
|
5
|
+
require_relative 'nest_api/api'
|
6
|
+
require_relative 'nest_api/thermostat'
|
7
|
+
|
8
|
+
module NestApi
|
9
|
+
CONFIG_FILE = './nest_config'
|
10
|
+
|
11
|
+
API_URL = 'https://developer-api.nest.com'
|
12
|
+
AUTH_URL = 'https://home.nest.com/login/oauth2?state=STATE&client_id='
|
13
|
+
TOKEN_URL = 'https://api.home.nest.com/oauth2/access_token'
|
14
|
+
|
15
|
+
ConfigurationError = Class.new(StandardError)
|
16
|
+
ApiError = Class.new(StandardError)
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nest_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jessica Burns
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Simple API Wrapper for Nest Thermostats
|
14
|
+
email: jessburnsm@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/nest_api.rb
|
20
|
+
- lib/nest_api/access_token.rb
|
21
|
+
- lib/nest_api/api.rb
|
22
|
+
- lib/nest_api/thermostat.rb
|
23
|
+
homepage: http://rubygems.org/gems/nest_api
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.6.8
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Nest thermostat API Wrapper
|
47
|
+
test_files: []
|