Class: NestThermostat::Nest
- Inherits:
-
Object
- Object
- NestThermostat::Nest
- Defined in:
- lib/nest_thermostat/nest.rb
Instance Attribute Summary collapse
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#device_id ⇒ Object
Returns the value of attribute device_id.
-
#email ⇒ Object
Returns the value of attribute email.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#login ⇒ Object
Returns the value of attribute login.
-
#login_url ⇒ Object
Returns the value of attribute login_url.
-
#password ⇒ Object
Returns the value of attribute password.
-
#structure_id ⇒ Object
Returns the value of attribute structure_id.
-
#temperature_scale ⇒ Object
Returns the value of attribute temperature_scale.
-
#token ⇒ Object
Returns the value of attribute token.
-
#transport_host ⇒ Object
Returns the value of attribute transport_host.
-
#transport_url ⇒ Object
Returns the value of attribute transport_url.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Instance Method Summary collapse
- #away ⇒ Object
- #away=(state) ⇒ Object
- #current_temperature ⇒ Object (also: #current_temp)
- #humidity ⇒ Object
-
#initialize(config = {}) ⇒ Nest
constructor
A new instance of Nest.
- #leaf ⇒ Object
- #public_ip ⇒ Object
- #status ⇒ Object
- #target_temperature_at ⇒ Object (also: #target_temp_at)
- #temp_scale=(scale) ⇒ Object
- #temperature ⇒ Object (also: #temp)
- #temperature=(degrees) ⇒ Object (also: #temp=)
Constructor Details
#initialize(config = {}) ⇒ Nest
Returns a new instance of Nest.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/nest_thermostat/nest.rb', line 12 def initialize(config = {}) raise 'Please specify your nest email' unless config[:email] raise 'Please specify your nest password' unless config[:password] # User specified information self.email = config[:email] self.password = config[:password] self.temperature_scale = config[:temperature_scale] || config[:temp_scale] || 'f' self.login_url = config[:login_url] || 'https://home.nest.com/user/login' self.user_agent = config[:user_agent] ||'Nest/1.1.0.10 CFNetwork/548.0.4' # Login and get token, user_id and URLs perform_login self.token = @auth["access_token"] self.user_id = @auth["userid"] self.transport_url = @auth["urls"]["transport_url"] self.transport_host = URI.parse(self.transport_url).host self.headers = { 'Host' => self.transport_host, 'User-Agent' => self.user_agent, 'Authorization' => 'Basic ' + self.token, 'X-nl-user-id' => self.user_id, 'X-nl-protocol-version' => '1', 'Accept-Language' => 'en-us', 'Connection' => 'keep-alive', 'Accept' => '*/*' } # Set device and structure id status end |
Instance Attribute Details
#auth ⇒ Object
Returns the value of attribute auth.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def auth @auth end |
#device_id ⇒ Object
Returns the value of attribute device_id.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def device_id @device_id end |
#email ⇒ Object
Returns the value of attribute email.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def email @email end |
#headers ⇒ Object
Returns the value of attribute headers.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def headers @headers end |
#login ⇒ Object
Returns the value of attribute login.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def login @login end |
#login_url ⇒ Object
Returns the value of attribute login_url.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def login_url @login_url end |
#password ⇒ Object
Returns the value of attribute password.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def password @password end |
#structure_id ⇒ Object
Returns the value of attribute structure_id.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def structure_id @structure_id end |
#temperature_scale ⇒ Object
Returns the value of attribute temperature_scale.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def temperature_scale @temperature_scale end |
#token ⇒ Object
Returns the value of attribute token.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def token @token end |
#transport_host ⇒ Object
Returns the value of attribute transport_host.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def transport_host @transport_host end |
#transport_url ⇒ Object
Returns the value of attribute transport_url.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def transport_url @transport_url end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def user_agent @user_agent end |
#user_id ⇒ Object
Returns the value of attribute user_id.
8 9 10 |
# File 'lib/nest_thermostat/nest.rb', line 8 def user_id @user_id end |
Instance Method Details
#away ⇒ Object
93 94 95 |
# File 'lib/nest_thermostat/nest.rb', line 93 def away status["structure"][structure_id]["away"] end |
#away=(state) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/nest_thermostat/nest.rb', line 97 def away=(state) request = HTTParty.post( "#{self.transport_url}/v2/put/structure.#{self.structure_id}", body: %Q({"away_timestamp":#{Time.now.to_i},"away":#{!!state},"away_setter":0}), headers: self.headers ) rescue nil end |
#current_temperature ⇒ Object Also known as: current_temp
66 67 68 |
# File 'lib/nest_thermostat/nest.rb', line 66 def current_temperature convert_temp_for_get(status["shared"][self.device_id]["current_temperature"]) end |
#humidity ⇒ Object
62 63 64 |
# File 'lib/nest_thermostat/nest.rb', line 62 def humidity status["device"][self.device_id]["current_humidity"] end |
#leaf ⇒ Object
58 59 60 |
# File 'lib/nest_thermostat/nest.rb', line 58 def leaf status["device"][self.device_id]["leaf"] end |
#public_ip ⇒ Object
54 55 56 |
# File 'lib/nest_thermostat/nest.rb', line 54 def public_ip status["track"][self.device_id]["last_ip"].strip end |
#status ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/nest_thermostat/nest.rb', line 44 def status request = HTTParty.get("#{self.transport_url}/v2/mobile/user.#{self.user_id}", headers: self.headers) rescue nil result = JSON.parse(request.body) rescue nil self.structure_id = result['user'][user_id]['structures'][0].split('.')[1] self.device_id = result['structure'][structure_id]['devices'][0].split('.')[1] result end |
#target_temperature_at ⇒ Object Also known as: target_temp_at
87 88 89 90 |
# File 'lib/nest_thermostat/nest.rb', line 87 def target_temperature_at epoch = status["device"][self.device_id]["time_to_target"] epoch != 0 ? Time.at(epoch) : false end |
#temp_scale=(scale) ⇒ Object
105 106 107 |
# File 'lib/nest_thermostat/nest.rb', line 105 def temp_scale=(scale) self.temperature_scale = scale end |
#temperature ⇒ Object Also known as: temp
71 72 73 |
# File 'lib/nest_thermostat/nest.rb', line 71 def temperature convert_temp_for_get(status["shared"][self.device_id]["target_temperature"]) end |
#temperature=(degrees) ⇒ Object Also known as: temp=
76 77 78 79 80 81 82 83 84 |
# File 'lib/nest_thermostat/nest.rb', line 76 def temperature=(degrees) degrees = convert_temp_for_set(degrees) request = HTTParty.post( "#{self.transport_url}/v2/put/shared.#{self.device_id}", body: %Q({"target_change_pending":true,"target_temperature":#{degrees}}), headers: self.headers ) rescue nil end |