The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tesla::Vehicle - Access information and command Tesla automobiles via the API

DESCRIPTION

This distribution provides methods for accessing and updating aspects of your Tesla vehicle. Not all attributes available through Tesla's API have methods listed here yet, I'm only starting with the ones I use myself; I will add more as I go.

For now, you can use the Tesla::API distribution to write your own accessors that aren't complete here. (This distribution uses that module for all Tesla API access).

Object Management Methods

new(%params)

Instantiates and returns a new Tesla::Vehicle object. We subclass Tesla::API so there are several things inherited.

Parameters:

All parameters are sent in as a hash. See the documentation for Tesla::API for further parameters that can be sent into this method.

    id

Optional, Integer: The ID of the vehicle you want to associate this object with. Most methods require this to be set. You can send it in after instantiation by using the id() method. If you don't know what the ID is, you can instantiate the object, and dump the returned hashref from a call to list().

As a last case resort, we will try to figure out the ID by ourselves. If we can't, and no ID has been set, methods that require an ID will croak().

    auto_wake

Optional, Bool: If set, we will automatically wake up your vehicle on calls that require the car to be in an online state to retrieve data (via a call to wake()). If not set and the car is asleep, we will print a warning and exit. You can set this after instantiation by a call to auto_wake().

Default: False.

    delay

Optional, Integer: The number of seconds to cache data returned from Tesla's API.

Default: 2

auto_wake($bool)

Informs this software if we should automatically wake a vehicle for calls that require it online, and the vehicle is currently offline.

Send in a true value to allow us to do this.

Default: False

Vehicle Summary Methods

in_service

Returns a bool whether your vehicle is in service mode or not.

option_codes

Returns a comma-delimited list of option codes of the options that are enabled on your vehicle.

vehicle_id

Returns an integer of Tesla's representation of the vehicle identification of your vehicle. This is not the same as the ID you use to access the API.

vin

Returns an alpha-numeric string that contains the actual Vehicle Identification Number of your vehicle. This value is located on a stamped plate on the driver's side bottom on the outside of your windshield.

Vehicle Identification Methods

id($id)

Sets/gets your primary vehicle ID. If set, we will use this in all API calls that require it.

Parameters:

    $id

Optional, Integer: The vehicle ID you want to use in all API calls that require one. This can be set as a parameter in new(). If you attempt an API call that requires and ID and one isn't set, we croak().

If you only have a single Tesla vehicle registered under your account, we will set my_vehicle_id() to that ID when you instantiate the object.

You can also have this auto-populated in new() by sending it in with the id => $id parameter.

If you don't know the ID of the vehicle you want to use, make a call to list(), and it will return a hash reference where each key is a vehice ID, and the value is the name you've assigned your vehicle.

name

Returns the name you associated with your vehicle under your Tesla account.

NOTE:"id($id)" must have already been set, either through the id() method, or in new().

list

Returns a hash reference of your listed vehicles. The key is the vehicle ID, and the value is the name you've assigned to that vehicle.

Example:

    {
        1234567891011 => "Dream machine",
        1234567891012 => "Steve's Model S",
    }

Command Methods

wake

Wakes up an offline Tesla vehicle.

Most Tesla API calls related to your vehicle require the vehicle to be in an online state. If auto_wake() isn't set and you attempt to make an API call that requires the vehicle online, we will print a warning and exit.

Use this method to wake the vehicle up manually.

Default wake timeout is 30 seconds, and is set in the constant WAKE_TIMEOUT.

Aggregate Data Methods

These methods aggregate all attributes of the vehicle that relate to a specific aspect of the vehicle. Methods that allow access to individual attributes of these larger aggregates are listed below. For example, charge_state() will return the battery_level attribute, but so will battery_level(). By using the aggregate method, you'll have to fish that attribute out yourself.

data

Returns a hash reference containing all available API data that Tesla provides for your vehicles.

croak()s if you haven't specified a vehicle ID through new() or id(), and we weren't able to figure one out automatically.

This data will be retained and re-used for a period of two (2) seconds to reduce API calls through the Tesla API. This timing can be overridden in the new() method by specifying the refresh => $seconds parameter, or by a call to the object's delay($seconds) method.

Return: Hash reference. Contains every attribute Tesla has available through their API for their vehicles.

The data accessor methods listed below use this data, simply selecting out individual parts of it.

summary

Returns an important list of information about your vehicle, and Tesla's API access.

The most important piece of information is the vehicle's state, which shows whether the car is online or not. Other information includes in_service, vin, the display_name etc.

Return: Hash reference.

state

Returns the vehicle_state section of Tesla's vehicle data. This includes things like whether the car is locked, whether there is media playing, the odometer reading, whether sentry mode is enabled or not etc.

Return: Hash reference.

charge_state

Returns information regarding battery and charging information of your vehicle.

Return: Hash reference.

climate_state

Returns information regarding the climate state of the vehicle.

Return: Hash reference.

drive_state

Returns the information about the operation of the vehicle.

Return: Hash reference.

vehicle_config

Returns attributes related to the actual configuration of your vehicle.

Return: Hash reference.

Vehicle State Attribute Methods

dashcam

Returns a string of the state of the dashcam (eg. "Recording").

online

Is the vehicle online or asleep?

odometer

Returns the number of miles the vehicle is traveled since new, as a floating point number.

sentry_mode

Returns a bool indicating whether the vehicle is in sentry mode or not.

santa_mode

Returns a bool whether the vehicle is in "Santa" mode or not.

user_present

Returns a bool indicating whether someone with a valid FOB key is in proximity of the vehicle.

Drive State Attribute Methods

Retrieves information regarding the actual operation and location of the vehicle.

gear

Returns a single alpha character representing the gear the vehicle is in.

One of P for parked, N for Neutral, D for Drive and R for reverse.

gps_as_of

Returns an integer that is the timestamp that the GPS data was last refreshed from the vehicle.

heading

Returns an integer between 0 and 360 which is the current compass heading of the vehicle.

latitude

Returns a signed float of the current Latitude of the vehicle.

longitude

Returns a signed float of the current Longitude of the vehicle.

power

Returns a signed float that contains the current kWh (Kilowatt-hours) the car is currently consuming in its operation.

A negative value indicates that either the car is plugged in and charging, or that the regenerative brakes are engaged and are replenishing the battery (eg. the car is going downhill and the car is decelerating).

speed

Returns a float of the vehicle's speed in MPH.

Charge State Attribute Methods

battery_level

Returns an integer of the percent that the battery is charged to.

charging_state

Returns a string that identifies the state of the vehicle's charger. Eg. "Disconnected", "Connected" etc.

charge_amps

Returns a float indicating how many Amps the vehicle is set to draw through the current charger connection.

charge_actual_current

Returns a float indicating how many Amps are actually being drawn through the charger.

charge_limit_soc

Returns an integer stating what percentage of battery level you've indicated the charging will be cut off at.

"soc" stands for "State of Charge"

charge_limit_soc_std

Returns an integer stating Tesla's default charge_limit_soc is set to.

charge_limit_soc_min

Returns an integer stating what the minimum number you can set as the Charge Limit SOC (charge_limit_soc).

charge_limit_soc_max

Returns an integer stating what the maximum number you can set as the Charge Limit SOC (charge_limit_soc).

charge_port_color

Returns a string containing the color of the vehicle's charge port (eg. "Green Flashing" etc).

charger_voltage

Returns a float containing the actual Voltage level that the charger is connected through.

minutes_to_full_charge

Returns an integer containing the estimated number of minutes to fully charge the batteries, taking into consideration voltage level, Amps requested and drawn etc.

Climate State Attribute Methods

bioweapon_mode

Yes, this is truly a thing. At least my Tesla vehicle has a mode that seals the vehicle from all outside air, and puts positive pressure inside the cabin to ensure that no contaminents can enter the vehicle.

This method returns a bool to indicate whether this mode is enabled or not.

defroster_front

Is the front windshield defroster on or not

defroster_rear

Is the rear window defroster on or not.

fan_status

Returns an integer that represents the climate fan speed.

heater_battery

Is the battery warmer on or not.

heater_seat_driver

Is the driver's seat warmer on.

heater_seat_passenger

Is the passenger's seat warmer on.

heater_side_mirrors

Is the wing mirror heater on.

heater_steering_wheel

Is the steering wheel warmer on or not

heater_wipers

Is the windshield wiper warmer on.

is_climate_on

Is the climate system currently active.

is_air_conditioning_on

Is the air conditioning unit active.

temperature_inside

The current temperature inside of the vehicle cabin.

temperature_outside

The temperature outside of the vehicle.

temperature_setting_driver

What the driver's side temperature setting is set to.

temperature_setting_passenger

What the passenger's side temperature setting is set to.

AUTHOR

Steve Bertrand, <steveb at cpan.org>

LICENSE AND COPYRIGHT

Copyright 2022 Steve Bertrand.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0