Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions tesla_ble_mqtt/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
<!-- https://developers.home-assistant.io/docs/add-ons/presentation#keeping-a-changelog -->

## 0.0.7a

### Changed

WARNING: broken for standalone deployment, stay on 0.0.6a
OK for HA Addon

- logging fix debug and adjustment
- prevent always updating presence topic
- prevent checking for presence if BLE_MAC is empty

## 0.0.7

### Changed

WARNING: broken for standalone deployment, stay on 0.0.6
OK for HA Addon

- Bump to latest updates from tesla_ble_mqtt_docker
- Improve HA logging and error management
Expand Down Expand Up @@ -55,4 +65,4 @@ OK for HA Addon
### Changed

- Initial dev version based on https://github.com/iainbullock/tesla_ble_mqtt_docker
- WARN: standalone not tested
- WARN: standalone not tested
4 changes: 2 additions & 2 deletions tesla_ble_mqtt/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Tesla Local Commands"
version: "0.0.7"
version: "0.0.7a"
slug: "tesla_local_commands"
description: "Local BLE calls to control your Tesla."
# url: "tbc"
Expand Down Expand Up @@ -31,7 +31,7 @@ schema:
debug: bool
ble_mac: str?
mqtt_ip: str?
mqtt_port: str?
mqtt_port: port
mqtt_user: str?
mqtt_pwd: password?
send_cmd_retry_delay: str?
Expand Down
74 changes: 37 additions & 37 deletions tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh
Original file line number Diff line number Diff line change
@@ -1,136 +1,136 @@
#!/bin/ash

listen_to_mqtt() {
bashio::log.info "Connecting to MQTT server; subscribe topics tesla_ble/+ and homeassistant/status"
bashio::log.debug "Connecting to MQTT server; subscribe topics tesla_ble/+ and homeassistant/status"
mosquitto_sub --nodelay -E -c -i tesla_ble_mqtt -q 1 -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/+ -t homeassistant/status -F "%t %p" | \
while read -r payload
do
topic=$(echo "$payload" | cut -d ' ' -f 1)
msg=$(echo "$payload" | cut -d ' ' -f 2-)
bashio::log.green "Received MQTT message: $topic $msg"
bashio::log.info "Received MQTT message: $topic $msg"
case $topic in
tesla_ble/config)
bashio::log.green "Configuration $msg requested"
bashio::log.info "Configuration $msg requested"
case $msg in
generate_keys)
bashio::log.green "Generating the private key"
bashio::log.info "Generating the private key"
openssl ecparam -genkey -name prime256v1 -noout > /share/tesla_ble_mqtt/private.pem
cat /share/tesla_ble_mqtt/private.pem
bashio::log.green "Generating the public key"
bashio::log.info "Generating the public key"
openssl ec -in /share/tesla_ble_mqtt/private.pem -pubout > /share/tesla_ble_mqtt/public.pem
cat /share/tesla_ble_mqtt/public.pem
bashio::log.green "KEYS GENERATED. Next:
bashio::log.info "KEYS GENERATED. Next:
1/ Remove any previously deployed BLE keys from vehicle before deploying this one
2/ Wake the car up with your Tesla App
3/ Push the button 'Deploy Key'";;
deploy_key)
bashio::log.yellow "Deploying public key to vehicle"
send_key;;
*)
bashio::log.red "Invalid Configuration request. Topic: $topic Message: $msg";;
bashio::log.error "Invalid Configuration request. Topic: $topic Message: $msg";;
esac;;

tesla_ble/command)
bashio::log.green "Command $msg requested"
bashio::log.info "Command $msg requested"
case $msg in
wake)
bashio::log.green "Waking Car"
bashio::log.info "Waking Car"
send_command "-domain vcsec $msg";;
trunk-open)
bashio::log.green "Opening Trunk"
bashio::log.info "Opening Trunk"
send_command $msg;;
trunk-close)
bashio::log.green "Closing Trunk"
bashio::log.info "Closing Trunk"
send_command $msg;;
charging-start)
bashio::log.green "Start Charging"
bashio::log.info "Start Charging"
send_command $msg;;
charging-stop)
bashio::log.green "Stop Charging"
bashio::log.info "Stop Charging"
send_command $msg;;
charge-port-open)
bashio::log.green "Open Charge Port"
bashio::log.info "Open Charge Port"
send_command $msg;;
charge-port-close)
bashio::log.green "Close Charge Port"
bashio::log.info "Close Charge Port"
send_command $msg;;
climate-on)
bashio::log.green "Start Climate"
bashio::log.info "Start Climate"
send_command $msg;;
climate-off)
bashio::log.green "Stop Climate"
bashio::log.info "Stop Climate"
send_command $msg;;
flash-lights)
bashio::log.green "Flash Lights"
bashio::log.info "Flash Lights"
send_command $msg;;
frunk-open)
bashio::log.green "Open Frunk"
bashio::log.info "Open Frunk"
send_command $msg;;
honk)
bashio::log.green "Honk Horn"
bashio::log.info "Honk Horn"
send_command $msg;;
lock)
bashio::log.green "Lock Car"
bashio::log.info "Lock Car"
send_command $msg;;
unlock)
bashio::log.green "Unlock Car"
bashio::log.info "Unlock Car"
send_command $msg;;
windows-close)
bashio::log.green "Close Windows"
bashio::log.info "Close Windows"
send_command $msg;;
windows-vent)
bashio::log.green "Vent Windows"
bashio::log.info "Vent Windows"
send_command $msg;;
*)
bashio::log.red "Invalid Command Request. Topic: $topic Message: $msg";;
bashio::log.error "Invalid Command Request. Topic: $topic Message: $msg";;
esac;;

tesla_ble/charging-set-amps)
bashio::log.green "Set Charging Amps to $msg requested"
bashio::log.info "Set Charging Amps to $msg requested"
# https://github.com/iainbullock/tesla_ble_mqtt_docker/issues/4
if [ $msg -gt 4 ]; then
bashio::log.green "Set amps"
bashio::log.info "Set amps"
send_command "charging-set-amps $msg"
else
bashio::log.green "First Amp set"
bashio::log.info "First Amp set"
send_command "charging-set-amps $msg"
sleep 1
bashio::log.green "Second Amp set"
bashio::log.info "Second Amp set"
send_command "charging-set-amps $msg"
fi
;;

tesla_ble/climate-set-temp)
bashio::log.green "Set Climate Temp to $msg requested"
bashio::log.info "Set Climate Temp to $msg requested"
send_command "climate-set-temp $msg";;

tesla_ble/seat-heater)
bashio::log.green "Set Seat Heater to $msg requested"
bashio::log.info "Set Seat Heater to $msg requested"
send_command "seat-heater $msg";;

tesla_ble/auto-seat-and-climate)
bashio::log.green "Start Auto Seat and Climate"
bashio::log.info "Start Auto Seat and Climate"
send_command "auto-seat-and-climate LR on";;

tesla_ble/charging-set-limit)
bashio::log.green "Set Charging limit to $msg requested"
bashio::log.info "Set Charging limit to $msg requested"
send_command "charging-set-limit $msg";;

tesla_ble/heated_seat_left)
bashio::log.green "Set Seat heater to front-left $msg requested"
bashio::log.info "Set Seat heater to front-left $msg requested"
send_command "seat-heater front-left $msg";;

tesla_ble/heated_seat_right)
bashio::log.green "Set Seat heater to front-right $msg requested"
bashio::log.info "Set Seat heater to front-right $msg requested"
send_command "seat-heater front-right $msg";;

homeassistant/status)
# https://github.com/iainbullock/tesla_ble_mqtt_docker/discussions/6
bashio::log.green "Home Assistant is stopping or starting, re-running auto-discovery setup"
bashio::log.info "Home Assistant is stopping or starting, re-running auto-discovery setup"
setup_auto_discovery;;

*)
bashio::log.red "Invalid MQTT topic. Topic: $topic Message: $msg";;
bashio::log.error "Invalid MQTT topic. Topic: $topic Message: $msg";;
esac
done
}
65 changes: 43 additions & 22 deletions tesla_ble_mqtt/rootfs/app/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ if [ -n "${HASSIO_TOKEN:-}" ]; then
DEBUG="$(bashio::config 'debug')"; export DEBUG
fi

# Set log level to debug?
bashio::config.true debug && bashio::log.level debug

bashio::log.cyan "tesla_ble_mqtt_docker by Iain Bullock 2024 https://github.com/iainbullock/tesla_ble_mqtt_docker"
bashio::log.cyan "Inspiration by Raphael Murray https://github.com/raphmur"
bashio::log.cyan "Instructions by Shankar Kumarasamy https://shankarkumarasamy.blog/2024/01/28/tesla-developer-api-guide-ble-key-pair-auth-and-vehicle-commands-part-3"
Expand All @@ -29,32 +32,33 @@ bashio::log.green SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY

if [ ! -d /share/tesla_ble_mqtt ]
then
bashio::log.info SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY
mkdir /share/tesla_ble_mqtt
else
bashio::log.yellow "/share/tesla_ble_mqtt already exists, existing keys can be reused"
bashio::log.debug "/share/tesla_ble_mqtt already exists, existing keys can be reused"
fi


send_command() {
for i in $(seq 5); do
bashio::log.yellow "Attempt $i/5"
bashio::log.notice "Attempt $i/5 to send command"
set +e
tesla-control -ble -vin $TESLA_VIN -key-name /share/tesla_ble_mqtt/private.pem -key-file /share/tesla_ble_mqtt/private.pem $1
EXIT_STATUS=$?
set -e
if [ $EXIT_STATUS -eq 0 ]; then
bashio::log.green "Ok"
bashio::log.info "tesla-control send command succeeded"
break
else
bashio::log.red "Error calling tesla-control, exit code=$EXIT_STATUS - will retry in $SEND_CMD_RETRY_DELAY seconds"
bashio::log.error "tesla-control send command failed exit status $EXIT_STATUS. Retrying in $SEND_CMD_RETRY_DELAY"
sleep $SEND_CMD_RETRY_DELAY
fi
done
}

send_key() {
for i in $(seq 5); do
bashio::log.yellow "Attempt $i/5"
bashio::log.notice "Attempt $i/5 to send public key"
set +e
tesla-control -ble -vin $TESLA_VIN add-key-request /share/tesla_ble_mqtt/public.pem owner cloud_key
EXIT_STATUS=$?
Expand All @@ -63,49 +67,66 @@ send_key() {
bashio::log.yellow "KEY SENT TO VEHICLE: PLEASE CHECK YOU TESLA'S SCREEN AND ACCEPT WITH YOUR CARD"
break
else
bashio::log.red "COULD NOT SEND THE KEY. Is the car awake and sufficiently close to the bluetooth device?"
bashio::log.error "tesla-control could not send the key; make sure the car is awake and sufficiently close to the bluetooth device. Retrying in $SEND_CMD_RETRY_DELAY""
bashio::log.error "Retrying in $SEND_CMD_RETRY_DELAY"
sleep $SEND_CMD_RETRY_DELAY
fi
done
}

PRESENCE_BINARY_SENSOR=OFF
listen_to_ble() {
bashio::log.green "Listening to BLE"
PRESENCE_TIMEOUT=5
bashio::log.info "Listening to BLE for presence"
set +e
bluetoothctl --timeout 5 scan on | grep $BLE_MAC
bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_MAC
EXIT_STATUS=$?
set -e
if [ $? -eq 0 ]; then
bashio::log.green "$BLE_MAC presence detected"
mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON
bashio::log.info "$BLE_MAC presence detected"
if [ $PRESENCE_BINARY_SENSOR == "OFF" ]; then
bashio::log.info "Updating topic tesla_ble/binary_sensor/presence ON"
mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON
PRESENCE_BINARY_SENSOR=ON
else
bashio::log.debug "Topic tesla_ble/binary_sensor/presence already ON"
fi
else
bashio::log.yellow "$BLE_MAC presence not detected or issue in command, retrying now"
mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF
bashio::log.warning "$BLE_MAC presence not detected or issue in command, will retry later"
if [ $PRESENCE_BINARY_SENSOR == "ON" ]; then
bashio::log.info "Updating topic tesla_ble/binary_sensor/presence OFF"
mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF
PRESENCE_BINARY_SENSOR=OFF
else
bashio::log.debug "Topic tesla_ble/binary_sensor/presence already OFF"
fi
fi
}

bashio::log.green "Sourcing functions"
bashio::log.yellow "Sourcing functions"
. /app/listen_to_mqtt.sh
. /app/discovery.sh

bashio::log.green "Setting up auto discovery for Home Assistant"
bashio::log.info "Setting up auto discovery for Home Assistant"
setup_auto_discovery

bashio::log.green "Connecting to MQTT to discard any unread messages"
bashio::log.info "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/+

bashio::log.green "Initialize BLE listening loop counter"
bashio::log.info "Initialize BLE listening loop counter"
counter=0
bashio::log.green "Entering main MQTT & BLE listening loop"
bashio::log.info "Entering main MQTT & BLE listening loop"
while true
do
set +e
listen_to_mqtt
((counter++))
if [[ $counter -gt 90 ]]; then
bashio::log.green "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence"
listen_to_ble
counter=0
if [ -z "$BLE_MAC" ]; then
((counter++))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may pass an explicit parameter to enable BLE presence scan later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand your comment properly, that is added in #25 using configuration setting BLE_PRESENCE_ENABLE (true|false).

if [[ $counter -gt 90 ]]; then
bashio::log.info "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence"
listen_to_ble
counter=0
fi
fi
sleep 2
done
13 changes: 6 additions & 7 deletions tesla_ble_mqtt/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ configuration:
mqtt_pwd:
name: MQTT Password
description: MQTT User's password
send_cmd_retry_delay:
name: Delay to retry a command
description: Delay to retry sending a command to the vehicle over BLE
debug:
name: Debug Logging
description: Print verbose messages to the log for debugging
vehicle:
vin:
name: Car's VIN
description: Vehicle Identification Number found in Tesla app
ble_mac:
name: BLE MAC (optional for proximity detection)
description: Car's BLE MAC (S___________C); Use Android "BLE scanner" or iOS "nRF Connect"
debug:
name: Debug Logging
description: Print verbose messages to the log for debugging
send_cmd_retry_delay:
name: Delay to retry a command
description: Delay to retry sending a command to the vehicle over BLE
13 changes: 6 additions & 7 deletions tesla_ble_mqtt/translations/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ configuration:
mqtt_pwd:
name: Mot de passe MQTT
description: Mot de passe de l'utilisateur MQTT
send_cmd_retry_delay:
name: Délai pour re-essayer une commande
description: Délai pour re-essayer d'envoyer une command au véhicule via BLE
debug:
name: Journalisation du débogage
description: Imprimer des messages détaillés dans le journal pour le débogage
véhicule:
vin:
name: NIV du véhicule
description: Numéro d'Identification de Véhicule affiché dans l'application Tesla
ble_mac:
name: BLE MAC (optionnel pour détection de proximité)
description: BLE MAC (S___________C) du véhicule; Utiliser Android "BLE scanner" ou iOS "nRF Connect"
debug:
name: Journalisation du débogage
description: Imprimer des messages détaillés dans le journal pour le débogage
send_cmd_retry_delay:
name: Délai pour re-essayer une commande
description: Délai pour re-essayer d'envoyer une command au véhicule via BLE