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
66if [ -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
3333 function bashio::log.yellow { echo -e " ${YELLOW} $1 ${NOCOLOR} " ; }
3434fi
3535
36+ # ## INITIALIZE AND LOG CONFIG VARS ##################################################################################
3637# Set log level to debug
3738bashio::config.true debug && bashio::log.level debug
3839
@@ -42,7 +43,6 @@ bashio::log.cyan "Instructions by Shankar Kumarasamy https://shankarkumarasamy.b
4243
4344bashio::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
5858 bashio::log.debug " /share/tesla_ble_mqtt already exists, existing keys can be reused"
5959fi
6060
61-
61+ # ## DEFINE FUNCTIONS ###############################################################################################
6262send_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
9696listen_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 ###########################################################################################
113113bashio::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"
118118setup_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"
121121mosquitto_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"
124133counter=0
125- bashio::log.info " Entering main MQTT & BLE listening loop"
126134while true
127135do
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
137158done
0 commit comments