Skip to content

Commit 261ed7c

Browse files
committed
[feat] Auto MAC address detection
1 parent 8c104ba commit 261ed7c

File tree

7 files changed

+72
-101
lines changed

7 files changed

+72
-101
lines changed

CHANGELOG.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
# Changelog
22

3-
## 0.1.1
3+
## 0.2.0
44

55
### Changed
66

7-
<p>WARNING WARNING WARNING<br>
8-
DO NOT UPGRADE PRIOR TO READ THE 0.1.0 UPGRADE INSTRUCTIONS<br>
9-
WARNING WARNING WARNING</p>
7+
<p>**WARNING WARNING WARNING**<br>
8+
Upgrading from 0.0.10 or previous? DO NOT UPGRADE PRIOR TO READ THE 0.1.0 UPGRADE INSTRUCTIONS.</p>
9+
10+
- NEW Feature: BLE MAC address auto-detection (unless presence detection is disable)
11+
- CHG: Removed ble\_mac\_list; obsoleted by mac-auto-detection
12+
- CHG: Removed scan-bleln-macaddr; obsoleted by mac-auto-detection
13+
14+
## 0.1.2
15+
16+
### Changed
17+
18+
- CHG: Fix allow empty setting BLE MAC addr (Docker standalone)
19+
20+
## 0.1.1
21+
22+
### Changed
1023

1124
- CHG: Fix upgrade forcing to redeploy the key to the car
1225

env.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# shellcheck shell=dash
44
#
5-
export SW_VERSION=0.1.1
5+
export SW_VERSION=0.2.0
66

77
### LOAD LIBRARIES (FUNCTIONS & ENVIRONMENT ) #################################
88
echo "[$(date +%H:%M:%S)] loading libproduct.sh"
@@ -35,7 +35,7 @@ export PRESENCE_DETECTION_TTL=${PRESENCE_DETECTION_TTL:-240}
3535

3636
export BLE_LN_REGEX='S[0-9A-Fa-f]{16}C'
3737
export KEYS_DIR=/share/tesla_ble_mqtt
38-
export MAC_REGEX='([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})'
38+
export MAC_REGEX='([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})'
3939
export VIN_REGEX='[A-HJ-NPR-Z0-9]{17}'
4040

4141
### LOG CONFIG VARS ###########################################################

mqtt-discovery.sh

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,18 @@ function setupHADevicePanelCardsMain() {
5757
setupHADeviceDeployKeyButton $vin
5858
setupHADeviceGenerateKeysButton $vin
5959
setupHADeviceControlsCard $vin
60-
setupHADeviceScanBLElnButton $vin
6160
elif [ ! -f $KEYS_DIR/${vin}_private.pem ] && [ ! -f $KEYS_DIR/${vin}_public.pem ]; then
6261

6362
log_debug "setupHADevicePanelCardsMain() found new vehicle, need to generate keys set vin:$vin"
6463
# Show button to Generate Keys
6564
setupHADeviceGenerateKeysButton $vin
66-
setupHADeviceScanBLElnButton $vin
6765

6866
# listen_to_mqtt call setupHADeviceDeployKeyButton once the keys are generated
6967

7068
else
7169
log_debug "setupHADevicePanelCardsMain() found new vehicle, need to deploy public key vin:$vin"
7270
setupHADeviceDeployKeyButton $vin
7371
setupHADeviceGenerateKeysButton $vin
74-
setupHADeviceScanBLElnButton $vin
7572
fi
7673

7774
log_debug "setupHADevicePanelCardsMain() leaving vin:$vin"
@@ -635,40 +632,6 @@ function setupHADeviceDeployKeyButton() {
635632

636633
}
637634

638-
###
639-
##
640-
# Setup Scan BLE LN Button
641-
##
642-
###
643-
function setupHADeviceScanBLElnButton() {
644-
vin=$1
645-
646-
log_debug "setupHADeviceScanBLElnButton() entering vin:$vin"
647-
configHADeviceEnvVars $vin
648-
649-
echo '{
650-
"command_topic": "'${TOPIC_ROOT}'/config",
651-
"device": {
652-
"identifiers": [
653-
"'${DEV_ID}'"
654-
],
655-
"manufacturer": "tesla-local-control",
656-
"model": "Tesla_BLE",
657-
"name": "'${DEV_NAME}'",
658-
"sw_version": "'${SW_VERSION}'"
659-
},
660-
"device_class": "update",
661-
"name": "Scan Bluetooth",
662-
"payload_press": "scan-bleln-macaddr",
663-
"qos": "'${QOS_LEVEL}'",
664-
"unique_id": "'${DEV_ID}'_scan-bleln-macaddr",
665-
"entity_category": "diagnostic"
666-
}' | sed ':a;N;$!ba;s/\n//g' | retryMQTTpub -t homeassistant/button/${DEV_ID}/scan-bleln-macaddr/config -l
667-
668-
log_debug "setupHADeviceScanBLElnButton() leaving vin:$vin"
669-
670-
}
671-
672635
###
673636
##
674637
# Setup Device's Panel Cards for all VINS

mqtt-listen.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ listen_to_mqtt() {
6666
deployKeyMain $vin
6767
;;
6868

69-
scan-bleln-macaddr)
70-
log_notice 'scan-bleln-macaddr; calling scanBLEforMACaddr()'
71-
if ble_mac_addr=$(scanBLEforMACaddr $vin); then
72-
log_notice "Found BLE MAC addr for vin:$vin is $ble_mac_addr"
73-
fi
74-
;;
75-
7669
*)
7770
log_error "Invalid configuration request:$msg topic:$topic vin:$vin"
7871
;;

run.sh

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ echo "[$(date +%H:%M:%S)] Starting... loading /app/env.sh"
1010
. /app/env.sh
1111

1212
# Replace | with ' ' white space
13-
BLE_MAC_LIST=$(echo $BLE_MAC_LIST | sed -e 's/[|,;]/ /g')
1413
VIN_LIST=$(echo $VIN_LIST | sed -e 's/[|,;]/ /g')
1514

1615
vin_count=0
1716
for vin in $VIN_LIST; do
1817
# Populate BLE Local Names list
1918
vin_count=$((vin_count + 1))
2019
BLE_LN=$(vinToBLEln $vin)
21-
log_debug "Adding $BLE_LN to BLE_LN_LIST, count $vin_count"
20+
log_debug "Adding $BLE_LN to BLE_LN_LIST, vin_count:$vin_count"
2221
BLE_LN_LIST="$BLE_LN_LIST $BLE_LN"
22+
log_debug "Adding default value FF:FF:FF:FF:FF:FF to BLE_MAC_LIST"
23+
BLE_MAC_LIST="$BLE_MAC_LIST FF:FF:FF:FF:FF:FF"
24+
log_debug "Adding default value 0 to PRESENCE_EXPIRE_TIME_LIST"
25+
PRESENCE_EXPIRE_TIME_LIST="$PRESENCE_EXPIRE_TIME_LIST 0"
2326

2427
if [ -f $KEYS_DIR/${vin}_private.pem ] &&
2528
[ -f $KEYS_DIR/${vin}_public.pem ]; then
@@ -41,20 +44,17 @@ for vin in $VIN_LIST; do
4144
delete_legacies $vin
4245
fi # END TEMPORARY
4346
done
47+
# remove leading white space
48+
BLE_LN_LIST=$(echo $BLE_LN_LIST | sed -e 's/^ //g')
49+
BLE_MAC_LIST=$(echo $BLE_MAC_LIST | sed -e 's/^ //g')
50+
PRESENCE_EXPIRE_TIME_LIST=$(echo $PRESENCE_EXPIRE_TIME_LIST | sed -e 's/^ //g')
4451

45-
# Populate PRESENCE_EXPIRE_TIME_LIST only if Presence Detection is enable
46-
if [ $PRESENCE_DETECTION_TTL -gt 0 ]; then
47-
log_info "Presence detection is enable with a TTL of $PRESENCE_DETECTION_TTL seconds"
48-
ble_mac_addr_count=0
49-
# shellcheck disable=SC2034
50-
for ble_mac in $BLE_MAC_LIST; do
51-
ble_mac_addr_count=$((ble_mac_addr_count + 1))
52-
log_debug "Adding 0 to PRESENCE_EXPIRE_TIME_LIST, count $ble_mac_addr_count"
53-
PRESENCE_EXPIRE_TIME_LIST="$PRESENCE_EXPIRE_TIME_LIST 0"
54-
done
55-
else
56-
log_info "Presence detection is not enabled due to TTL of $PRESENCE_DETECTION_TTL seconds"
57-
fi
52+
# log _LIST values
53+
log_debug "VIN_LIST:$VIN_LIST"
54+
log_debug "BLE_LN:$BLE_LN"
55+
log_debug "BLE_LN_LIST:$BLE_LN_LIST"
56+
log_debug "BLE_MAC_LIST:$BLE_MAC_LIST"
57+
log_debug "PRESENCE_EXPIRE_TIME_LIST:$PRESENCE_EXPIRE_TIME_LIST"
5858

5959
# Setup HA auto discovery, or skip if HA backend is disable, and discard old MQTT messages
6060
discardMessages=yes
@@ -79,7 +79,7 @@ while :; do
7979
# Don't run presence detection if TTL is 0
8080

8181
# If PRESENCE_DETECTION_TTL > 0 and BLE_MAC_LIST is not empty
82-
if [ $PRESENCE_DETECTION_TTL -gt 0 ] && [ -n "$BLE_MAC_LIST" ]; then
82+
if [ $PRESENCE_DETECTION_TTL -gt 0 ]; then
8383
log_info "Launch BLE scanning for car presence every $PRESENCE_DETECTION_LOOP_DELAY seconds"
8484
listen_to_ble $vin_count
8585
# Run listen_to_ble every 3m

subroutines.sh

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,23 @@ replace_value_at_position() {
3535
# Function
3636
check_presence() {
3737
TYPE="$1" # BLE MAC, LN
38-
MATCH="$2"
38+
BLE_LN="$2"
39+
BLE_MAC="$3"
3940

4041
CURRENT_TIME_EPOCH=$(date +%s)
42+
MATCH="($BLE_LN|$BLE_MAC)"
43+
44+
if [ $BLE_MAC == "FF:FF:FF:FF:FF:FF" ]; then
45+
log_debug "check_presence; looking BLE_MAC for BLE_LN:$BLE_LN"
46+
if grepOutput=$(echo "${BLTCTL_OUT}" | grep $BLE_LN | tail -1); then
47+
BLE_MAC=$(echo $grepOutput | grep -Eo $MAC_REGEX)
48+
log_info "check_presence; found BLE MAC addr:$BLE_MAC for BLE_LN:$BLE_LN"
49+
else
50+
log_debug "check_presence; did not find BLE MAC addr for BLE_LN:$BLE_LN"
51+
fi
52+
fi
53+
# return value to calling function
54+
echo $BLE_MAC
4155

4256
if echo "${BLTCTL_OUT}" | grep -Eq "$MATCH"; then
4357
log_info "VIN $VIN $TYPE $MATCH presence detected"
@@ -85,9 +99,20 @@ bluetoothctl_read() {
8599
# Read BLE data from bluetoothctl or an input file
86100
if [ -z $BLECTL_FILE_INPUT ]; then
87101
log_debug "Launching bluetoothctl to check for BLE presence"
88-
BLECTL_TIMEOUT=11
89102
set +e
90-
BLTCTL_OUT=$(bluetoothctl --timeout $BLECTL_TIMEOUT scan on 2>&1 | grep -v DEL)
103+
BLTCTL_OUT=$({
104+
for bltctlCommand in "power on" devices "scan on"; do
105+
echo "$bltctlCommand"
106+
sleep 0.2
107+
done
108+
109+
# scan for 18 seconds (Tesla adverstisement each ~9s)
110+
sleep 18
111+
112+
echo "scan off"
113+
echo "power off"
114+
echo "exit"
115+
} | bluetoothctl)
91116
set -e
92117
else
93118
# Read from file, great for testing w/ no Bluetooth adapter
@@ -132,39 +157,16 @@ listen_to_ble() {
132157
MQTT_TOPIC="tesla_ble/$VIN/binary_sensor/presence"
133158

134159
# Check the presence using both MAC Addr and BLE Local Name
135-
log_debug "$(echo "$BLTCTL_OUT" | grep -E "($BLE_MAC|$BLE_LN)")"
136-
check_presence "BLE MAC & LN" "($BLE_MAC|$BLE_LN)"
160+
log_debug "BLTCTL_OUT:$(echo "$BLTCTL_OUT" | grep -E "($BLE_MAC|$BLE_LN)")"
161+
macAddr=$(check_presence "BLE LN & MAC" $BLE_LN $BLE_MAC)
162+
163+
# Add the MAC address to BLE_MAC_LIST if it's not already present at the position
164+
if [ "$macAddr" != "$BLE_MAC" ]; then
165+
eval "BLE_MAC_LIST=\$(echo \$BLE_MAC_LIST | awk '{\$${position}=\"$macAddr\"; print}')"
166+
log_debug "listen_to_ble; BLE_MAC_LIST:$BLE_MAC_LIST"
167+
fi
137168

138169
done
139170
sleep $PRESENCE_DETECTION_LOOP_DELAY
140171
done
141172
}
142-
143-
### scanBLEforMACaddr
144-
##
145-
# Uses BLE Local Name derived from the VIN to match a MAC addr in the output
146-
## of the command bluetoothctl "devices" and "scan on"
147-
###
148-
scanBLEforMACaddr() {
149-
# copied from legacy "scan_bluetooth" function. To decide if still relevant
150-
# note there is this PR https://github.com/tesla-local-control/tesla-local-control-addon/pull/32
151-
# quite old, but has the principles for auto populating the BLE MAC Address with only the VIN
152-
vin=$1
153-
154-
mac_regex='([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})'
155-
156-
ble_ln=$(vinToBLEln $vin)
157-
158-
log_info "Looking for vin:$vin in the BLE cache that matches ble_ln:$ble_ln"
159-
if ! bltctl_out=$(bluetoothctl --timeout 2 devices | grep $ble_ln | grep -Eo $mac_regex); then
160-
log_notice "Couldn't find a match in the cache for ble_ln:$ble_ln for vin:$vin"
161-
# Look for a BLE adverstisement matching ble_ln
162-
log_notice "Scanning (10 seconds) for BLE advertisement that matches ble_ln:$ble_ln vin:$vin"
163-
if ! bltctl_out=$(bluetoothctl --timeout 10 scan on | grep $ble_ln | grep -Eo $mac_regex); then
164-
log_notice "Couldn't find a BLE advertisement for ble_ln:$ble_ln vin:$vin"
165-
return 1
166-
fi
167-
fi
168-
echo $bltctl_out
169-
return 0
170-
}

version.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#
33
# shellcheck shell=dash
44
#
5-
export SW_VERSION=0.1.1
5+
export SW_VERSION=0.2.0

0 commit comments

Comments
 (0)