Skip to content

Commit 7fac156

Browse files
committed
[ble] Run presence if enable, BLE_MAC calculate & rm opt
2 parents 9eb2c28 + fd42d6d commit 7fac156

File tree

6 files changed

+93
-20
lines changed

6 files changed

+93
-20
lines changed

tesla_ble_mqtt/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
<!-- https://developers.home-assistant.io/docs/add-ons/presentation#keeping-a-changelog -->
22

3+
## 0.0.8a
4+
5+
### Changed
6+
7+
- [ble presence] Add functionality to enable or not presence detection via config UI
8+
- [ble presence] Add function to calculate BLE_MAC
9+
- [ble presence] Remove BLE_MAC config option
10+
11+
## 0.0.8
12+
13+
### Changed
14+
15+
- [Standalone] Fix broken deployment introduced in 0.0.7
16+
- [Standalone] Add colored logging based on log level
17+
318
## 0.0.7a
419

520
### Changed

tesla_ble_mqtt/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ RUN apk add --no-cache \
1919
openssl \
2020
bluez \
2121
mosquitto-clients \
22-
python3
22+
python3 \
23+
py3-cryptography
2324

2425
# Python 3 HTTP Server serves the current working dir
2526
WORKDIR /data

tesla_ble_mqtt/config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "Tesla Local Commands"
2-
version: "0.0.7a"
2+
version: "0.0.8a"
33
slug: "tesla_local_commands"
44
description: "Local BLE calls to control your Tesla."
55
# url: "tbc"
@@ -19,22 +19,22 @@ map:
1919
startup: services
2020
options:
2121
vin: ""
22-
ble_mac: ""
2322
debug: false
2423
mqtt_ip: ""
2524
mqtt_port: "1883"
2625
mqtt_user: ""
2726
mqtt_pwd: ""
2827
send_cmd_retry_delay: "5"
28+
ble_presence_enable: true
2929
schema:
3030
vin: str?
3131
debug: bool
32-
ble_mac: str?
3332
mqtt_ip: str?
3433
mqtt_port: str?
3534
mqtt_user: str?
3635
mqtt_pwd: password?
3736
send_cmd_retry_delay: str?
37+
ble_presence_enable: bool
3838
# ingress: true
3939
# panel_icon: mdi:forward
4040
# backup_exclude:

tesla_ble_mqtt/rootfs/app/run.sh

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,33 @@
55
# read options in case of HA addon. Otherwise, they will be sent as environment variables
66
if [ -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}"; }
1535
fi
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/
2141
bashio::log.cyan "Inspiration by Raphael Murray https://github.com/raphmur"
2242
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"
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+
2461
bashio::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

3372
if [ ! -d /share/tesla_ble_mqtt ]
3473
then
@@ -100,18 +139,35 @@ setup_auto_discovery
100139
bashio::log.info "Connecting to MQTT to discard any unread messages"
101140
mosquitto_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+
106160
while true
107161
do
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
117173
done

tesla_ble_mqtt/translations/en.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ configuration:
1414
vin:
1515
name: Car's VIN
1616
description: Vehicle Identification Number found in Tesla app
17-
ble_mac:
18-
name: BLE MAC (optional for proximity detection)
19-
description: Car's BLE MAC (S___________C); Use Android "BLE scanner" or iOS "nRF Connect"
2017
debug:
2118
name: Debug Logging
2219
description: Print verbose messages to the log for debugging
2320
send_cmd_retry_delay:
2421
name: Delay to retry a command
2522
description: Delay to retry sending a command to the vehicle over BLE
23+
ble_presence_enable:
24+
name: BLE proximity presence detection
25+
description: Controls if BLE proximity presence is enable or not
26+
description: Detection over BLE of proximity presence

tesla_ble_mqtt/translations/fr.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ configuration:
1414
vin:
1515
name: NIV du véhicule
1616
description: Numéro d'Identification de Véhicule affiché dans l'application Tesla
17-
ble_mac:
18-
name: BLE MAC (optionnel pour détection de proximité)
19-
description: BLE MAC (S___________C) du véhicule; Utiliser Android "BLE scanner" ou iOS "nRF Connect"
2017
debug:
2118
name: Journalisation du débogage
2219
description: Imprimer des messages détaillés dans le journal pour le débogage
2320
send_cmd_retry_delay:
2421
name: Délai pour re-essayer une commande
2522
description: Délai pour re-essayer d'envoyer une command au véhicule via BLE
23+
ble_presence_enable:
24+
name: Détection de présence BLE par proximitée
25+
description: Contrôle de la détection de présence par proximité basé sur BLE

0 commit comments

Comments
 (0)