From 11d15006449abc907f1bf187a4dc1c56874f3a34 Mon Sep 17 00:00:00 2001 From: Raphael Murray Date: Sun, 16 Jun 2024 23:27:24 +0200 Subject: [PATCH] fix startup bug and update logging - Fix extension stopping at first MQTT listening - Update logging and documentation - Improve pairing procedure doc and logging - Improve HA entities classification - Cleanup --- README.md | 14 -- tesla_ble_mqtt/CHANGELOG.md | 10 + tesla_ble_mqtt/README.md | 57 +++-- tesla_ble_mqtt/config.yaml | 2 +- tesla_ble_mqtt/rootfs/app/discovery.sh | 233 ++++++++++---------- tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh | 14 +- tesla_ble_mqtt/rootfs/app/run.sh | 23 +- 7 files changed, 196 insertions(+), 157 deletions(-) diff --git a/README.md b/README.md index 17fc4c5..2aefba0 100644 --- a/README.md +++ b/README.md @@ -12,20 +12,6 @@ This package contains: ## BLE Pairing with your car -- Navigate to the Tesla_BLE_MQTT Device info. - - In HA : Settings -> Devices & Services -> Devices (tab) -> Tesla_BLE_MQTT -- Press : Generate Keys (required once) -- Press : Deploy Key (car may need to be awake - TBC) - - If the command succeed to initiate the pairing with the car, the following will show in the add-on logs: - `Sent add-key request to _YOUR_CAR_VIN. Confirm by tapping NFC card on center console.` - - Tap your NFC card on the center console - - On the car's screen `Phone Key pairing request`, confirm your accept the pairing - - - If the command failed, the following error will show up: - `Error: failed to find BLE beacon for _YOUR_CAR_VIN_. (S________________C): can’t scan: context deadline exceeded` - - You car might just be too far from your Bluetooth adapter. - -- Credits : Shankar Kumarasamy's [Blog](https://shankarkumarasamy.blog/2024/01/28/tesla-developer-api-guide-ble-key-pair-auth-and-vehicle-commands-part-3/) ## Add-ons diff --git a/tesla_ble_mqtt/CHANGELOG.md b/tesla_ble_mqtt/CHANGELOG.md index ea17c43..5f58a2b 100644 --- a/tesla_ble_mqtt/CHANGELOG.md +++ b/tesla_ble_mqtt/CHANGELOG.md @@ -1,5 +1,15 @@ +## 0.0.5 + +### Changed + +- Fix extension stopping at first MQTT listening +- Update logging and documentation +- Improve pairing procedure doc and logging +- Improve HA entities classification +- Cleanup + ## 0.0.4 ### Changed diff --git a/tesla_ble_mqtt/README.md b/tesla_ble_mqtt/README.md index 68bb5ce..7f72d59 100644 --- a/tesla_ble_mqtt/README.md +++ b/tesla_ble_mqtt/README.md @@ -1,30 +1,31 @@ # Home Assistant Add-on: Tesla Local Commands +Send commands via MQTT to a Tesla car using Bluetooth Low Energy (BLE) + + This Addon is a package of iainbullock's https://github.com/iainbullock/tesla_ble_mqtt_docker It runs the official Tesla Vehicle SDK commands via BLE to activate various entities in your Tesla. -This is to bypass the current Fleet API rate limitation. - -It does not rely on the API. +This is to bypass the current Fleet API rate limitation as it does not rely on the API. This package contains: - All the code generated by iainbullock repackaged as a HA addon - A `standalone` folder which contains a single script to run the BLE service on a separate machine -# Tesla BLE MQTT Docker -Send commands via MQTT to a Tesla car using Bluetooth Low Energy (BLE) - -If Home Assistant (HA) is already using the MQTT integration, then the various entities will be auto-discovered by HA +# Prerequisites +You must already have a working MQTT broker. If you want the entities to be auto-discovered by Home Assistant (HA), then the HA MQTT Integration must already be set up and working. See: https://www.home-assistant.io/integrations/mqtt/ The advantage of the MQTT setup is that it can run on a device separate to your HA server, e.g. Raspberry Pi located close to where you park your car -## Installation and setup -1 You must already have a working MQTT broker. If you want the entities to be auto-discovered by Home Assistant (HA), then the HA MQTT Integration must already be set up and working. See: https://www.home-assistant.io/integrations/mqtt/ +# Installation and setup -Note that in both cases below, if you have already created a key pair that you want to reuse, place the private key in `/share/tesla_ble_mqtt` +If you have already created a key pair that you want to reuse, place the private key in `/share/tesla_ble_mqtt` + +## 1.1 HA Add-on: install below and configure + +[![Open your Home Assistant instance and show the add add-on repository dialog with a specific repository URL pre-filled.](https://my.home-assistant.io/badges/supervisor_add_addon_repository.svg)](https://my.home-assistant.io/redirect/supervisor_add_addon_repository/?repository_url=https://github.com/raphmur/tesla-local-control-addon) -2.1 For the HA Add-on: install below and configure. You will need to provide: - TESLA_VIN (car VIN) @@ -35,20 +36,40 @@ You will need to provide: - MQTT_PWD - SEND_CMD_RETRY_DELAY: delay between retries in case BLE fails. Use 5 by default -[![Open your Home Assistant instance and show the add add-on repository dialog with a specific repository URL pre-filled.](https://my.home-assistant.io/badges/supervisor_add_addon_repository.svg)](https://my.home-assistant.io/redirect/supervisor_add_addon_repository/?repository_url=https://github.com/raphmur/tesla-local-control-addon) -2.2 For the standalone version, it has been tested on RPi 3B so far. Here are the assumptions and way forward: +## 1.2 For the standalone version + +It has been tested on RPi 3B so far. Here are the assumptions and way forward: - You already have Docker working on the host device, and you are familiar with basic Docker concepts and actions -- Clone the self packaged shell script: `https://github.com/raphmur/tesla-local-control-addon/blob/main/standalone/start_tesla_ble_mqtt.sh` +- Clone the self packaged shell script: `wget https://github.com/raphmur/tesla-local-control-addon/blob/main/standalone/start_tesla_ble_mqtt.sh` - Edit the script to input your own settings: TESLA_VIN, MQTT_IP (ip of your MQTT server), MQTT_PORT, MQTT_USER, MQTT_PWD, SEND_CMD_RETRY_DELAY (delay between retries in case BLE fails), _optional_ _BLE_MAC_ (to be used for proximity discovery) - run the script: `./start_tesla_ble_mqtt.sh`, it will download the rest of the elements, build and deploy the container -3 THEN: +## 2 Pairing with your car + +- Navigate to the Tesla_BLE_MQTT Device info. + - In HA : Settings -> Devices & Services -> Devices (tab) -> Tesla_BLE_MQTT +- Press : Generate Keys (required once) +- Press : Deploy Key (car may need to be awake - TBC) + - If the command succeed to initiate the pairing with the car, the following will show in the add-on logs: + `Sent add-key request to _YOUR_CAR_VIN. Confirm by tapping NFC card on center console.` + - Tap your NFC card on the center console + - On the car's screen `Phone Key pairing request`, confirm your accept the pairing + + - If the command failed, the following error will show up: + `Error: failed to find BLE beacon for _YOUR_CAR_VIN_. (S________________C): can’t scan: context deadline exceeded` + - You car might just be too far from your Bluetooth adapter. + +- Credits : Shankar Kumarasamy's [Blog](https://shankarkumarasamy.blog/2024/01/28/tesla-developer-api-guide-ble-key-pair-auth-and-vehicle-commands-part-3/) + +## 3 THEN - A new device called Tesla_BLE_MQTT should have automatically appeared. Click it to view the the associated entities. You should find a list of Button entities and a Number entity -- If this is the first time you have run the container, press the 'Generate Keys' button. This will generate the public and private keys as per Shanker's blog -- Wake up your car using the Tesla App. Then press the 'Deploy Key' button. This will deploy the public key to the car. You will then need to access your car and use a Key Card to accept the public key into the car (see the blog for screenshots) -- Then you are ready. Press the other button entities to send various commands, or change the Charging Current. You can use the relevant service calls in HA automations if you wish +- If this is the first time you have run the container, press the 'Generate Keys' button in HA (Settings -> Devices & Services -> Devices (tab) -> Tesla_BLE_MQTT). This will generate the public and private keys as per Shanker's blog +- **Wake up your car using the Tesla App**. Then press the 'Deploy Key' button. This will deploy the public key to the car. You will then need to access your car and use a Key Card to accept the public key into the car (see the blog for screenshots) + - If the command succeed to initiate the pairing with the car, the following will show in the add-on logs: `Sent add-key request to [YOUR_CAR_VIN]. Confirm by tapping NFC card on center console.` Go in your car and tap your NFC card on the center console and on the car's screen `Phone Key pairing request`, confirm your accept the pairing + - If the command failed, the following error will show up: `Error: failed to find BLE beacon for [YOUR_CAR_VIN]. (xxx): can’t scan: context deadline exceeded`. You car might just be too far from your Bluetooth adapter. The command will be tried in case bluetooth is weak or unavailable... +- Then you are ready. Press the other button entities to send various commands... You can use the relevant service calls in HA automations if you wish ## Credits diff --git a/tesla_ble_mqtt/config.yaml b/tesla_ble_mqtt/config.yaml index 0f9fcce..74117b8 100644 --- a/tesla_ble_mqtt/config.yaml +++ b/tesla_ble_mqtt/config.yaml @@ -1,5 +1,5 @@ name: "Tesla Local Commands" -version: "0.0.3zb" +version: "0.0.5" slug: "tesla_local_commands" description: "Local BLE calls to control your Tesla." # url: "tbc" diff --git a/tesla_ble_mqtt/rootfs/app/discovery.sh b/tesla_ble_mqtt/rootfs/app/discovery.sh index 245bfee..81aaf0e 100644 --- a/tesla_ble_mqtt/rootfs/app/discovery.sh +++ b/tesla_ble_mqtt/rootfs/app/discovery.sh @@ -7,180 +7,183 @@ setup_auto_discovery() { "state_topic": "tesla_ble/binary_sensor/presence", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "device_class": "presence", "name": "Presence", "unique_id": "tesla_ble_presence" }' - + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/generate_keys/config -m \ '{ "command_topic": "tesla_ble/config", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "device_class": "update", "name": "Generate Keys", "payload_press": "generate_keys", "qos": 1, - "unique_id": "tesla_ble_generate_keys" - }' + "unique_id": "tesla_ble_generate_keys", + "entity_category": "config" + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/deploy_key/config -m \ '{ "command_topic": "tesla_ble/config", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "device_class": "update", "name": "Deploy Key", "payload_press": "deploy_key", "qos": 1, - "unique_id": "tesla_ble_deploy_key" - }' + "unique_id": "tesla_ble_deploy_key", + "entity_category": "config" + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/scan_bluetooth/config -m \ '{ "command_topic": "tesla_ble/config", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "device_class": "update", "name": "Scan Bluetooth", "payload_press": "scan_bluetooth", "qos": 1, - "unique_id": "tesla_ble_scan_bluetooth" - }' + "unique_id": "tesla_ble_scan_bluetooth", + "entity_category": "diagnostic" + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/wake/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Wake Car", "payload_press": "wake", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_wake" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/flash-lights/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Flash Lights", "payload_press": "flash-lights", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_flash_lights" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/honk/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Honk", "payload_press": "honk", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_honk" - }' - + }' + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/lock/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Lock Car", "payload_press": "lock", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_lock" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/unlock/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Unlock Car", "payload_press": "unlock", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_unlock" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/auto_seat-climate/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Auto Seat & Climate", "payload_press": "auto-seat-and-climate", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_auto_seat-climate" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/climate-off/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Climate Off", "payload_press": "climate-off", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_climate-off" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/climate-on/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Climate On", "payload_press": "climate-on", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_climate-on" }' @@ -189,166 +192,168 @@ setup_auto_discovery() { "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Open Trunk", "payload_press": "trunk-open", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_trunk-open" - }' - + }' + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/trunk-close/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Close Trunk", "payload_press": "trunk-close", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_trunk-close" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/frunk-open/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Open Frunk", "payload_press": "frunk-open", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_frunk-open" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/charging-start/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Start Charging", "payload_press": "charging-start", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_charging-start" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/charging-stop/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Stop Charging", "payload_press": "charging-stop", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_charging-stop" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/charge-port-open/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Open Charge Port", "payload_press": "charge-port-open", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_charge-port-open" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/charge-port-close/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Close Charge Port", "payload_press": "charge-port-close", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_charge-port-close" - }' - + }' + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/windows-close/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Close Windows", "payload_press": "windows-close", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_windows-close" - }' - + }' + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/windows-vent/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Vent Windows", "payload_press": "windows-vent", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_windows-vent" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/product-info/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Product Info", "payload_press": "product-info", - "qos": 1, + "qos": 1, "enabled_by_default": 0, - "unique_id": "tesla_ble_product-info" - }' + "unique_id": "tesla_ble_product-info", + "entity_category": "diagnostic" + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/session-info/config -m \ '{ "command_topic": "tesla_ble/command", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Session Info", "payload_press": "session-info", - "qos": 1, + "qos": 1, "enabled_by_default": 0, - "unique_id": "tesla_ble_session-info" + "unique_id": "tesla_ble_session-info", + "entity_category": "diagnostic" }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/number/tesla_ble/charging-set-amps/config -m \ @@ -356,8 +361,8 @@ setup_auto_discovery() { "command_topic": "tesla_ble/charging-set-amps", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Charging Current", @@ -368,15 +373,15 @@ setup_auto_discovery() { "unit_of_measurement": "A", "qos": 1, "icon": "mdi:current-ac" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/number/tesla_ble/charging-set-limit/config -m \ '{ "command_topic": "tesla_ble/charging-set-limit", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Charging Limit", @@ -387,15 +392,15 @@ setup_auto_discovery() { "unit_of_measurement": "%", "qos": 1, "icon": "mdi:battery-90" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/number/tesla_ble/climate-set-temp/config -m \ '{ "command_topic": "tesla_ble/climate-set-temp", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Climate Temp", @@ -406,15 +411,15 @@ setup_auto_discovery() { "unit_of_measurement": "°C", "qos": 1, "icon": "mdi:temperature" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/number/tesla_ble/seat-heater/config -m \ '{ "command_topic": "tesla_ble/seat-heater", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Seat Heater", @@ -424,20 +429,20 @@ setup_auto_discovery() { "mode": "slider", "qos": 1, "icon": "mdi:temperature" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/switch/tesla_ble/sw-heater/config -m \ '{ "command_topic": "tesla_ble/sw-heater", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Steering Wheel Heater", - "device_class": "switch", - "qos": 1, + "device_class": "switch", + "qos": 1, "unique_id": "tesla_ble_sw_heater" }' @@ -446,13 +451,13 @@ setup_auto_discovery() { "command_topic": "tesla_ble/sentry-mode", "device": { "identifiers": ["tesla_ble_mqtt"], - "manufacturer": "iainbullock", - "model": "tesla_ble_mqtt", + "manufacturer": "tesla_ble_mqtt", + "model": "Tesla BLE", "name": "Tesla_BLE_MQTT" }, "name": "Sentry Mode", "device_class": "switch", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_sentry-mode" }' diff --git a/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh b/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh index 10015fd..a95bc89 100644 --- a/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh +++ b/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh @@ -19,10 +19,13 @@ listen_to_mqtt() { echo "Generating the public key" openssl ec -in /share/tesla_ble_mqtt/private.pem -pubout > /share/tesla_ble_mqtt/public.pem cat public.pem - echo "Keys generated, ready to deploy to vehicle. Remove any previously deployed BLE keys from vehicle before deploying this one";; + echo "KEYS GENERATED. Next:" + echo "1/ Remove any previously deployed BLE keys from vehicle before deploying this one" + echo "2/ Wake the car up with your Tesla App" + echo "3/ Push the button Deploy Key";; deploy_key) echo "Deploying public key to vehicle" - tesla-control -ble -vin $TESLA_VIN add-key-request /share/tesla_ble_mqtt/public.pem owner cloud_key;; + send_key; *) echo "Invalid Configuration request. Topic: $topic Message: $msg";; esac;; @@ -54,8 +57,8 @@ listen_to_mqtt() { auto-seat-and-climate) echo "Start Auto Seat and Climate" send_command $msg;; - climate-off) - echo "Stop Climate" + climate-on) + echo "Start Climate" send_command $msg;; climate-off) echo "Stop Climate" @@ -75,9 +78,6 @@ listen_to_mqtt() { unlock) echo "Unlock Car" send_command $msg;; - unlock) - echo "Unlock Car" - send_command $msg;; windows-close) echo "Close Windows" send_command $msg;; diff --git a/tesla_ble_mqtt/rootfs/app/run.sh b/tesla_ble_mqtt/rootfs/app/run.sh index 4dd0c93..c42ec21 100644 --- a/tesla_ble_mqtt/rootfs/app/run.sh +++ b/tesla_ble_mqtt/rootfs/app/run.sh @@ -51,6 +51,22 @@ send_command() { done } +send_key() { + for i in $(seq 5); do + echo "Attempt $i/5" + tesla-control -ble -vin $TESLA_VIN add-key-request /share/tesla_ble_mqtt/public.pem owner cloud_key + EXIT_STATUS=$? + set -e + if [ $EXIT_STATUS -eq 0 ]; then + echo "KEY SENT TO VEHICLE: PLEASE CHECK YOU TESLA'S SCREEN AND ACCEPT WITH YOUR CARD" + break + else + echo "COULD NOT SEND THE KEY. Is the car awake and sufficiently close to the bluetooth device?" + sleep $SEND_CMD_RETRY_DELAY + fi + done +} + listen_to_ble() { echo "Listening to BLE" set +e @@ -76,15 +92,16 @@ setup_auto_discovery echo "Connecting to MQTT to discard any unread messages" mosquitto_sub -E -i tesla_ble_mqtt -h $MQTT_IP -p $MQTT_PORT -u $MQTT_USER -P $MQTT_PWD -t tesla_ble/+ -echo "Initialize counter" +echo "Initialize BLE listening loop counter" counter=0 -echo "Entering listening loop" +echo "Entering main MQTT & BLE listening loop" while true do + set +e listen_to_mqtt ((counter++)) if [[ $counter -gt 90 ]]; then - echo "Reached 90 loops (~3min): scanning car presence" + echo "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence" listen_to_ble counter=0 fi