|
| 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 | + |
0 commit comments