55# read options in case of HA addon. Otherwise, they will be sent as environment variables
66if [ -n " ${HASSIO_TOKEN:- } " ]; then
77 TESLA_VIN=" $( bashio::config ' vin' ) " ; export TESLA_VIN
8- BLE_MAC=" $( bashio::config ' ble_mac' ) " ; export BLE_MAC
98 MQTT_IP=" $( bashio::config ' mqtt_ip' ) " ; export MQTT_IP
109 MQTT_PORT=" $( bashio::config ' mqtt_port' ) " ; export MQTT_PORT
1110 MQTT_USER=" $( bashio::config ' mqtt_user' ) " ; export MQTT_USER
1211 MQTT_PWD=" $( bashio::config ' mqtt_pwd' ) " ; export MQTT_PWD
1312 SEND_CMD_RETRY_DELAY=" $( bashio::config ' send_cmd_retry_delay' ) " ; export SEND_CMD_RETRY_DELAY
13+ BLE_PRESENCE_ENABLE=" $( bashio::config ' ble_presence_enable' ) " ; export BLE_PRESENCE_ENABLE
1414 DEBUG=" $( bashio::config ' debug' ) " ; export DEBUG
15+ else
16+ NOCOLOR=' \033[0m'
17+ GREEN=' \033[0;32m'
18+ CYAN=' \033[0;36m'
19+ YELLOW=' \033[1;32m'
20+ MAGENTA=' \033[0;35m'
21+ RED=' \033[0;31m'
22+
23+ function bashio::log.debug { [ $DEBUG == " true" ] && echo -e " ${NOCOLOR} $1 " ; }
24+ function bashio::log.info { echo -e " ${GREEN} $1 ${NOCOLOR} " ; }
25+ function bashio::log.notice { echo -e " ${CYAN} $1 ${NOCOLOR} " ; }
26+ function bashio::log.warning { echo -e " ${YELLOW} $1 ${NOCOLOR} " ; }
27+ function bashio::log.error { echo -e " ${MAGENTA} $1 ${NOCOLOR} " ; }
28+ function bashio::log.fatal { echo -e " ${RED} $1 ${NOCOLOR} " ; }
29+
30+ function bashio::log.cyan { echo -e " ${CYAN} $1 ${NOCOLOR} " ; }
31+ function bashio::log.green { echo -e " ${GREEN} $1 ${NOCOLOR} " ; }
32+ function bashio::log.magenta { echo -e " ${GREEN} $1 ${NOCOLOR} " ; }
33+ function bashio::log.red { echo -e " ${RED} $1 ${NOCOLOR} " ; }
34+ function bashio::log.yellow { echo -e " ${YELLOW} $1 ${NOCOLOR} " ; }
1535fi
1636
1737# Set log level to debug
@@ -21,14 +41,33 @@ bashio::log.cyan "tesla_ble_mqtt_docker by Iain Bullock 2024 https://github.com/
2141bashio::log.cyan " Inspiration by Raphael Murray https://github.com/raphmur"
2242bashio::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"
2343
44+ # Generate BLE_MAC from TESLA_VIN
45+ function ble_mac_generate() {
46+
47+ python3 - << __END_PY__
48+ from cryptography.hazmat.primitives import hashes;
49+ vin = bytes("$TESLA_VIN ", "UTF8");
50+ digest = hashes.Hash(hashes.SHA1())
51+ digest.update(vin)
52+ vinSHA = digest.finalize().hex()
53+ middleSection = vinSHA[0:16]
54+ bleName = "S" + middleSection + "C"
55+ print(bleName)
56+ __END_PY__
57+ }
58+
59+ export BLE_MAC=$( ble_mac_generate)
60+
2461bashio::log.green " Configuration Options are:
2562 TESLA_VIN=$TESLA_VIN
2663 BLE_MAC=$BLE_MAC
2764 MQTT_IP=$MQTT_IP
2865 MQTT_PORT=$MQTT_PORT
2966 MQTT_USER=$MQTT_USER
3067 MQTT_PWD=Not Shown
31- SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY "
68+ SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY
69+ BLE_PRESENCE_ENABLE=$BLE_PRESENCE_ENABLE
70+ DEBUG=$DEBUG "
3271
3372if [ ! -d /share/tesla_ble_mqtt ]
3473then
@@ -100,18 +139,35 @@ setup_auto_discovery
100139bashio::log.info " Connecting to MQTT to discard any unread messages"
101140mosquitto_sub -E -i tesla_ble_mqtt -h $MQTT_IP -p $MQTT_PORT -u $MQTT_USER -P $MQTT_PWD -t tesla_ble/+
102141
103- bashio::log.green " Initialize BLE listening loop counter"
104- counter=0
105- bashio::log.green " Entering main MQTT & BLE listening loop"
142+
143+ # Run BLE presence if BLE_MAC is defined and BLE_PRESENCE_ENABLE=true
144+ if [ -z " $BLE_MAC " ]; then
145+ bashio::log.notice " BLE_MAC being undefined, presence detection for the car will not run"
146+ counter=-1
147+ else
148+ if [ $BLE_PRESENCE_ENABLE == " true" ]; then
149+ bashio::log.info " BLE_MAC is defined, initializing BLE listening loop counter"
150+ counter=0
151+ else
152+ bashio::log.notice " Will not run proximity presence detection BLE_PRESENCE_ENABLE=false"
153+ counter=-1
154+ fi
155+ fi
156+ [ $counter -eq 0 ] && \
157+ bashio::log.info " Entering main MQTT & BLE listening loop" || \
158+ bashio::log.info " Entering main MQTT loop, not running BLE listening"
159+
106160while true
107161do
108162 set +e
109163 listen_to_mqtt
110- (( counter++ ))
111- if [[ $counter -gt 90 ]]; then
112- bashio::log.green " Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence"
113- listen_to_ble
114- counter=0
164+ if [ $counter -ge 0 ]; then
165+ (( counter++ ))
166+ if [[ $counter -gt 90 ]]; then
167+ bashio::log.green " Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence"
168+ listen_to_ble
169+ counter=0
170+ fi
115171 fi
116172 sleep 2
117173done
0 commit comments