介绍
# Tesla Fleet API
通过官方 Fleet API 控制 Tesla 车辆。
## 脚本概览
| 脚本 | 用途 | |--------|---------| | `command.py` | 车辆指令(空调、充电、门锁等) | | `vehicle_data.py` | 读取车辆数据(电池、空调、位置等) | | `vehicles.py` | 列出车辆 + 刷新缓存 | | `auth.py` | 认证与配置 | | `tesla_oauth_local.py` | 带有本地回调服务器的 OAuth 辅助工具 | | `start_proxy.sh` | 启动签名代理(用于车辆指令) | | `stop_proxy.sh` | 停止签名代理 |
---
## 设置 / 配置
设置文档位于 **`SETUP.md`**:
- [SETUP.md](SETUP.md)
状态目录:`{workspace}/tesla-fleet-api/` - `config.json`(提供商凭据 + 非令牌配置) - `auth.json`(令牌) - `vehicles.json`(缓存的车辆列表) - `places.json`(命名地点) - `proxy/`(签名代理的 TLS 材料)
不加载 `.env` 文件 —— 凭据位于 `config.json` 或环境变量中。
---
## command.py - 车辆指令
在您的 Tesla 上执行指令。如果您只有一辆车,会自动选择该车辆。
### 用法
```bash command.py [VEHICLE] <command> [options] ```
- `VEHICLE` - 车辆名称或 VIN(如果只有一辆车则为可选) - 可以在不指定车辆的情况下运行指令:`command.py honk` - 或指定车辆名称:`command.py flash honk`(车辆 "flash",指令 "honk")
---
### 空调控制
#### 开启/停止空调 ```bash command.py climate start command.py climate stop command.py flash climate start # specific vehicle ```
#### 设置温度 ```bash command.py climate temps <driver_temp> [passenger_temp] command.py climate temps 21 # both seats 21°C command.py climate temps 22 20 # driver 22°C, passenger 20°C ```
#### 恒温模式 ```bash command.py climate keeper <mode> ``` 模式:`off`, `keep`, `dog`, `camp`
---
### 座椅加热
```bash command.py seat-heater --level <level> [--position <position>] command.py seat-heater -l <level> [-p <position>] ```
**档位:** | 值 | 名称 | |-------|------| | 0 | off | | 1 | low | | 2 | medium | | 3 | high |
**位置:** | 值 | 名称 | |-------|-------| | 0 | `driver`, `front_left`, `fl` | | 1 | `passenger`, `front_right`, `fr` | | 2 | `rear_left`, `rl` | | 3 | `rear_left_back` | | 4 | `rear_center`, `rc` | | 5 | `rear_right`, `rr` | | 6 | `rear_right_back` | | 7 | `third_left` | | 8 | `third_right` |
**示例:** ```bash command.py seat-heater -l high # driver (default) command.py seat-heater -l medium -p passenger command.py seat-heater --level low --position rear_left command.py seat-heater -l 2 -p 4 # medium, rear center command.py seat-heater -l off -p driver # turn off ```
---
### 座椅通风
```bash command.py seat-cooler --level <level> [--position <position>] command.py seat-cooler -l <level> [-p <position>] ```
档位和位置与座椅加热相同。
**示例:** ```bash command.py seat-cooler -l medium -p driver command.py seat-cooler -l high -p passenger ```
---
### 座椅自动空调
```bash command.py seat-climate [--position <position>] <mode> command.py seat-climate [-p <position>] <mode> ```
模式:`auto`, `on`, `off`
**示例:** ```bash command.py seat-climate auto # driver auto command.py seat-climate -p passenger auto command.py seat-climate -p driver off # disable auto ```
---
### 方向盘加热
```bash command.py steering-heater <on|off> ```
**示例:** ```bash command.py steering-heater on command.py steering-heater off ```
---
### 预约定时
用于规划出发预热的现代 API(取代已弃用的 `set_scheduled_departure`)。
#### 添加定时 ```bash command.py precondition add --time <HH:MM> [--days <days>] [--id <id>] [--one-time] [--disabled] command.py precondition add -t <HH:MM> [-d <days>] [--id <id>] ```
**天数选项:** | 值 | 描述 | |-------|-------------| | `all` | 每天(默认) | | `weekdays` | 周一到周五 | | `weekends` | 周六和周日 | | `mon,tue,wed,...` | 特定日期(逗号分隔) |
日期名称:`sun`, `mon`, `tue`, `wed`, `thu`, `fri`, `sat`(或全名)
**示例:** ```bash command.py precondition add -t 08:00 # every day at 8am command.py precondition add -t 08:00 -d weekdays # Mon-Fri command.py precondition add -t 07:30 -d mon,wed,fri command.py precondition add -t 09:00 --one-time # one-time only command.py precondition add -t 08:30 --id 123 # modify existing schedule command.py precondition add -t 08:00 --disabled # create but disabled ```
#### 移除定时 ```bash command.py precondition remove --id <id> ```
**示例:** ```bash command.py precondition remove --id 123 command.py precondition remove --id 1 ```
---
### 充电控制
#### 开始/停止充电 ```bash command.py charge start command.py charge stop ```
#### 设置充电限制 ```bash command.py charge limit <percent> ```
百分比必须在 50-100 之间。
**示例:** ```bash command.py charge limit 80 command.py charge limit 90 command.py flash charge limit 70 # specific vehicle ```
---
### 车门与安全
```bash command.py lock # lock all doors command.py unlock # unlock all doors command.py honk # honk the horn command.py flash # flash the lights command.py wake # wake vehicle from sleep ```
**指定车辆名称:** ```bash command.py flash wake # wake vehicle named "flash" command.py flash flash # flash lights on vehicle "flash" ```
---
## vehicle_data.py - 读取车辆数据
默认获取具有人类可读输出的车辆数据。
### 用法
```bash vehicle_data.py [VEHICLE] [flags] [--json] ```
- `VEHICLE` - 车辆名称或 VIN(如果只有一辆车则为可选) - 无标志 = 所有数据 - `--json` = 原始 JSON 输出
### 标志
| 标志 | 长选项 | 数据 | |------|------|------| | `-c` | `--charge` | 电量等级、充电限制、充电状态 | | `-t` | `--climate` | 车内/车外温度、HVAC 状态 | | `-d` | `--drive` | 挡位、速度、功率、航向 | | `-l` | `--location` | GPS 坐标 | | `-s` | `--state` | 锁、门、窗、里程、软件版本 | | `-g` | `--gui` | GUI 设置(单位、24小时制) | | | `--config-data` | 车辆配置(型号、颜色、轮毂) |
### 示例
```bash # All data vehicle_data.py vehicle_data.py flash
# Specific data vehicle_data.py -c # charge only vehicle_data.py -c -t # charge + climate vehicle_data.py flash -c -l # charge + location
# Raw JSON vehicle_data.py --json vehicle_data.py -c --json ```
### 输出示例
``` 🚗 My Tesla (online) VIN: 5YJ... (redacted)
⚡ Charge State ──────────────────────────────────────── Battery: [███████████████░░░░░] 78% Limit: 80% State: Charging Power: 11 kW (16A × 234V × 3φ) Added: 37.2 kWh Remaining: 10m Range: 438 km (272 mi) Cable: IEC
🌡️ Climate State ──────────────────────────────────────── Inside: 11.9°C Outside: 6.0°C Set to: 20.5°C Climate: Off ```
---
## auth.py - 认证
管理 OAuth 令牌和配置。
### 用法
```bash auth.py <command> [options] ```
### 指令
#### 登录 (OAuth 流程) ```bash auth.py login ``` 交互式:生成认证 URL,提示输入代码,交换令牌。
#### 交换代码 ```bash auth.py exchange <code> ``` 交换授权代码以获取令牌(非交互式)。
#### 刷新令牌 ```bash auth.py refresh ``` 刷新访问令牌。注意:刷新令牌会轮换 —— 新令牌会自动保存。
#### 注册域名 ```bash auth.py register --domain <domain> ``` 向 Tesla 注册您的应用域名(签名指令所必需)。
注册后,登记您的虚拟密钥: ``` https://tesla.com/_ak/<domain> ```
#### 显示配置 ```bash auth.py config ``` 显示当前配置(已隐藏敏感信息)。
#### 设置配置 ```bash auth.py config set [options] ```
选项: - `--client-id <id>` - `--client-secret <secret>` - `--redirect-uri <uri>` - `--audience <url>` - `--base-url <url>` - `--ca-cert <path>` - `--domain <domain>`
**示例:** ```bash # Initial setup auth.py config set \ --client-id "abc123" \ --client-secret "secret" \ --redirect-uri "http://localhost:18080/callback"
# Configure proxy auth.py config set \ --base-url "https://localhost:4443" \ --ca-cert "/path/to/tls-cert.pem" ```
---
## tesla_fleet.py - 列出车辆
列出车辆并显示人类可读的输出。
```bash python3 scripts/tesla_fleet.py vehicles python3 scripts/tesla_fleet.py vehicles --json ```
### 输出示例
``` 🚗 Name: My Tesla 🔖 VIN: 5YJ... (redacted) 🟢 Status: Online 👤 Access: Owner ```
---
## 配置 / 代理 / 文件布局
所有设置 + 配置均记录在 **[SETUP.md](SETUP.md)** 中。
---
## 区域基础 URL
| 区域 | Audience URL | |--------|--------------| | 欧洲 | `https://fleet-api.prd.eu.vn.cloud.tesla.com` | | 北美 | `https://fleet-api.prd.na.vn.cloud.tesla.com` | | 中国 | `https://fleet-api.prd.cn.vn.cloud.tesla.cn` |
OAuth 令牌端点(所有区域): ``` https://fleet-auth.prd.vn.cloud.tesla.com/oauth2/v3/token ```
---
## 故障排除
### "vehicle unavailable: vehicle is offline or asleep" 请先唤醒车辆: ```bash command.py wake ```
### "command not signed" / "vehicle rejected" 确保签名代理正在运行并已配置。请参阅 [SETUP.md](SETUP.md) § Proxy Setup。
### Token expired(令牌过期) ```bash auth.py refresh ```
### 多辆车辆 通过名称或 VIN 指定车辆: ```bash command.py flash climate start command.py 5YJ... honk ```
---
## 完整指令参考
### command.py
``` climate start|stop climate temps <driver> [passenger] climate keeper off|keep|dog|camp
seat-heater -l <level> [-p <position>] seat-cooler -l <level> [-p <position>] seat-climate [-p <position>] auto|on|off
steering-heater on|off
precondition add -t <HH:MM> [-d <days>] [--id <id>] [--one-time] precondition remove --id <id>
charge start|stop charge limit <percent>
lock unlock honk flash wake ```
### vehicle_data.py
``` [VEHICLE] [-c] [-t] [-d] [-l] [-s] [-g] [--config-data] [--json] ```
### auth.py
``` login exchange <code> refresh register --domain <domain> config config set [--client-id] [--client-secret] [--redirect-uri] [--audience] [--base-url] [--ca-cert] [--domain] ```