Skip to content

Commit 8953f8d

Browse files
committed
Auto calculation of MAC from VIN
Credit to https://github.com/BogdanDIA
1 parent 727ff37 commit 8953f8d

File tree

4 files changed

+104
-16
lines changed

4 files changed

+104
-16
lines 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+
py-cryptography
2324

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

tesla_ble_mqtt/config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: "Tesla Local Commands"
2-
version: "0.0.8"
1+
name: "Tesla Local Commands local"
2+
version: "0.0.8b"
33
slug: "tesla_local_commands"
44
description: "Local BLE calls to control your Tesla."
55
# url: "tbc"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
# Generate BLE_MAC from TESLA_VIN
4+
bashio::log.notice "TESLA_VIN: $TESLA_VIN. Calculating BLE_LOCAL_NAME..."
5+
6+
function ble_mac_generate() {
7+
python3 - << __END_PY__
8+
from cryptography.hazmat.primitives import hashes;
9+
vin = bytes("$TESLA_VIN", "UTF8");
10+
digest = hashes.Hash(hashes.SHA1())
11+
digest.update(vin)
12+
vinSHA = digest.finalize().hex()
13+
middleSection = vinSHA[0:16]
14+
bleName = "S" + middleSection + "C"
15+
print(bleName)
16+
__END_PY__
17+
}
18+
19+
BLE_LOCAL_NAME=$(ble_mac_generate)
20+
21+
22+
23+
# print BLE Local Name for which we want the MAC
24+
bashio::log.notice "BLE_LOCAL_NAME: $BLE_LOCAL_NAME. Start MAC finding..."
25+
26+
# scan and store the list of found MACs, list is provided after timeout
27+
bluetoothctl --timeout 30 scan on | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' > scan-macs.txt
28+
sort scan-macs.txt | uniq > uniq_macs.txt
29+
30+
# print the list of MACs we found
31+
bashio::log.info "Found MACS:"
32+
cat uniq_macs.txt
33+
34+
# start scan again in another process so we can get the detailed info for each MAC
35+
bluetoothctl --timeout 20 scan on > /dev/zero &
36+
37+
#xargs -0 -n 1 bluetoothctl info < <(tr \\n \\0 <scan-macs.txt) | grep "Name: $BLE_LOCAL_NAME"
38+
39+
# cycle through all scanned MACs to find our MAC
40+
BLE_MAC=""
41+
while read mac; do
42+
INFO=$(bluetoothctl --timeout 1 info $mac)
43+
INFONAME=$(echo "$INFO" | grep "Name: $BLE_LOCAL_NAME")
44+
45+
if [[ -n $INFONAME ]]; then
46+
bashio::log.info "Found Tesla $TESLA_VIN's MAC:"
47+
echo $mac > mac.txt
48+
INFORSSI=$(echo "$INFO" | grep "RSSI: ")
49+
bashio::log.info "$mac"
50+
bashio::log.info "$INFONAME"
51+
bashio::log.info "$INFORSSI"
52+
BLE_MAC="$mac"
53+
# kill bluetoothctl
54+
# killall `ps -aux | grep bluetoothctl | grep -v grep | awk '{ print $1 }'`
55+
break
56+
fi
57+
done < uniq_macs.txt
58+
59+
60+
if [ -z ${BLE_MAC-} ]; then
61+
bashio::log.info "No corresponding MAC address found: is the car close to the BLE device?"
62+
bashio::log.info "Will retry later"
63+
else
64+
bashio::log.info "End MAC finding: success $BLE_MAC"
65+
export BLE_MAC
66+
fi

tesla_ble_mqtt/rootfs/app/run.sh

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/command/with-contenv bashio
22
#set -e
33

4-
4+
### INITIALIZE VARIABLES AND FUNCTIONS TO MAKE THIS .sh RUN ALSO STANDALONE ##########################################
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
8+
# BLE_MAC="$(bashio::config 'ble_mac')"; export BLE_MAC
99
MQTT_IP="$(bashio::config 'mqtt_ip')"; export MQTT_IP
1010
MQTT_PORT="$(bashio::config 'mqtt_port')"; export MQTT_PORT
1111
MQTT_USER="$(bashio::config 'mqtt_user')"; export MQTT_USER
@@ -33,6 +33,7 @@ else
3333
function bashio::log.yellow { echo -e "${YELLOW}$1${NOCOLOR}"; }
3434
fi
3535

36+
### INITIALIZE AND LOG CONFIG VARS ##################################################################################
3637
# Set log level to debug
3738
bashio::config.true debug && bashio::log.level debug
3839

@@ -42,7 +43,6 @@ bashio::log.cyan "Instructions by Shankar Kumarasamy https://shankarkumarasamy.b
4243

4344
bashio::log.green "Configuration Options are:
4445
TESLA_VIN=$TESLA_VIN
45-
BLE_MAC=$BLE_MAC
4646
MQTT_IP=$MQTT_IP
4747
MQTT_PORT=$MQTT_PORT
4848
MQTT_USER=$MQTT_USER
@@ -58,7 +58,7 @@ else
5858
bashio::log.debug "/share/tesla_ble_mqtt already exists, existing keys can be reused"
5959
fi
6060

61-
61+
### DEFINE FUNCTIONS ###############################################################################################
6262
send_command() {
6363
for i in $(seq 5); do
6464
bashio::log.notice "Attempt $i/5 to send command"
@@ -94,7 +94,7 @@ send_key() {
9494
}
9595

9696
listen_to_ble() {
97-
bashio::log.info "Listening to BLE for presence"
97+
bashio::log.notice "Listening to BLE for presence"
9898
PRESENCE_TIMEOUT=5
9999
set +e
100100
bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_MAC
@@ -104,34 +104,55 @@ listen_to_ble() {
104104
bashio::log.info "$BLE_MAC presence detected"
105105
mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON
106106
else
107-
bashio::log.notice "$BLE_MAC presence not detected or issue in command"
107+
bashio::log.info "$BLE_MAC presence not detected or issue in command"
108108
mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF
109109
fi
110-
111110
}
112111

112+
### SETUP ENVIRONMENT ###########################################################################################
113113
bashio::log.notice "Sourcing functions"
114114
. /app/listen_to_mqtt.sh
115115
. /app/discovery.sh
116116

117-
bashio::log.info "Setting up auto discovery for Home Assistant"
117+
bashio::log.notice "Setting up auto discovery for Home Assistant"
118118
setup_auto_discovery
119119

120-
bashio::log.info "Connecting to MQTT to discard any unread messages"
120+
bashio::log.notice "Connecting to MQTT to discard any unread messages"
121121
mosquitto_sub -E -i tesla_ble_mqtt -h $MQTT_IP -p $MQTT_PORT -u $MQTT_USER -P $MQTT_PWD -t tesla_ble/+
122122

123-
bashio::log.info "Initialize BLE listening loop counter"
123+
bashio::log.info "Start finding the BLE MAC address from VIN (will work only if Tesla is in BLE range)"
124+
set +e
125+
. /app/calc_ble_from_vin.sh
126+
set -e
127+
128+
echo $BLE_MAC
129+
130+
### START MAIN PROGRAM LOOP ######################################################################################
131+
# bashio::log.info "Initialize BLE listening loop counter"
132+
bashio::log.notice "Entering main MQTT & BLE listening loop"
124133
counter=0
125-
bashio::log.info "Entering main MQTT & BLE listening loop"
126134
while true
127135
do
128136
set +e
129137
listen_to_mqtt
138+
# set -e
130139
((counter++))
131140
if [[ $counter -gt 90 ]]; then
132141
bashio::log.info "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence"
133-
listen_to_ble
134-
counter=0
142+
if [ -z ${BLE_MAC-} ]; then
143+
bashio::log.notice "Retry finding the BLE MAC address"
144+
# set +e
145+
. /app/calc_ble_from_vin.sh
146+
# set -e
147+
fi
148+
if [ -z ${BLE_MAC-} ]; then continue
149+
else
150+
# set +e
151+
listen_to_ble
152+
# set -e
153+
counter=0
154+
fi
135155
fi
136156
sleep 2
157+
set -e
137158
done

0 commit comments

Comments
 (0)