Class: NestThermostat::Nest

Inherits:
Object
  • Object
show all
Defined in:
lib/nest_thermostat/nest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.         = 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
  
  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

#authObject

Returns the value of attribute auth.



8
9
10
# File 'lib/nest_thermostat/nest.rb', line 8

def auth
  @auth
end

#device_idObject

Returns the value of attribute device_id.



8
9
10
# File 'lib/nest_thermostat/nest.rb', line 8

def device_id
  @device_id
end

#emailObject

Returns the value of attribute email.



8
9
10
# File 'lib/nest_thermostat/nest.rb', line 8

def email
  @email
end

#headersObject

Returns the value of attribute headers.



8
9
10
# File 'lib/nest_thermostat/nest.rb', line 8

def headers
  @headers
end

#loginObject

Returns the value of attribute login.



8
9
10
# File 'lib/nest_thermostat/nest.rb', line 8

def 
  @login
end

#login_urlObject

Returns the value of attribute login_url.



8
9
10
# File 'lib/nest_thermostat/nest.rb', line 8

def 
  @login_url
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/nest_thermostat/nest.rb', line 8

def password
  @password
end

#structure_idObject

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_scaleObject

Returns the value of attribute temperature_scale.



8
9
10
# File 'lib/nest_thermostat/nest.rb', line 8

def temperature_scale
  @temperature_scale
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/nest_thermostat/nest.rb', line 8

def token
  @token
end

#transport_hostObject

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_urlObject

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_agentObject

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_idObject

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

#awayObject



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_temperatureObject 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

#humidityObject



62
63
64
# File 'lib/nest_thermostat/nest.rb', line 62

def humidity
  status["device"][self.device_id]["current_humidity"]
end

#leafObject



58
59
60
# File 'lib/nest_thermostat/nest.rb', line 58

def leaf
  status["device"][self.device_id]["leaf"]
end

#public_ipObject



54
55
56
# File 'lib/nest_thermostat/nest.rb', line 54

def public_ip
  status["track"][self.device_id]["last_ip"].strip
end

#statusObject



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_atObject 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

#temperatureObject 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