Skip to content

Instantly share code, notes, and snippets.

@tlyakhov
Created July 19, 2017 00:21
Show Gist options
  • Save tlyakhov/4be20b588c4d34d9bd831c01c7594c92 to your computer and use it in GitHub Desktop.
Save tlyakhov/4be20b588c4d34d9bd831c01c7594c92 to your computer and use it in GitHub Desktop.
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
LEVITON_ROOT = 'https://my.leviton.com/api'
def lev_request(api, body, type = :get, sessionId = nil)
uri = URI.parse("#{LEVITON_ROOT}#{api}")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
header = {'Content-Type' =>'application/json'}
header['authorization'] = sessionId unless sessionId.nil?
if type == :post
req = Net::HTTP::Post.new(uri.path, initheader = header)
elsif type == :get
req = Net::HTTP::Get.new(uri.path, initheader = header)
elsif type == :put
req = Net::HTTP::Put.new(uri.path, initheader = header)
end
req.body = body.to_json unless body.nil?
https.request(req)
end
def lev_login(email, password)
body = {
# 'firstName' => 'Tim',
# 'lastName' => 'Lyakhovetskiy',
'email' => email,
'password' => password,
'clientId' => 'levdb-echo-proto', # magic?
'registeredVia' => 'myLeviton' # magic?
}
res = lev_request('/Person/login', body, :post)
if res.code != '200'
puts "Response #{res.code} #{res.message}: #{res.body}"
return nil
end
JSON.parse(res.body)
end
def lev_logout(session)
body = {}
res = lev_request('/Person/logout', body, :post, session['id'])
puts res
if res.code != '200' && res.code != '204'
puts "Response #{res.code} #{res.message}: #{res.body}"
return nil
end
end
def lev_residential_permissions(session)
res = lev_request("/Person/#{session['userId']}/residentialPermissions", nil, :get, session['id'])
if res.code != '200' && res.code != '204'
puts "Response #{res.code} #{res.message}: #{res.body}"
return nil
end
JSON.parse(res)
end
def lev_residences(session, residential_account_id)
res = lev_request("/ResidentialAccounts/#{residential_account_id}/Residences", nil, :get, session['id'])
if res.code != '200' && res.code != '204'
puts "Response #{res.code} #{res.message}: #{res.body}"
return nil
end
JSON.parse(res.body)
end
def lev_iot_switches(session, residence_id)
res = lev_request("/Residences/#{residence_id}/iotSwitches", nil, :get, session['id'])
if res.code != '200' && res.code != '204'
puts "Response #{res.code} #{res.message}: #{res.body}"
return nil
end
JSON.parse(res.body)
end
def lev_update_switch(session, switch_id, power, brightness)
body = { 'brightness' => brightness, 'power' => power }
res = lev_request("/IotSwitches/#{switch_id}", body, :put, session['id'])
#if res.code != '200' && res.code != '204'
puts "Response #{res.code} #{res.message}: #{res.body}"
# return nil
#end
JSON.parse(res.body)
end
session = nil
begin
session = JSON.parse(File.read('lev_session.json'))
rescue => ex
end
if session.nil?
session = lev_login('my_email_address@email.com', 'my_password_xxxxx')
end
if session.nil?
puts 'Login failed.'
exit(1)
end
puts "Login success: #{session['id']}"
File.write('lev_session.json', session.to_json)
begin
perm = lev_residential_permissions(session)
residences = lev_residences(session, perm[0]['residentialAccountId'])
switches = lev_iot_switches(session, residences[0]['id'])
puts switches.inspect
lev_update_switch(session, switches[0]['id'], 'ON', 40)
ensure
logout(session)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment