Skip to content

Commit 369f505

Browse files
committed
BLE presence - Rename MAC, redo logic
1 parent 65d57ae commit 369f505

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

tesla_ble_mqtt/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
### Changed
66

77
- [ble presence] Add functionality to enable or not presence detection via config UI
8-
- [ble presence] Add function to calculate BLE_MAC
8+
- [ble presence] Rename BLE_MAC to BLE_SC_NAME
9+
- [ble presence] Add logic to calculate BLE_SC_NAME
910
- [ble presence] Remove BLE_MAC config option
1011

1112
## 0.0.8

tesla_ble_mqtt/rootfs/app/run.sh

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,8 @@ bashio::log.cyan "tesla_ble_mqtt_docker by Iain Bullock 2024 https://github.com/
4141
bashio::log.cyan "Inspiration by Raphael Murray https://github.com/raphmur"
4242
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"
4343

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-
6144
bashio::log.green "Configuration Options are:
6245
TESLA_VIN=$TESLA_VIN
63-
BLE_MAC=$BLE_MAC
6446
MQTT_IP=$MQTT_IP
6547
MQTT_PORT=$MQTT_PORT
6648
MQTT_USER=$MQTT_USER
@@ -116,19 +98,20 @@ listen_to_ble() {
11698
bashio::log.info "Listening to BLE for presence"
11799
PRESENCE_TIMEOUT=5
118100
set +e
119-
bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_MAC
101+
bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_SC_NAME
120102
EXIT_STATUS=$?
121103
set -e
122104
if [ $EXIT_STATUS -eq 0 ]; then
123-
bashio::log.info "$BLE_MAC presence detected"
105+
bashio::log.info "$BLE_SC_NAME presence detected"
124106
mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON
125107
else
126-
bashio::log.notice "$BLE_MAC presence not detected or issue in command"
108+
bashio::log.notice "$BLE_SC_NAME presence not detected or issue in command"
127109
mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF
128110
fi
129111

130112
}
131113

114+
132115
bashio::log.notice "Sourcing functions"
133116
. /app/listen_to_mqtt.sh
134117
. /app/discovery.sh
@@ -140,33 +123,37 @@ bashio::log.info "Connecting to MQTT to discard any unread messages"
140123
mosquitto_sub -E -i tesla_ble_mqtt -h $MQTT_IP -p $MQTT_PORT -u $MQTT_USER -P $MQTT_PWD -t tesla_ble/+
141124

142125

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
126+
# Run BLE presence if BLE_PRESENCE_ENABLE is true
127+
if [ $BLE_PRESENCE_ENABLE == "true" ]; then
128+
129+
# Generate BLE_SC_NAME from TESLA_VIN
130+
TESLA_VIN_HASH=$(echo -n "$TESLA_VIN" | sha1sum)
131+
BLE_SC_NAME=S${TESLA_VIN_HASH:0:16}C
132+
bashio::log.info "BLE_SC_NAME=$BLE_SC_NAME"
133+
134+
bashio::log.info "BLE_PRESENCE_ENABLE is true, initializing BLE listening loop counter"
135+
ble_listen_counter=0
136+
137+
bashio::log.info "Entering main MQTT & BLE listening loop"
138+
151139
else
152-
bashio::log.notice "Will not run proximity presence detection BLE_PRESENCE_ENABLE=false"
153-
counter=-1
154-
fi
140+
bashio::log.notice "BLE_PRESENCE_ENABLE is false, Will not run proximity presence detection"
141+
bashio::log.info "Entering main MQTT loop, not running BLE listening"
142+
143+
ble_listen_counter=-1
155144
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"
159145

146+
# Main loop
160147
while true
161148
do
162149
set +e
163150
listen_to_mqtt
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"
151+
if [ $ble_listen_counter -ge 0 ]; then
152+
((ble_listen_counter++))
153+
if [[ $ble_listen_counter -gt 90 ]]; then
154+
bashio::log.notice "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence"
168155
listen_to_ble
169-
counter=0
156+
ble_listen_counter=0
170157
fi
171158
fi
172159
sleep 2

0 commit comments

Comments
 (0)