Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added device= method to set the device based on name or number #8

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions lib/nest_thermostat/nest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module NestThermostat
class Nest
attr_accessor :email, :password, :login_url, :user_agent, :auth,
:temperature_scale, :login, :token, :user_id, :transport_url,
:transport_host, :structure_id, :device_id, :headers
:transport_host, :structure_id, :device_id, :headers, :names

def initialize(config = {})
raise 'Please specify your nest email' unless config[:email]
Expand Down Expand Up @@ -38,6 +38,8 @@ def initialize(config = {})
}

# Set device and structure id
self.names = Hash.new
self.device = 0
status
end

Expand All @@ -46,11 +48,19 @@ def status
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["shared"].each {|device, value| names[value['name']] = device }
result
end

def device=(device)
if device.class == String
self.device_id = self.names[device]
else
self.device_id = status['structure'][structure_id]['devices'][device].split('.')[1]
end
end

def public_ip
status["track"][self.device_id]["last_ip"].strip
end
Expand Down Expand Up @@ -84,6 +94,17 @@ def temperature=(degrees)
end
alias_method :temp=, :temperature=

def set_temperature(degrees,name=self.names.keys.first)
degrees = convert_temp_for_set(degrees)

request = HTTParty.post(
"#{self.transport_url}/v2/put/shared.#{self.names[name]}",
body: %Q({"target_change_pending":true,"target_temperature":#{degrees}}),
headers: self.headers
) rescue nil
end


def target_temperature_at
epoch = status["device"][self.device_id]["time_to_target"]
epoch != 0 ? Time.at(epoch) : false
Expand Down Expand Up @@ -118,6 +139,10 @@ def fan_mode=(state)
) rescue nil
end

# def name
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to leave this in? If so, uncomment it else remove please.

Also, could you add tests for this PR?

# status["device"][@nest.device_id]["name"]
# end

private
def perform_login
login_request = HTTParty.post(
Expand Down