Skip to content

Commit

Permalink
[ble] Run presence if enable, BLE_MAC calculate & rm opt
Browse files Browse the repository at this point in the history
  • Loading branch information
baylanger committed Jun 18, 2024
2 parents 9eb2c28 + fd42d6d commit 022c094
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 19 deletions.
15 changes: 15 additions & 0 deletions tesla_ble_mqtt/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<!-- https://developers.home-assistant.io/docs/add-ons/presentation#keeping-a-changelog -->

## 0.0.8a

### Changed

- [ble presence] Add functionality to enable or not presence detection via config UI
- [ble presence] Add function to calculate BLE_MAC
- [ble presence] Remove BLE_MAC config option

## 0.0.8

### Changed

- [Standalone] Fix broken deployment introduced in 0.0.7
- [Standalone] Add colored logging based on log level

## 0.0.7a

### Changed
Expand Down
6 changes: 3 additions & 3 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.7a"
version: "0.0.8a"
slug: "tesla_local_commands"
description: "Local BLE calls to control your Tesla."
# url: "tbc"
Expand All @@ -19,22 +19,22 @@ map:
startup: services
options:
vin: ""
ble_mac: ""
debug: false
mqtt_ip: ""
mqtt_port: "1883"
mqtt_user: ""
mqtt_pwd: ""
send_cmd_retry_delay: "5"
ble_presence_enable: true
schema:
vin: str?
debug: bool
ble_mac: str?
mqtt_ip: str?
mqtt_port: str?
mqtt_user: str?
mqtt_pwd: password?
send_cmd_retry_delay: str?
ble_presence_enable: bool
# ingress: true
# panel_icon: mdi:forward
# backup_exclude:
Expand Down
76 changes: 66 additions & 10 deletions tesla_ble_mqtt/rootfs/app/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,33 @@
# 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
BLE_MAC="$(bashio::config 'ble_mac')"; export BLE_MAC
MQTT_IP="$(bashio::config 'mqtt_ip')"; export MQTT_IP
MQTT_PORT="$(bashio::config 'mqtt_port')"; export MQTT_PORT
MQTT_USER="$(bashio::config 'mqtt_user')"; export MQTT_USER
MQTT_PWD="$(bashio::config 'mqtt_pwd')"; export MQTT_PWD
SEND_CMD_RETRY_DELAY="$(bashio::config 'send_cmd_retry_delay')"; export SEND_CMD_RETRY_DELAY
BLE_PRESENCE_ENABLE="$(bashio::config 'ble_presence_enable')"; export BLE_PRESENCE_ENABLE
DEBUG="$(bashio::config 'debug')"; export DEBUG
else
NOCOLOR='\033[0m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;32m'
MAGENTA='\033[0;35m'
RED='\033[0;31m'

function bashio::log.debug { [ $DEBUG == "true" ] && echo -e "${NOCOLOR}$1"; }
function bashio::log.info { echo -e "${GREEN}$1${NOCOLOR}"; }
function bashio::log.notice { echo -e "${CYAN}$1${NOCOLOR}"; }
function bashio::log.warning { echo -e "${YELLOW}$1${NOCOLOR}"; }
function bashio::log.error { echo -e "${MAGENTA}$1${NOCOLOR}"; }
function bashio::log.fatal { echo -e "${RED}$1${NOCOLOR}"; }

function bashio::log.cyan { echo -e "${CYAN}$1${NOCOLOR}"; }
function bashio::log.green { echo -e "${GREEN}$1${NOCOLOR}"; }
function bashio::log.magenta { echo -e "${GREEN}$1${NOCOLOR}"; }
function bashio::log.red { echo -e "${RED}$1${NOCOLOR}"; }
function bashio::log.yellow { echo -e "${YELLOW}$1${NOCOLOR}"; }
fi

# Set log level to debug
Expand All @@ -21,14 +41,33 @@ bashio::log.cyan "tesla_ble_mqtt_docker by Iain Bullock 2024 https://github.com/
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"

# Generate BLE_MAC from TESLA_VIN
function ble_mac_generate() {

python3 - << __END_PY__
from cryptography.hazmat.primitives import hashes;
vin = bytes("$TESLA_VIN", "UTF8");
digest = hashes.Hash(hashes.SHA1())
digest.update(vin)
vinSHA = digest.finalize().hex()
middleSection = vinSHA[0:16]
bleName = "S" + middleSection + "C"
print(bleName)
__END_PY__
}

export BLE_MAC=$(ble_mac_generate)

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"
SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY
BLE_PRESENCE_ENABLE=$BLE_PRESENCE_ENABLE
DEBUG=$DEBUG"

if [ ! -d /share/tesla_ble_mqtt ]
then
Expand Down Expand Up @@ -100,18 +139,35 @@ setup_auto_discovery
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"
counter=0
bashio::log.green "Entering main MQTT & BLE listening loop"

# Run BLE presence if BLE_MAC is defined and BLE_PRESENCE_ENABLE=true
if [ -z "$BLE_MAC" ]; then
bashio::log.notice "BLE_MAC being undefined, presence detection for the car will not run"
counter=-1
else
if [ $BLE_PRESENCE_ENABLE == "true" ]; then
bashio::log.info "BLE_MAC is defined, initializing BLE listening loop counter"
counter=0
else
bashio::log.notice "Will not run proximity presence detection BLE_PRESENCE_ENABLE=false"
counter=-1
fi
fi
[ $counter -eq 0 ] && \
bashio::log.info "Entering main MQTT & BLE listening loop" || \
bashio::log.info "Entering main MQTT loop, not running BLE listening"

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 [ $counter -ge 0 ]; then
((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
fi
fi
sleep 2
done
7 changes: 4 additions & 3 deletions tesla_ble_mqtt/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ configuration:
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
ble_presence_enable:
name: BLE proximity presence detection
description: Controls if BLE proximity presence is enable or not
description: Detection over BLE of proximity presence
6 changes: 3 additions & 3 deletions tesla_ble_mqtt/translations/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ configuration:
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
ble_presence_enable:
name: Détection de présence BLE par proximitée
description: Contrôle de la détection de présence par proximité basé sur BLE

0 comments on commit 022c094

Please sign in to comment.