From 49352d5762245bdbef982017d0c6dd92ef254eec Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Mon, 17 Jun 2024 18:22:40 -0400 Subject: [PATCH 1/9] [translations] remove unsupported sections --- tesla_ble_mqtt/translations/en.yaml | 13 ++++++------- tesla_ble_mqtt/translations/fr.yaml | 13 ++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/tesla_ble_mqtt/translations/en.yaml b/tesla_ble_mqtt/translations/en.yaml index 490f736..5375267 100644 --- a/tesla_ble_mqtt/translations/en.yaml +++ b/tesla_ble_mqtt/translations/en.yaml @@ -11,16 +11,15 @@ configuration: mqtt_pwd: name: MQTT Password description: MQTT User's password - send_cmd_retry_delay: - name: Delay to retry a command - description: Delay to retry sending a command to the vehicle over BLE - debug: - name: Debug Logging - description: Print verbose messages to the log for debugging -vehicle: vin: name: Car's VIN description: Vehicle Identification Number found in Tesla app ble_mac: name: BLE MAC (optional for proximity detection) description: Car's BLE MAC (S___________C); Use Android "BLE scanner" or iOS "nRF Connect" + debug: + name: Debug Logging + description: Print verbose messages to the log for debugging + send_cmd_retry_delay: + name: Delay to retry a command + description: Delay to retry sending a command to the vehicle over BLE diff --git a/tesla_ble_mqtt/translations/fr.yaml b/tesla_ble_mqtt/translations/fr.yaml index 1a869e0..b0c66f1 100644 --- a/tesla_ble_mqtt/translations/fr.yaml +++ b/tesla_ble_mqtt/translations/fr.yaml @@ -11,16 +11,15 @@ configuration: mqtt_pwd: name: Mot de passe MQTT description: Mot de passe de l'utilisateur MQTT - send_cmd_retry_delay: - name: Délai pour re-essayer une commande - description: Délai pour re-essayer d'envoyer une command au véhicule via BLE - debug: - name: Journalisation du débogage - description: Imprimer des messages détaillés dans le journal pour le débogage -véhicule: vin: name: NIV du véhicule description: Numéro d'Identification de Véhicule affiché dans l'application Tesla ble_mac: name: BLE MAC (optionnel pour détection de proximité) description: BLE MAC (S___________C) du véhicule; Utiliser Android "BLE scanner" ou iOS "nRF Connect" + debug: + name: Journalisation du débogage + description: Imprimer des messages détaillés dans le journal pour le débogage + send_cmd_retry_delay: + name: Délai pour re-essayer une commande + description: Délai pour re-essayer d'envoyer une command au véhicule via BLE From 7d183faffe901067fae56f7e672e54548056a614 Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Mon, 17 Jun 2024 18:57:29 -0400 Subject: [PATCH 2/9] [bashio::log] set log level, prevent presence & always topic update --- tesla_ble_mqtt/CHANGELOG.md | 14 +++- tesla_ble_mqtt/config.yaml | 4 +- tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh | 74 ++++++++++----------- tesla_ble_mqtt/rootfs/app/run.sh | 65 ++++++++++++------ 4 files changed, 94 insertions(+), 63 deletions(-) diff --git a/tesla_ble_mqtt/CHANGELOG.md b/tesla_ble_mqtt/CHANGELOG.md index d9cd0e0..20028dd 100644 --- a/tesla_ble_mqtt/CHANGELOG.md +++ b/tesla_ble_mqtt/CHANGELOG.md @@ -1,11 +1,21 @@ +## 0.0.7a + +### Changed + +WARNING: broken for standalone deployment, stay on 0.0.6a +OK for HA Addon + +- logging fix debug and adjustment +- prevent always updating presence topic +- prevent checking for presence if BLE_MAC is empty + ## 0.0.7 ### Changed WARNING: broken for standalone deployment, stay on 0.0.6 -OK for HA Addon - Bump to latest updates from tesla_ble_mqtt_docker - Improve HA logging and error management @@ -55,4 +65,4 @@ OK for HA Addon ### Changed - Initial dev version based on https://github.com/iainbullock/tesla_ble_mqtt_docker -- WARN: standalone not tested \ No newline at end of file +- WARN: standalone not tested diff --git a/tesla_ble_mqtt/config.yaml b/tesla_ble_mqtt/config.yaml index ff62b33..e5a541e 100644 --- a/tesla_ble_mqtt/config.yaml +++ b/tesla_ble_mqtt/config.yaml @@ -1,5 +1,5 @@ name: "Tesla Local Commands" -version: "0.0.7" +version: "0.0.7a" slug: "tesla_local_commands" description: "Local BLE calls to control your Tesla." # url: "tbc" @@ -31,7 +31,7 @@ schema: debug: bool ble_mac: str? mqtt_ip: str? - mqtt_port: str? + mqtt_port: port mqtt_user: str? mqtt_pwd: password? send_cmd_retry_delay: str? diff --git a/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh b/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh index 2aca396..b642cf3 100644 --- a/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh +++ b/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh @@ -1,25 +1,25 @@ #!/bin/ash listen_to_mqtt() { - bashio::log.info "Connecting to MQTT server; subscribe topics tesla_ble/+ and homeassistant/status" + bashio::log.debug "Connecting to MQTT server; subscribe topics tesla_ble/+ and homeassistant/status" mosquitto_sub --nodelay -E -c -i tesla_ble_mqtt -q 1 -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/+ -t homeassistant/status -F "%t %p" | \ while read -r payload do topic=$(echo "$payload" | cut -d ' ' -f 1) msg=$(echo "$payload" | cut -d ' ' -f 2-) - bashio::log.green "Received MQTT message: $topic $msg" + bashio::log.info "Received MQTT message: $topic $msg" case $topic in tesla_ble/config) - bashio::log.green "Configuration $msg requested" + bashio::log.info "Configuration $msg requested" case $msg in generate_keys) - bashio::log.green "Generating the private key" + bashio::log.info "Generating the private key" openssl ecparam -genkey -name prime256v1 -noout > /share/tesla_ble_mqtt/private.pem cat /share/tesla_ble_mqtt/private.pem - bashio::log.green "Generating the public key" + bashio::log.info "Generating the public key" openssl ec -in /share/tesla_ble_mqtt/private.pem -pubout > /share/tesla_ble_mqtt/public.pem cat /share/tesla_ble_mqtt/public.pem - bashio::log.green "KEYS GENERATED. Next: + bashio::log.info "KEYS GENERATED. Next: 1/ Remove any previously deployed BLE keys from vehicle before deploying this one 2/ Wake the car up with your Tesla App 3/ Push the button 'Deploy Key'";; @@ -27,110 +27,110 @@ listen_to_mqtt() { bashio::log.yellow "Deploying public key to vehicle" send_key;; *) - bashio::log.red "Invalid Configuration request. Topic: $topic Message: $msg";; + bashio::log.error "Invalid Configuration request. Topic: $topic Message: $msg";; esac;; tesla_ble/command) - bashio::log.green "Command $msg requested" + bashio::log.info "Command $msg requested" case $msg in wake) - bashio::log.green "Waking Car" + bashio::log.info "Waking Car" send_command "-domain vcsec $msg";; trunk-open) - bashio::log.green "Opening Trunk" + bashio::log.info "Opening Trunk" send_command $msg;; trunk-close) - bashio::log.green "Closing Trunk" + bashio::log.info "Closing Trunk" send_command $msg;; charging-start) - bashio::log.green "Start Charging" + bashio::log.info "Start Charging" send_command $msg;; charging-stop) - bashio::log.green "Stop Charging" + bashio::log.info "Stop Charging" send_command $msg;; charge-port-open) - bashio::log.green "Open Charge Port" + bashio::log.info "Open Charge Port" send_command $msg;; charge-port-close) - bashio::log.green "Close Charge Port" + bashio::log.info "Close Charge Port" send_command $msg;; climate-on) - bashio::log.green "Start Climate" + bashio::log.info "Start Climate" send_command $msg;; climate-off) - bashio::log.green "Stop Climate" + bashio::log.info "Stop Climate" send_command $msg;; flash-lights) - bashio::log.green "Flash Lights" + bashio::log.info "Flash Lights" send_command $msg;; frunk-open) - bashio::log.green "Open Frunk" + bashio::log.info "Open Frunk" send_command $msg;; honk) - bashio::log.green "Honk Horn" + bashio::log.info "Honk Horn" send_command $msg;; lock) - bashio::log.green "Lock Car" + bashio::log.info "Lock Car" send_command $msg;; unlock) - bashio::log.green "Unlock Car" + bashio::log.info "Unlock Car" send_command $msg;; windows-close) - bashio::log.green "Close Windows" + bashio::log.info "Close Windows" send_command $msg;; windows-vent) - bashio::log.green "Vent Windows" + bashio::log.info "Vent Windows" send_command $msg;; *) - bashio::log.red "Invalid Command Request. Topic: $topic Message: $msg";; + bashio::log.error "Invalid Command Request. Topic: $topic Message: $msg";; esac;; tesla_ble/charging-set-amps) - bashio::log.green "Set Charging Amps to $msg requested" + bashio::log.info "Set Charging Amps to $msg requested" # https://github.com/iainbullock/tesla_ble_mqtt_docker/issues/4 if [ $msg -gt 4 ]; then - bashio::log.green "Set amps" + bashio::log.info "Set amps" send_command "charging-set-amps $msg" else - bashio::log.green "First Amp set" + bashio::log.info "First Amp set" send_command "charging-set-amps $msg" sleep 1 - bashio::log.green "Second Amp set" + bashio::log.info "Second Amp set" send_command "charging-set-amps $msg" fi ;; tesla_ble/climate-set-temp) - bashio::log.green "Set Climate Temp to $msg requested" + bashio::log.info "Set Climate Temp to $msg requested" send_command "climate-set-temp $msg";; tesla_ble/seat-heater) - bashio::log.green "Set Seat Heater to $msg requested" + bashio::log.info "Set Seat Heater to $msg requested" send_command "seat-heater $msg";; tesla_ble/auto-seat-and-climate) - bashio::log.green "Start Auto Seat and Climate" + bashio::log.info "Start Auto Seat and Climate" send_command "auto-seat-and-climate LR on";; tesla_ble/charging-set-limit) - bashio::log.green "Set Charging limit to $msg requested" + bashio::log.info "Set Charging limit to $msg requested" send_command "charging-set-limit $msg";; tesla_ble/heated_seat_left) - bashio::log.green "Set Seat heater to front-left $msg requested" + bashio::log.info "Set Seat heater to front-left $msg requested" send_command "seat-heater front-left $msg";; tesla_ble/heated_seat_right) - bashio::log.green "Set Seat heater to front-right $msg requested" + bashio::log.info "Set Seat heater to front-right $msg requested" send_command "seat-heater front-right $msg";; homeassistant/status) # https://github.com/iainbullock/tesla_ble_mqtt_docker/discussions/6 - bashio::log.green "Home Assistant is stopping or starting, re-running auto-discovery setup" + bashio::log.info "Home Assistant is stopping or starting, re-running auto-discovery setup" setup_auto_discovery;; *) - bashio::log.red "Invalid MQTT topic. Topic: $topic Message: $msg";; + bashio::log.error "Invalid MQTT topic. Topic: $topic Message: $msg";; esac done } diff --git a/tesla_ble_mqtt/rootfs/app/run.sh b/tesla_ble_mqtt/rootfs/app/run.sh index 68cbac2..38333ac 100644 --- a/tesla_ble_mqtt/rootfs/app/run.sh +++ b/tesla_ble_mqtt/rootfs/app/run.sh @@ -14,6 +14,9 @@ if [ -n "${HASSIO_TOKEN:-}" ]; then DEBUG="$(bashio::config 'debug')"; export DEBUG fi +# Set log level to debug? +bashio::config.true debug && bashio::log.level debug + bashio::log.cyan "tesla_ble_mqtt_docker by Iain Bullock 2024 https://github.com/iainbullock/tesla_ble_mqtt_docker" bashio::log.cyan "Inspiration by Raphael Murray https://github.com/raphmur" 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" @@ -29,24 +32,25 @@ bashio::log.green SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY if [ ! -d /share/tesla_ble_mqtt ] then + bashio::log.info SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY mkdir /share/tesla_ble_mqtt else - bashio::log.yellow "/share/tesla_ble_mqtt already exists, existing keys can be reused" + bashio::log.debug "/share/tesla_ble_mqtt already exists, existing keys can be reused" fi send_command() { for i in $(seq 5); do - bashio::log.yellow "Attempt $i/5" + bashio::log.notice "Attempt $i/5 to send command" set +e tesla-control -ble -vin $TESLA_VIN -key-name /share/tesla_ble_mqtt/private.pem -key-file /share/tesla_ble_mqtt/private.pem $1 EXIT_STATUS=$? set -e if [ $EXIT_STATUS -eq 0 ]; then - bashio::log.green "Ok" + bashio::log.info "tesla-control send command succeeded" break else - bashio::log.red "Error calling tesla-control, exit code=$EXIT_STATUS - will retry in $SEND_CMD_RETRY_DELAY seconds" + bashio::log.error "tesla-control send command failed exit status $EXIT_STATUS. Retrying in $SEND_CMD_RETRY_DELAY" sleep $SEND_CMD_RETRY_DELAY fi done @@ -54,7 +58,7 @@ send_command() { send_key() { for i in $(seq 5); do - bashio::log.yellow "Attempt $i/5" + bashio::log.notice "Attempt $i/5 to send public key" set +e tesla-control -ble -vin $TESLA_VIN add-key-request /share/tesla_ble_mqtt/public.pem owner cloud_key EXIT_STATUS=$? @@ -63,49 +67,66 @@ send_key() { bashio::log.yellow "KEY SENT TO VEHICLE: PLEASE CHECK YOU TESLA'S SCREEN AND ACCEPT WITH YOUR CARD" break else - bashio::log.red "COULD NOT SEND THE KEY. Is the car awake and sufficiently close to the bluetooth device?" + bashio::log.error "tesla-control could not send the key; make sure the car is awake and sufficiently close to the bluetooth device. Retrying in $SEND_CMD_RETRY_DELAY"" + bashio::log.error "Retrying in $SEND_CMD_RETRY_DELAY" sleep $SEND_CMD_RETRY_DELAY fi done } +PRESENCE_BINARY_SENSOR=OFF listen_to_ble() { - bashio::log.green "Listening to BLE" + PRESENCE_TIMEOUT=5 + bashio::log.info "Listening to BLE for presence" set +e - bluetoothctl --timeout 5 scan on | grep $BLE_MAC + bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_MAC EXIT_STATUS=$? set -e if [ $? -eq 0 ]; then - bashio::log.green "$BLE_MAC presence detected" - mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON + bashio::log.info "$BLE_MAC presence detected" + if [ $PRESENCE_BINARY_SENSOR == "OFF" ]; then + bashio::log.info "Updating topic tesla_ble/binary_sensor/presence ON" + mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON + PRESENCE_BINARY_SENSOR=ON + else + bashio::log.debug "Topic tesla_ble/binary_sensor/presence already ON" + fi else - bashio::log.yellow "$BLE_MAC presence not detected or issue in command, retrying now" - mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF + bashio::log.warning "$BLE_MAC presence not detected or issue in command, will retry later" + if [ $PRESENCE_BINARY_SENSOR == "ON" ]; then + bashio::log.info "Updating topic tesla_ble/binary_sensor/presence OFF" + mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF + PRESENCE_BINARY_SENSOR=OFF + else + bashio::log.debug "Topic tesla_ble/binary_sensor/presence already OFF" + fi fi } -bashio::log.green "Sourcing functions" +bashio::log.yellow "Sourcing functions" . /app/listen_to_mqtt.sh . /app/discovery.sh -bashio::log.green "Setting up auto discovery for Home Assistant" +bashio::log.info "Setting up auto discovery for Home Assistant" setup_auto_discovery -bashio::log.green "Connecting to MQTT to discard any unread messages" +bashio::log.info "Connecting to MQTT to discard any unread messages" mosquitto_sub -E -i tesla_ble_mqtt -h $MQTT_IP -p $MQTT_PORT -u $MQTT_USER -P $MQTT_PWD -t tesla_ble/+ -bashio::log.green "Initialize BLE listening loop counter" +bashio::log.info "Initialize BLE listening loop counter" counter=0 -bashio::log.green "Entering main MQTT & BLE listening loop" +bashio::log.info "Entering main MQTT & BLE listening loop" while true do set +e listen_to_mqtt - ((counter++)) - if [[ $counter -gt 90 ]]; then - bashio::log.green "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence" - listen_to_ble - counter=0 + if [ -z "$BLE_MAC" ]; then + ((counter++)) + if [[ $counter -gt 90 ]]; then + bashio::log.info "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence" + listen_to_ble + counter=0 + fi fi sleep 2 done From b9433ca6cd920f68bcf2a2c6209a5ce46b6829f1 Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Mon, 17 Jun 2024 19:17:50 -0400 Subject: [PATCH 3/9] remove trailing spaces --- tesla_ble_mqtt/rootfs/app/discovery.sh | 94 ++++++++++----------- tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh | 4 +- tesla_ble_mqtt/rootfs/app/run.sh | 2 +- 3 files changed, 50 insertions(+), 50 deletions(-) diff --git a/tesla_ble_mqtt/rootfs/app/discovery.sh b/tesla_ble_mqtt/rootfs/app/discovery.sh index 931bca3..40c002f 100644 --- a/tesla_ble_mqtt/rootfs/app/discovery.sh +++ b/tesla_ble_mqtt/rootfs/app/discovery.sh @@ -15,7 +15,7 @@ setup_auto_discovery() { "name": "Presence", "unique_id": "tesla_ble_presence" }' - + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/generate_keys/config -m \ '{ "command_topic": "tesla_ble/config", @@ -31,7 +31,7 @@ setup_auto_discovery() { "qos": 1, "unique_id": "tesla_ble_generate_keys", "entity_category": "config" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/deploy_key/config -m \ '{ @@ -48,7 +48,7 @@ setup_auto_discovery() { "qos": 1, "unique_id": "tesla_ble_deploy_key", "entity_category": "config" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/scan_bluetooth/config -m \ '{ @@ -65,7 +65,7 @@ setup_auto_discovery() { "qos": 1, "unique_id": "tesla_ble_scan_bluetooth", "entity_category": "diagnostic" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/wake/config -m \ '{ @@ -78,9 +78,9 @@ setup_auto_discovery() { }, "name": "Wake Car", "payload_press": "wake", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_wake" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/flash-lights/config -m \ '{ @@ -93,9 +93,9 @@ setup_auto_discovery() { }, "name": "Flash Lights", "payload_press": "flash-lights", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_flash_lights" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/honk/config -m \ '{ @@ -108,10 +108,10 @@ setup_auto_discovery() { }, "name": "Honk", "payload_press": "honk", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_honk" - }' - + }' + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/lock/config -m \ '{ "command_topic": "tesla_ble/command", @@ -123,9 +123,9 @@ setup_auto_discovery() { }, "name": "Lock Car", "payload_press": "lock", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_lock" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/unlock/config -m \ '{ @@ -138,9 +138,9 @@ setup_auto_discovery() { }, "name": "Unlock Car", "payload_press": "unlock", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_unlock" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/auto_seat-climate/config -m \ '{ @@ -153,9 +153,9 @@ setup_auto_discovery() { }, "name": "Auto Seat & Climate", "payload_press": "auto-seat-and-climate", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_auto_seat-climate" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/climate-off/config -m \ '{ @@ -168,9 +168,9 @@ setup_auto_discovery() { }, "name": "Climate Off", "payload_press": "climate-off", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_climate-off" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/climate-on/config -m \ '{ @@ -183,7 +183,7 @@ setup_auto_discovery() { }, "name": "Climate On", "payload_press": "climate-on", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_climate-on" }' @@ -198,10 +198,10 @@ setup_auto_discovery() { }, "name": "Open Trunk", "payload_press": "trunk-open", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_trunk-open" - }' - + }' + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/trunk-close/config -m \ '{ "command_topic": "tesla_ble/command", @@ -213,9 +213,9 @@ setup_auto_discovery() { }, "name": "Close Trunk", "payload_press": "trunk-close", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_trunk-close" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/frunk-open/config -m \ '{ @@ -228,9 +228,9 @@ setup_auto_discovery() { }, "name": "Open Frunk", "payload_press": "frunk-open", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_frunk-open" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/charging-start/config -m \ '{ @@ -243,9 +243,9 @@ setup_auto_discovery() { }, "name": "Start Charging", "payload_press": "charging-start", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_charging-start" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/charging-stop/config -m \ '{ @@ -258,9 +258,9 @@ setup_auto_discovery() { }, "name": "Stop Charging", "payload_press": "charging-stop", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_charging-stop" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/charge-port-open/config -m \ '{ @@ -273,9 +273,9 @@ setup_auto_discovery() { }, "name": "Open Charge Port", "payload_press": "charge-port-open", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_charge-port-open" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/charge-port-close/config -m \ '{ @@ -288,10 +288,10 @@ setup_auto_discovery() { }, "name": "Close Charge Port", "payload_press": "charge-port-close", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_charge-port-close" - }' - + }' + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/windows-close/config -m \ '{ "command_topic": "tesla_ble/command", @@ -303,10 +303,10 @@ setup_auto_discovery() { }, "name": "Close Windows", "payload_press": "windows-close", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_windows-close" - }' - + }' + mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/button/tesla_ble/windows-vent/config -m \ '{ "command_topic": "tesla_ble/command", @@ -318,7 +318,7 @@ setup_auto_discovery() { }, "name": "Vent Windows", "payload_press": "windows-vent", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_windows-vent" }' @@ -339,7 +339,7 @@ setup_auto_discovery() { "unit_of_measurement": "A", "qos": 1, "icon": "mdi:current-ac" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/number/tesla_ble/charging-set-limit/config -m \ '{ @@ -358,7 +358,7 @@ setup_auto_discovery() { "unit_of_measurement": "%", "qos": 1, "icon": "mdi:battery-90" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/number/tesla_ble/climate-set-temp/config -m \ '{ @@ -377,7 +377,7 @@ setup_auto_discovery() { "unit_of_measurement": "°C", "qos": 1, "icon": "mdi:temperature" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/number/tesla_ble/seat-heater/config -m \ '{ @@ -395,7 +395,7 @@ setup_auto_discovery() { "mode": "slider", "qos": 1, "icon": "mdi:temperature" - }' + }' mosquitto_pub -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t homeassistant/switch/tesla_ble/sw-heater/config -m \ '{ @@ -407,8 +407,8 @@ setup_auto_discovery() { "name": "Tesla_BLE_MQTT" }, "name": "Steering Wheel Heater", - "device_class": "switch", - "qos": 1, + "device_class": "switch", + "qos": 1, "unique_id": "tesla_ble_sw_heater" }' @@ -423,7 +423,7 @@ setup_auto_discovery() { }, "name": "Sentry Mode", "device_class": "switch", - "qos": 1, + "qos": 1, "unique_id": "tesla_ble_sentry-mode" }' diff --git a/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh b/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh index b642cf3..02a6d87 100644 --- a/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh +++ b/tesla_ble_mqtt/rootfs/app/listen_to_mqtt.sh @@ -115,11 +115,11 @@ listen_to_mqtt() { tesla_ble/charging-set-limit) bashio::log.info "Set Charging limit to $msg requested" send_command "charging-set-limit $msg";; - + tesla_ble/heated_seat_left) bashio::log.info "Set Seat heater to front-left $msg requested" send_command "seat-heater front-left $msg";; - + tesla_ble/heated_seat_right) bashio::log.info "Set Seat heater to front-right $msg requested" send_command "seat-heater front-right $msg";; diff --git a/tesla_ble_mqtt/rootfs/app/run.sh b/tesla_ble_mqtt/rootfs/app/run.sh index 38333ac..800b75d 100644 --- a/tesla_ble_mqtt/rootfs/app/run.sh +++ b/tesla_ble_mqtt/rootfs/app/run.sh @@ -71,7 +71,7 @@ send_key() { bashio::log.error "Retrying in $SEND_CMD_RETRY_DELAY" sleep $SEND_CMD_RETRY_DELAY fi - done + done } PRESENCE_BINARY_SENSOR=OFF From 50fbfb7034aabe9d13f0f181e53c4502f9f9faca Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Mon, 17 Jun 2024 20:06:56 -0400 Subject: [PATCH 4/9] update topic if needed --- tesla_ble_mqtt/rootfs/app/run.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tesla_ble_mqtt/rootfs/app/run.sh b/tesla_ble_mqtt/rootfs/app/run.sh index 800b75d..39c7c4c 100644 --- a/tesla_ble_mqtt/rootfs/app/run.sh +++ b/tesla_ble_mqtt/rootfs/app/run.sh @@ -14,7 +14,9 @@ if [ -n "${HASSIO_TOKEN:-}" ]; then DEBUG="$(bashio::config 'debug')"; export DEBUG fi -# Set log level to debug? +PRESENCE_BINARY_SENSOR=Unknown + +# Set log level to debug bashio::config.true debug && bashio::log.level debug bashio::log.cyan "tesla_ble_mqtt_docker by Iain Bullock 2024 https://github.com/iainbullock/tesla_ble_mqtt_docker" @@ -32,7 +34,7 @@ bashio::log.green SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY if [ ! -d /share/tesla_ble_mqtt ] then - bashio::log.info SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY + bashio::log.info "Creating directory /share/tesla_ble_mqtt" mkdir /share/tesla_ble_mqtt else bashio::log.debug "/share/tesla_ble_mqtt already exists, existing keys can be reused" @@ -74,7 +76,6 @@ send_key() { done } -PRESENCE_BINARY_SENSOR=OFF listen_to_ble() { PRESENCE_TIMEOUT=5 bashio::log.info "Listening to BLE for presence" @@ -84,7 +85,7 @@ listen_to_ble() { set -e if [ $? -eq 0 ]; then bashio::log.info "$BLE_MAC presence detected" - if [ $PRESENCE_BINARY_SENSOR == "OFF" ]; then + if [ $PRESENCE_BINARY_SENSOR == "OFF" || $PRESENCE_BINARY_SENSOR == "Unknown" ]; then bashio::log.info "Updating topic tesla_ble/binary_sensor/presence ON" mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON PRESENCE_BINARY_SENSOR=ON @@ -93,7 +94,7 @@ listen_to_ble() { fi else bashio::log.warning "$BLE_MAC presence not detected or issue in command, will retry later" - if [ $PRESENCE_BINARY_SENSOR == "ON" ]; then + if [ $PRESENCE_BINARY_SENSOR == "ON" || $PRESENCE_BINARY_SENSOR == "Unknown" ]; then bashio::log.info "Updating topic tesla_ble/binary_sensor/presence OFF" mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF PRESENCE_BINARY_SENSOR=OFF From d5f9f57c783e8fc6b963a8a4d966955ed79d73fe Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Mon, 17 Jun 2024 22:13:17 -0400 Subject: [PATCH 5/9] remove extra " --- tesla_ble_mqtt/rootfs/app/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tesla_ble_mqtt/rootfs/app/run.sh b/tesla_ble_mqtt/rootfs/app/run.sh index 39c7c4c..9f32751 100644 --- a/tesla_ble_mqtt/rootfs/app/run.sh +++ b/tesla_ble_mqtt/rootfs/app/run.sh @@ -69,7 +69,7 @@ send_key() { bashio::log.yellow "KEY SENT TO VEHICLE: PLEASE CHECK YOU TESLA'S SCREEN AND ACCEPT WITH YOUR CARD" break else - bashio::log.error "tesla-control could not send the key; make sure the car is awake and sufficiently close to the bluetooth device. Retrying in $SEND_CMD_RETRY_DELAY"" + bashio::log.error "tesla-control could not send the key; make sure the car is awake and sufficiently close to the bluetooth device. Retrying in $SEND_CMD_RETRY_DELAY" bashio::log.error "Retrying in $SEND_CMD_RETRY_DELAY" sleep $SEND_CMD_RETRY_DELAY fi From c340f66546e4adfbc935392e2fa51a25f3bd1847 Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Mon, 17 Jun 2024 22:54:01 -0400 Subject: [PATCH 6/9] Adjust UI's config list --- tesla_ble_mqtt/config.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tesla_ble_mqtt/config.yaml b/tesla_ble_mqtt/config.yaml index e5a541e..5c9c1a5 100644 --- a/tesla_ble_mqtt/config.yaml +++ b/tesla_ble_mqtt/config.yaml @@ -18,23 +18,23 @@ map: - share:rw startup: services options: - vin: "" - ble_mac: "" - debug: false mqtt_ip: "" mqtt_port: "1883" mqtt_user: "" mqtt_pwd: "" send_cmd_retry_delay: "5" + debug: false + vin: "" + ble_mac: "" schema: - vin: str? - debug: bool - ble_mac: str? mqtt_ip: str? - mqtt_port: port + mqtt_port: str? mqtt_user: str? mqtt_pwd: password? send_cmd_retry_delay: str? + debug: bool + vin: str? + ble_mac: str? # ingress: true # panel_icon: mdi:forward # backup_exclude: From 4d026f0b4b08b8c0b7ac72f1414374128f36c0ab Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Mon, 17 Jun 2024 22:54:43 -0400 Subject: [PATCH 7/9] Fix BLE presence --- tesla_ble_mqtt/CHANGELOG.md | 3 ++- tesla_ble_mqtt/config.yaml | 14 ++++++------ tesla_ble_mqtt/rootfs/app/run.sh | 37 +++++++++++++++++++------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/tesla_ble_mqtt/CHANGELOG.md b/tesla_ble_mqtt/CHANGELOG.md index 20028dd..6bb0a21 100644 --- a/tesla_ble_mqtt/CHANGELOG.md +++ b/tesla_ble_mqtt/CHANGELOG.md @@ -4,7 +4,7 @@ ### Changed -WARNING: broken for standalone deployment, stay on 0.0.6a +WARNING: broken for standalone deployment, stay on 0.0.6 OK for HA Addon - logging fix debug and adjustment @@ -16,6 +16,7 @@ OK for HA Addon ### Changed WARNING: broken for standalone deployment, stay on 0.0.6 +OK for HA Addon - Bump to latest updates from tesla_ble_mqtt_docker - Improve HA logging and error management diff --git a/tesla_ble_mqtt/config.yaml b/tesla_ble_mqtt/config.yaml index 5c9c1a5..bdcc3a9 100644 --- a/tesla_ble_mqtt/config.yaml +++ b/tesla_ble_mqtt/config.yaml @@ -27,14 +27,14 @@ options: vin: "" ble_mac: "" schema: - mqtt_ip: str? - mqtt_port: str? - mqtt_user: str? - mqtt_pwd: password? - send_cmd_retry_delay: str? + mqtt_ip: str + mqtt_port: str + mqtt_user: str + mqtt_pwd: password + send_cmd_retry_delay: int debug: bool - vin: str? - ble_mac: str? + vin: str + ble_mac: "str?" # ingress: true # panel_icon: mdi:forward # backup_exclude: diff --git a/tesla_ble_mqtt/rootfs/app/run.sh b/tesla_ble_mqtt/rootfs/app/run.sh index 9f32751..11b2659 100644 --- a/tesla_ble_mqtt/rootfs/app/run.sh +++ b/tesla_ble_mqtt/rootfs/app/run.sh @@ -23,14 +23,14 @@ bashio::log.cyan "tesla_ble_mqtt_docker by Iain Bullock 2024 https://github.com/ bashio::log.cyan "Inspiration by Raphael Murray https://github.com/raphmur" 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" -bashio::log.green "Configuration Options are:" -bashio::log.green TESLA_VIN=$TESLA_VIN -bashio::log.green BLE_MAC=$BLE_MAC -bashio::log.green MQTT_IP=$MQTT_IP -bashio::log.green MQTT_PORT=$MQTT_PORT -bashio::log.green MQTT_USER=$MQTT_USER -bashio::log.green "MQTT_PWD=Not Shown" -bashio::log.green SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY +bashio::log.green "Configuration Options are: + TESLA_VIN=$TESLA_VIN + BLE_MAC=$BLE_MAC + MQTT_IP=$MQTT_IP + MQTT_PORT=$MQTT_PORT + MQTT_USER=$MQTT_USER + MQTT_PWD=Not Shown + SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY" if [ ! -d /share/tesla_ble_mqtt ] then @@ -69,7 +69,7 @@ send_key() { bashio::log.yellow "KEY SENT TO VEHICLE: PLEASE CHECK YOU TESLA'S SCREEN AND ACCEPT WITH YOUR CARD" break else - bashio::log.error "tesla-control could not send the key; make sure the car is awake and sufficiently close to the bluetooth device. Retrying in $SEND_CMD_RETRY_DELAY" + bashio::log.error "tesla-control could not send the pubkey; make sure the car is awake and sufficiently close to the bluetooth device. Retrying in $SEND_CMD_RETRY_DELAY" bashio::log.error "Retrying in $SEND_CMD_RETRY_DELAY" sleep $SEND_CMD_RETRY_DELAY fi @@ -85,7 +85,7 @@ listen_to_ble() { set -e if [ $? -eq 0 ]; then bashio::log.info "$BLE_MAC presence detected" - if [ $PRESENCE_BINARY_SENSOR == "OFF" || $PRESENCE_BINARY_SENSOR == "Unknown" ]; then + if [[ $PRESENCE_BINARY_SENSOR == "OFF" || $PRESENCE_BINARY_SENSOR == "Unknown" ]]; then bashio::log.info "Updating topic tesla_ble/binary_sensor/presence ON" mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON PRESENCE_BINARY_SENSOR=ON @@ -94,7 +94,7 @@ listen_to_ble() { fi else bashio::log.warning "$BLE_MAC presence not detected or issue in command, will retry later" - if [ $PRESENCE_BINARY_SENSOR == "ON" || $PRESENCE_BINARY_SENSOR == "Unknown" ]; then + if [[ $PRESENCE_BINARY_SENSOR == "ON" || $PRESENCE_BINARY_SENSOR == "Unknown" ]]; then bashio::log.info "Updating topic tesla_ble/binary_sensor/presence OFF" mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF PRESENCE_BINARY_SENSOR=OFF @@ -114,14 +114,21 @@ setup_auto_discovery bashio::log.info "Connecting to MQTT to discard any unread messages" mosquitto_sub -E -i tesla_ble_mqtt -h $MQTT_IP -p $MQTT_PORT -u $MQTT_USER -P $MQTT_PWD -t tesla_ble/+ -bashio::log.info "Initialize BLE listening loop counter" -counter=0 -bashio::log.info "Entering main MQTT & BLE listening loop" +if [ -z "$BLE_MAC" ]; then + bashio::log.notice "BLE_MAC being undefined, presence detection for the car will not run" + counter=-1 + bashio::log.info "Entering main MQTT loop" +else + bashio::log.info "BLE_MAC is defined, initializing BLE listening loop counter" + bashio::log.info "Entering main MQTT & BLE listening loop" + counter=0 +fi + while true do set +e listen_to_mqtt - if [ -z "$BLE_MAC" ]; then + if [ $counter -ge 0 ]; then ((counter++)) if [[ $counter -gt 90 ]]; then bashio::log.info "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence" From 263c0866c4d9c894c4e3640498625fbd5ef50f9a Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Tue, 18 Jun 2024 10:26:54 -0400 Subject: [PATCH 8/9] [wip] adjust logging --- tesla_ble_mqtt/CHANGELOG.md | 4 +--- tesla_ble_mqtt/config.yaml | 22 +++++++++--------- tesla_ble_mqtt/rootfs/app/run.sh | 38 ++++++++++++++------------------ 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/tesla_ble_mqtt/CHANGELOG.md b/tesla_ble_mqtt/CHANGELOG.md index 6bb0a21..694e3a1 100644 --- a/tesla_ble_mqtt/CHANGELOG.md +++ b/tesla_ble_mqtt/CHANGELOG.md @@ -7,9 +7,7 @@ WARNING: broken for standalone deployment, stay on 0.0.6 OK for HA Addon -- logging fix debug and adjustment -- prevent always updating presence topic -- prevent checking for presence if BLE_MAC is empty +- logging adjustment and fix debug ## 0.0.7 diff --git a/tesla_ble_mqtt/config.yaml b/tesla_ble_mqtt/config.yaml index bdcc3a9..ff62b33 100644 --- a/tesla_ble_mqtt/config.yaml +++ b/tesla_ble_mqtt/config.yaml @@ -1,5 +1,5 @@ name: "Tesla Local Commands" -version: "0.0.7a" +version: "0.0.7" slug: "tesla_local_commands" description: "Local BLE calls to control your Tesla." # url: "tbc" @@ -18,23 +18,23 @@ map: - share:rw startup: services options: + vin: "" + ble_mac: "" + debug: false mqtt_ip: "" mqtt_port: "1883" mqtt_user: "" mqtt_pwd: "" send_cmd_retry_delay: "5" - debug: false - vin: "" - ble_mac: "" schema: - mqtt_ip: str - mqtt_port: str - mqtt_user: str - mqtt_pwd: password - send_cmd_retry_delay: int + vin: str? debug: bool - vin: str - ble_mac: "str?" + ble_mac: str? + mqtt_ip: str? + mqtt_port: str? + mqtt_user: str? + mqtt_pwd: password? + send_cmd_retry_delay: str? # ingress: true # panel_icon: mdi:forward # backup_exclude: diff --git a/tesla_ble_mqtt/rootfs/app/run.sh b/tesla_ble_mqtt/rootfs/app/run.sh index 11b2659..f79f780 100644 --- a/tesla_ble_mqtt/rootfs/app/run.sh +++ b/tesla_ble_mqtt/rootfs/app/run.sh @@ -14,8 +14,6 @@ if [ -n "${HASSIO_TOKEN:-}" ]; then DEBUG="$(bashio::config 'debug')"; export DEBUG fi -PRESENCE_BINARY_SENSOR=Unknown - # Set log level to debug bashio::config.true debug && bashio::log.level debug @@ -70,7 +68,6 @@ send_key() { break else bashio::log.error "tesla-control could not send the pubkey; make sure the car is awake and sufficiently close to the bluetooth device. Retrying in $SEND_CMD_RETRY_DELAY" - bashio::log.error "Retrying in $SEND_CMD_RETRY_DELAY" sleep $SEND_CMD_RETRY_DELAY fi done @@ -80,27 +77,26 @@ listen_to_ble() { PRESENCE_TIMEOUT=5 bashio::log.info "Listening to BLE for presence" set +e + if [ $? -eq 0 ]; then + bashio::log.green "$BLE_MAC presence detected" + mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/bin +ry_sensor/presence -m ON + else + bashio::log.yellow "$BLE_MAC presence not detected or issue in command, retrying now" + mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/bin +ary_sensor/presence -m OFF + fi + bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_MAC EXIT_STATUS=$? set -e - if [ $? -eq 0 ]; then - bashio::log.info "$BLE_MAC presence detected" - if [[ $PRESENCE_BINARY_SENSOR == "OFF" || $PRESENCE_BINARY_SENSOR == "Unknown" ]]; then - bashio::log.info "Updating topic tesla_ble/binary_sensor/presence ON" - mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON - PRESENCE_BINARY_SENSOR=ON - else - bashio::log.debug "Topic tesla_ble/binary_sensor/presence already ON" - fi - else - bashio::log.warning "$BLE_MAC presence not detected or issue in command, will retry later" - if [[ $PRESENCE_BINARY_SENSOR == "ON" || $PRESENCE_BINARY_SENSOR == "Unknown" ]]; then - bashio::log.info "Updating topic tesla_ble/binary_sensor/presence OFF" - mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF - PRESENCE_BINARY_SENSOR=OFF - else - bashio::log.debug "Topic tesla_ble/binary_sensor/presence already OFF" - fi + bashio::log.info "$BLE_MAC presence detected" + mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON + + bashio::log.warning "$BLE_MAC presence not detected or issue in command, will retry later" + bashio::log.info "Updating topic tesla_ble/binary_sensor/presence OFF" + mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF + bashio::log.debug "Topic tesla_ble/binary_sensor/presence already OFF" fi } From ef61d12b36adff008b64032e1ebcec97abeb9c46 Mon Sep 17 00:00:00 2001 From: Pierre Belanger Date: Tue, 18 Jun 2024 10:45:08 -0400 Subject: [PATCH 9/9] [logging] adjust msg & enable debug log --- tesla_ble_mqtt/CHANGELOG.md | 3 +- tesla_ble_mqtt/config.yaml | 2 +- tesla_ble_mqtt/rootfs/app/run.sh | 55 +++++++++++--------------------- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/tesla_ble_mqtt/CHANGELOG.md b/tesla_ble_mqtt/CHANGELOG.md index 694e3a1..5a1c78c 100644 --- a/tesla_ble_mqtt/CHANGELOG.md +++ b/tesla_ble_mqtt/CHANGELOG.md @@ -7,7 +7,8 @@ WARNING: broken for standalone deployment, stay on 0.0.6 OK for HA Addon -- logging adjustment and fix debug +- logging msg adjustments & fix logic for log.debug +- logging add logic for log.debug to properly work ## 0.0.7 diff --git a/tesla_ble_mqtt/config.yaml b/tesla_ble_mqtt/config.yaml index ff62b33..86e5165 100644 --- a/tesla_ble_mqtt/config.yaml +++ b/tesla_ble_mqtt/config.yaml @@ -1,5 +1,5 @@ name: "Tesla Local Commands" -version: "0.0.7" +version: "0.0.7a" slug: "tesla_local_commands" description: "Local BLE calls to control your Tesla." # url: "tbc" diff --git a/tesla_ble_mqtt/rootfs/app/run.sh b/tesla_ble_mqtt/rootfs/app/run.sh index f79f780..30a37bf 100644 --- a/tesla_ble_mqtt/rootfs/app/run.sh +++ b/tesla_ble_mqtt/rootfs/app/run.sh @@ -64,7 +64,7 @@ send_key() { EXIT_STATUS=$? set -e if [ $EXIT_STATUS -eq 0 ]; then - bashio::log.yellow "KEY SENT TO VEHICLE: PLEASE CHECK YOU TESLA'S SCREEN AND ACCEPT WITH YOUR CARD" + bashio::log.notice "KEY SENT TO VEHICLE: PLEASE CHECK YOU TESLA'S SCREEN AND ACCEPT WITH YOUR CARD" break else bashio::log.error "tesla-control could not send the pubkey; make sure the car is awake and sufficiently close to the bluetooth device. Retrying in $SEND_CMD_RETRY_DELAY" @@ -74,33 +74,23 @@ send_key() { } listen_to_ble() { - PRESENCE_TIMEOUT=5 bashio::log.info "Listening to BLE for presence" + PRESENCE_TIMEOUT=5 set +e - if [ $? -eq 0 ]; then - bashio::log.green "$BLE_MAC presence detected" - mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/bin -ry_sensor/presence -m ON - else - bashio::log.yellow "$BLE_MAC presence not detected or issue in command, retrying now" - mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/bin -ary_sensor/presence -m OFF - fi - bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_MAC EXIT_STATUS=$? set -e - bashio::log.info "$BLE_MAC presence detected" - mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON - - bashio::log.warning "$BLE_MAC presence not detected or issue in command, will retry later" - bashio::log.info "Updating topic tesla_ble/binary_sensor/presence OFF" - mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF - bashio::log.debug "Topic tesla_ble/binary_sensor/presence already OFF" + if [ $EXIT_STATUS -eq 0 ]; then + bashio::log.info "$BLE_MAC presence detected" + mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m ON + else + bashio::log.notice "$BLE_MAC presence not detected or issue in command" + mosquitto_pub --nodelay -h $MQTT_IP -p $MQTT_PORT -u "$MQTT_USER" -P "$MQTT_PWD" -t tesla_ble/binary_sensor/presence -m OFF fi + } -bashio::log.yellow "Sourcing functions" +bashio::log.notice "Sourcing functions" . /app/listen_to_mqtt.sh . /app/discovery.sh @@ -110,27 +100,18 @@ setup_auto_discovery bashio::log.info "Connecting to MQTT to discard any unread messages" mosquitto_sub -E -i tesla_ble_mqtt -h $MQTT_IP -p $MQTT_PORT -u $MQTT_USER -P $MQTT_PWD -t tesla_ble/+ -if [ -z "$BLE_MAC" ]; then - bashio::log.notice "BLE_MAC being undefined, presence detection for the car will not run" - counter=-1 - bashio::log.info "Entering main MQTT loop" -else - bashio::log.info "BLE_MAC is defined, initializing BLE listening loop counter" - bashio::log.info "Entering main MQTT & BLE listening loop" - counter=0 -fi - +bashio::log.info "Initialize BLE listening loop counter" +counter=0 +bashio::log.info "Entering main MQTT & BLE listening loop" while true do set +e listen_to_mqtt - if [ $counter -ge 0 ]; then - ((counter++)) - if [[ $counter -gt 90 ]]; then - bashio::log.info "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence" - listen_to_ble - counter=0 - fi + ((counter++)) + if [[ $counter -gt 90 ]]; then + bashio::log.info "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence" + listen_to_ble + counter=0 fi sleep 2 done