Skip to content

Commit f3fa801

Browse files
committed
Adds a command line interface to the gem
This adds a "tesla" command which has the ability to: lock, unlock, get the range of the vehicle, get outside and inside temperatures, and turn on the car's A/C system.
1 parent 41b4ec5 commit f3fa801

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

Gemfile.lock

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
PATH
2+
remote: .
3+
specs:
4+
tesla-api (0.0.2)
5+
httpclient (~> 2.3.2)
6+
thor
7+
8+
GEM
9+
remote: https://rubygems.org/
10+
specs:
11+
addressable (2.3.5)
12+
crack (0.4.1)
13+
safe_yaml (~> 0.9.0)
14+
httpclient (2.3.4.1)
15+
safe_yaml (0.9.5)
16+
thor (0.18.1)
17+
vcr (2.4.0)
18+
webmock (1.9.3)
19+
addressable (>= 2.2.7)
20+
crack (>= 0.3.2)
21+
22+
PLATFORMS
23+
ruby
24+
25+
DEPENDENCIES
26+
tesla-api!
27+
vcr (~> 2.4.0)
28+
webmock (~> 1.9.1)

bin/tesla

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'tesla-api'
4+
require 'tesla-api/cli'
5+
TeslaAPI::CLI.start

lib/tesla-api/cli.rb

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
require 'thor'
2+
3+
module TeslaAPI
4+
class CLI < Thor
5+
include Thor::Actions
6+
7+
class_option :login
8+
class_option :password
9+
10+
def initialize(*args)
11+
super
12+
13+
@login = ENV["TESLA_API_LOGIN"]
14+
@password = ENV["TESLA_API_PASSWORD"]
15+
end
16+
17+
desc "range", "Gets the current rated range of the vehicle"
18+
def range
19+
display(vehicle.charge_state.battery_range_miles, "miles")
20+
end
21+
22+
desc "inside_temp", "Gets the inside temperature"
23+
def inside_temp
24+
display(vehicle.climate_state.inside_temp_celcius, "C")
25+
end
26+
27+
desc "outside_temp", "Gets the outside temperature"
28+
def outside_temp
29+
display(vehicle.climate_state.outside_temp_celcius.inspect, "C")
30+
end
31+
32+
desc "lock", "Locks the car doors"
33+
def lock
34+
vehicle.lock_door!
35+
36+
puts "Locking..."
37+
38+
sleep 1
39+
40+
puts vehicle.state.locked? ? "Doors are locked" : "Doors are unlocked"
41+
end
42+
43+
desc "unlock", "Unlocks the car doors"
44+
def unlock
45+
vehicle.unlock_door!
46+
47+
puts "Unlocking..."
48+
49+
sleep 1
50+
51+
puts vehicle.state.locked? ? "Doors are locked" : "Doors are unlocked"
52+
end
53+
54+
desc "cool TEMP", "Starts the A/C on the car and set it to the desired temp"
55+
def cool(temp)
56+
vehicle.set_temperature!(temp, temp)
57+
vehicle.auto_conditioning_start!
58+
end
59+
60+
desc "where", "Generates a google maps link showing where your car is"
61+
def where
62+
drive_state = vehicle.drive_state
63+
64+
puts "http://maps.google.com?q=#{drive_state.latitude},#{drive_state.longitude}"
65+
end
66+
67+
protected
68+
69+
def vehicle
70+
@vehicle ||= begin
71+
populate_auth(options)
72+
73+
tesla = TeslaAPI::Connection.new(@login, @password)
74+
75+
unless tesla.vehicle
76+
puts "Could not connect to the API and access your vehicle"
77+
exit 1
78+
end
79+
80+
tesla.vehicle
81+
rescue TeslaAPI::Errors::NotLoggedIn => ex
82+
puts "Invalid login"
83+
exit 1
84+
end
85+
end
86+
87+
def display(value, units)
88+
if value.to_s.empty?
89+
puts "Could not read data from the vehicle"
90+
else
91+
puts "#{value} #{units}"
92+
end
93+
end
94+
95+
def populate_auth(options)
96+
@login = options[:login] if options[:login]
97+
@password = options[:password] if options[:password]
98+
end
99+
end
100+
end
101+

tesla-api.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
1717
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
1818
gem.require_paths = ["lib"]
1919
gem.add_dependency 'httpclient', "~> 2.3.2"
20+
gem.add_dependency 'thor'
2021
gem.add_development_dependency 'vcr', "~> 2.4.0"
2122
gem.add_development_dependency 'webmock', "~> 1.9.1"
2223
end

0 commit comments

Comments
 (0)