Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
13 changes: 8 additions & 5 deletions standalone/start_tesla_ble_mqtt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ MQTT_PORT=1883
MQTT_USER=""
MQTT_PWD=""
SEND_CMD_RETRY_DELAY=5
BLE_MAC=00:00:00:00:00:00
DEBUG="false"
PRESENCE_DETECTION="false"
BLE_MAC=""
############################################################


Expand Down Expand Up @@ -42,8 +43,9 @@ echo "Start main docker container with configuration Options:
MQTT_USER=$MQTT_USER
MQTT_PWD=Not Shown
SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY
BLE_MAC=$BLE_MAC
DEBUG=$DEBUG"
DEBUG=$DEBUG
PRESENCE_DETECTION=$PRESENCE_DETECTION
BLE_MAC=$BLE_MAC"

docker-compose up -d \
-e TESLA_VIN=$TESLA_VIN \
Expand All @@ -52,5 +54,6 @@ docker-compose up -d \
-e MQTT_USER=$MQTT_USER \
-e MQTT_PWD=$MQTT_PWD \
-e SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY \
-e BLE_MAC=$BLE_MAC \
-e DEBUG=$DEBUG
-e DEBUG=$DEBUG \
-e PRESENCE_DETECTION=$PRESENCE_DETECTION \
-e BLE_MAC=$BLE_MAC
9 changes: 8 additions & 1 deletion tesla_ble_mqtt/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<!-- https://developers.home-assistant.io/docs/add-ons/presentation#keeping-a-changelog -->

## 0.0.9

### Changed

- Add option to deactivate BLE proximity scanner (by default, periodicity is around 4 min)
- Implement MAC address detection from VIN (MAC can still be input if prefered)

## 0.0.8a

### Changed

- Updated doc for MAC addresses config
- Updated doc for MAC addresses

## 0.0.8

Expand Down
3 changes: 2 additions & 1 deletion tesla_ble_mqtt/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ RUN apk add --no-cache \
openssl \
bluez \
mosquitto-clients \
python3
python3 \
py-cryptography

# Python 3 HTTP Server serves the current working dir
WORKDIR /data
Expand Down
4 changes: 3 additions & 1 deletion 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.8a"
version: "0.0.9"
slug: "tesla_local_commands"
description: "Local BLE calls to control your Tesla."
# url: "tbc"
Expand All @@ -19,6 +19,7 @@ map:
startup: services
options:
vin: ""
presence_detection: false
ble_mac: ""
debug: false
mqtt_ip: ""
Expand All @@ -29,6 +30,7 @@ options:
schema:
vin: str?
debug: bool
presence_detection: bool
ble_mac: str?
mqtt_ip: str?
mqtt_port: str?
Expand Down
56 changes: 56 additions & 0 deletions tesla_ble_mqtt/rootfs/app/calc_ble_from_vin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

# Generate BLE_MAC from TESLA_VIN
bashio::log.notice "TESLA_VIN: $TESLA_VIN. Calculating BLE_LOCAL_NAME..."

# reset file and calculate LNAME from VIN
rm -f tesla_vin.txt
echo -n $TESLA_VIN > tesla_vin.txt

VIN_SHA1=`sha1sum tesla_vin.txt`
BLE_LOCAL_NAME="S${VIN_SHA1:0:16}C"

# print BLE Local Name for which we want the MAC
bashio::log.notice "BLE_LOCAL_NAME: $BLE_LOCAL_NAME. Start MAC finding..."

# scan and store the list of found MACs, list is provided after timeout
bluetoothctl --timeout 30 scan on | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' > scan-macs.txt
sort scan-macs.txt | uniq > uniq_macs.txt

# print the list of MACs we found
bashio::log.info "Found MACS:"
cat uniq_macs.txt

# start scan again in another process so we can get the detailed info for each MAC
bluetoothctl --timeout 20 scan on > /dev/zero &

#xargs -0 -n 1 bluetoothctl info < <(tr \\n \\0 <scan-macs.txt) | grep "Name: $BLE_LOCAL_NAME"

bashio::log.notice "Cycling through all scanned MACs to find the vehicle"
BLE_MAC=""
while read mac; do
INFO=$(bluetoothctl --timeout 1 info $mac)
INFONAME=$(echo "$INFO" | grep "Name: $BLE_LOCAL_NAME")

if [[ -n $INFONAME ]]; then
bashio::log.info "Found Tesla $TESLA_VIN's MAC:"
echo $mac > mac.txt
INFORSSI=$(echo "$INFO" | grep "RSSI: ")
bashio::log.info "$mac"
bashio::log.info "$INFONAME"
bashio::log.info "$INFORSSI"
BLE_MAC="$mac"
# kill bluetoothctl
# killall `ps -aux | grep bluetoothctl | grep -v grep | awk '{ print $1 }'`
break
fi
done < uniq_macs.txt


if [ -z ${BLE_MAC-} ]; then
bashio::log.info "No corresponding MAC address found: is the car close to the BLE device?"
bashio::log.info "Will retry later"
else
bashio::log.info "End MAC finding: success $BLE_MAC"
export BLE_MAC
fi
58 changes: 41 additions & 17 deletions tesla_ble_mqtt/rootfs/app/run.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/command/with-contenv bashio
#set -e


### INITIALIZE VARIABLES AND FUNCTIONS TO MAKE THIS .sh RUN ALSO STANDALONE ##########################################
# read options in case of HA addon. Otherwise, they will be sent as environment variables
if [ -n "${HASSIO_TOKEN:-}" ]; then
TESLA_VIN="$(bashio::config 'vin')"; export TESLA_VIN
Expand All @@ -12,6 +12,7 @@ if [ -n "${HASSIO_TOKEN:-}" ]; then
MQTT_PWD="$(bashio::config 'mqtt_pwd')"; export MQTT_PWD
SEND_CMD_RETRY_DELAY="$(bashio::config 'send_cmd_retry_delay')"; export SEND_CMD_RETRY_DELAY
DEBUG="$(bashio::config 'debug')"; export DEBUG
PRESENCE_DETECTION="$(bashio::config 'presence_detection')"; export PRESENCE_DETECTION
else
NOCOLOR='\033[0m'
GREEN='\033[0;32m'
Expand All @@ -33,6 +34,7 @@ else
function bashio::log.yellow { echo -e "${YELLOW}$1${NOCOLOR}"; }
fi

### INITIALIZE AND LOG CONFIG VARS ##################################################################################
# Set log level to debug
bashio::config.true debug && bashio::log.level debug

Expand All @@ -42,13 +44,14 @@ bashio::log.cyan "Instructions by Shankar Kumarasamy https://shankarkumarasamy.b

bashio::log.green "Configuration Options are:
TESLA_VIN=$TESLA_VIN
BLE_MAC=$BLE_MAC
MQTT_IP=$MQTT_IP
MQTT_PORT=$MQTT_PORT
MQTT_USER=$MQTT_USER
MQTT_PWD=Not Shown
SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY
DEBUG=$DEBUG"
DEBUG=$DEBUG
PRESENCE_DETECTION=$PRESENCE_DETECTION
BLE_MAC=$BLE_MAC"

if [ ! -d /share/tesla_ble_mqtt ]
then
Expand All @@ -58,7 +61,7 @@ else
bashio::log.debug "/share/tesla_ble_mqtt already exists, existing keys can be reused"
fi


### DEFINE FUNCTIONS ###############################################################################################
send_command() {
for i in $(seq 5); do
bashio::log.notice "Attempt $i/5 to send command"
Expand Down Expand Up @@ -94,7 +97,7 @@ send_key() {
}

listen_to_ble() {
bashio::log.info "Listening to BLE for presence"
bashio::log.notice "Listening to BLE for presence"
PRESENCE_TIMEOUT=5
set +e
bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_MAC
Expand All @@ -104,34 +107,55 @@ listen_to_ble() {
bashio::log.info "$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
else
bashio::log.notice "$BLE_MAC presence not detected or issue in command"
bashio::log.info "$BLE_MAC presence not detected or issue in command"
mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF
fi

}

bashio::log.notice "Sourcing functions"
### SETUP ENVIRONMENT ###########################################################################################
bashio::log.notice "Load functions"
. /app/listen_to_mqtt.sh
. /app/discovery.sh

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

bashio::log.info "Connecting to MQTT to discard any unread messages"
bashio::log.notice "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.info "Initialize BLE listening loop counter"
if [ "$PRESENCE_DETECTION" = true ] ; then
if [ -z ${BLE_MAC-} ]; then
bashio::log.info "Start finding the BLE MAC address from VIN (will work only if Tesla is in BLE range)"
set +e
. /app/calc_ble_from_vin.sh
set -e
fi
fi

echo $BLE_MAC

### START MAIN PROGRAM LOOP ######################################################################################
bashio::log.notice "Entering main MQTT & BLE listening loop"
counter=0
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.info "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence"
listen_to_ble
counter=0
if [ "$PRESENCE_DETECTION" = true ] ; then
((counter++))
if [[ $counter -gt 90 ]]; then
bashio::log.info "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence"
if [ -z ${BLE_MAC-} ]; then
bashio::log.notice "Retry finding the BLE MAC address"
. /app/calc_ble_from_vin.sh
fi
if [ -z ${BLE_MAC-} ]; then continue
else
listen_to_ble
counter=0
fi
fi
fi
sleep 2
set -e
done
3 changes: 3 additions & 0 deletions tesla_ble_mqtt/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ configuration:
debug:
name: Debug Logging
description: Print verbose messages to the log for debugging
presence_detection:
name: Proximity detector
description: Scan for vehicle proximity to the bluetooth device
send_cmd_retry_delay:
name: Delay to retry a command
description: Delay to retry sending a command to the vehicle over BLE
3 changes: 3 additions & 0 deletions tesla_ble_mqtt/translations/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ configuration:
debug:
name: Journalisation du débogage
description: Imprimer des messages détaillés dans le journal pour le débogage
presence_detection:
name: Détecteur de présence
description: Détecter la présence du véhicule, à portée de l'appareil bluetooth
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