diff --git a/standalone/start_tesla_ble_mqtt.sh b/standalone/start_tesla_ble_mqtt.sh index 304490c..3a37e90 100644 --- a/standalone/start_tesla_ble_mqtt.sh +++ b/standalone/start_tesla_ble_mqtt.sh @@ -7,7 +7,8 @@ MQTT_PORT=1883 MQTT_USER="" MQTT_PWD="" SEND_CMD_RETRY_DELAY=5 -BLE_MAC=00:00:00:00:00:00 +BLE_PRESENCE_ENABLE=true +DEBUG=false ############################################################ @@ -32,21 +33,22 @@ fi echo "Create docker structure..." docker volume create tesla_ble_mqtt -echo "Start main docker container with configuration Options:" - -echo TESLA_VIN=$TESLA_VIN -echo MQTT_IP=$MQTT_IP -echo MQTT_PORT=$MQTT_PORT -echo MQTT_USER=$MQTT_USER -echo "MQTT_PWD=Not Shown" -echo SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY -echo BLE_MAC=$BLE_MAC +echo "Start main docker container with configuration Options: + TESLA_VIN=$TESLA_VIN + MQTT_IP=$MQTT_IP + MQTT_PORT=$MQTT_PORT + MQTT_USER=$MQTT_USER + MQTT_PWD=Not Shown + SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY + BLE_PRESENCE_ENABLE=$BLE_PRESENCE_ENABLE + DEBUG=$DEBUG" docker-compose up -d \ - -e TESLA_VIN=$TESLA_VIN \ - -e MQTT_IP=$MQTT_IP \ - -e MQTT_PORT=$MQTT_PORT \ - -e MQTT_USER=$MQTT_USER \ - -e MQTT_PWD=$MQTT_PWD \ - -e SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY \ - -e BLE_MAC=$BLE_MAC + -e TESLA_VIN=$TESLA_VIN \ + -e MQTT_IP=$MQTT_IP \ + -e MQTT_PORT=$MQTT_PORT \ + -e MQTT_USER=$MQTT_USER \ + -e MQTT_PWD=$MQTT_PWD \ + -e SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY \ + -e BLE_PRESENCE_ENABLE=$BLE_PRESENCE_ENABLE \ + -e DEBUG=$DEBUG diff --git a/tesla_ble_mqtt/CHANGELOG.md b/tesla_ble_mqtt/CHANGELOG.md index 5a1c78c..dd16eca 100644 --- a/tesla_ble_mqtt/CHANGELOG.md +++ b/tesla_ble_mqtt/CHANGELOG.md @@ -1,5 +1,21 @@ +## 0.0.8a + +### Changed + +- [ble presence] Add functionality to enable or not presence detection via config UI +- [ble presence] Remove BLE_MAC from config option +- [ble presence] Rename BLE_MAC to BLE_LOCAL_NAME +- [ble presence] Add logic to generate BLE_LOCAL_NAME + +## 0.0.8 + +### Changed + +- [Standalone] Fix broken deployment introduced in 0.0.7 +- [Standalone] Add colored logging based on log level + ## 0.0.7a ### Changed diff --git a/tesla_ble_mqtt/Dockerfile b/tesla_ble_mqtt/Dockerfile index 75f634b..713f28b 100644 --- a/tesla_ble_mqtt/Dockerfile +++ b/tesla_ble_mqtt/Dockerfile @@ -19,7 +19,8 @@ RUN apk add --no-cache \ openssl \ bluez \ mosquitto-clients \ - python3 + python3 \ + py3-cryptography # Python 3 HTTP Server serves the current working dir WORKDIR /data diff --git a/tesla_ble_mqtt/config.yaml b/tesla_ble_mqtt/config.yaml index 86e5165..1b7eadb 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.8a" slug: "tesla_local_commands" description: "Local BLE calls to control your Tesla." # url: "tbc" @@ -19,22 +19,22 @@ map: startup: services options: vin: "" - ble_mac: "" debug: false mqtt_ip: "" mqtt_port: "1883" mqtt_user: "" mqtt_pwd: "" send_cmd_retry_delay: "5" + ble_presence_enable: true schema: vin: str? debug: bool - ble_mac: str? mqtt_ip: str? mqtt_port: str? mqtt_user: str? mqtt_pwd: password? send_cmd_retry_delay: str? + ble_presence_enable: bool # 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 30a37bf..e8d5def 100644 --- a/tesla_ble_mqtt/rootfs/app/run.sh +++ b/tesla_ble_mqtt/rootfs/app/run.sh @@ -5,13 +5,33 @@ # read options in case of HA addon. Otherwise, they will be sent as environment variables if [ -n "${HASSIO_TOKEN:-}" ]; then TESLA_VIN="$(bashio::config 'vin')"; export TESLA_VIN - BLE_MAC="$(bashio::config 'ble_mac')"; export BLE_MAC MQTT_IP="$(bashio::config 'mqtt_ip')"; export MQTT_IP MQTT_PORT="$(bashio::config 'mqtt_port')"; export MQTT_PORT MQTT_USER="$(bashio::config 'mqtt_user')"; export MQTT_USER MQTT_PWD="$(bashio::config 'mqtt_pwd')"; export MQTT_PWD SEND_CMD_RETRY_DELAY="$(bashio::config 'send_cmd_retry_delay')"; export SEND_CMD_RETRY_DELAY + BLE_PRESENCE_ENABLE="$(bashio::config 'ble_presence_enable')"; export BLE_PRESENCE_ENABLE DEBUG="$(bashio::config 'debug')"; export DEBUG +else + NOCOLOR='\033[0m' + GREEN='\033[0;32m' + CYAN='\033[0;36m' + YELLOW='\033[1;32m' + MAGENTA='\033[0;35m' + RED='\033[0;31m' + + function bashio::log.debug { [ $DEBUG == "true" ] && echo -e "${NOCOLOR}$1"; } + function bashio::log.info { echo -e "${GREEN}$1${NOCOLOR}"; } + function bashio::log.notice { echo -e "${CYAN}$1${NOCOLOR}"; } + function bashio::log.warning { echo -e "${YELLOW}$1${NOCOLOR}"; } + function bashio::log.error { echo -e "${MAGENTA}$1${NOCOLOR}"; } + function bashio::log.fatal { echo -e "${RED}$1${NOCOLOR}"; } + + function bashio::log.cyan { echo -e "${CYAN}$1${NOCOLOR}"; } + function bashio::log.green { echo -e "${GREEN}$1${NOCOLOR}"; } + function bashio::log.magenta { echo -e "${GREEN}$1${NOCOLOR}"; } + function bashio::log.red { echo -e "${RED}$1${NOCOLOR}"; } + function bashio::log.yellow { echo -e "${YELLOW}$1${NOCOLOR}"; } fi # Set log level to debug @@ -23,12 +43,13 @@ bashio::log.cyan "Instructions by Shankar Kumarasamy https://shankarkumarasamy.b 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" + SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY + BLE_PRESENCE_ENABLE=$BLE_PRESENCE_ENABLE + DEBUG=$DEBUG" if [ ! -d /share/tesla_ble_mqtt ] then @@ -77,19 +98,20 @@ listen_to_ble() { bashio::log.info "Listening to BLE for presence" PRESENCE_TIMEOUT=5 set +e - bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_MAC + bluetoothctl --timeout $PRESENCE_TIMEOUT scan on | grep $BLE_LOCAL_NAME EXIT_STATUS=$? set -e if [ $EXIT_STATUS -eq 0 ]; then - bashio::log.info "$BLE_MAC presence detected" + bashio::log.info "$BLE_LOCAL_NAME 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" + bashio::log.notice "$BLE_LOCAL_NAME 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.notice "Sourcing functions" . /app/listen_to_mqtt.sh . /app/discovery.sh @@ -100,18 +122,38 @@ 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" +# Run BLE presence if BLE_PRESENCE_ENABLE is true +if [ $BLE_PRESENCE_ENABLE == "true" ]; then + + # Generate BLE_LOCAL_NAME from TESLA_VIN + TESLA_VIN_HASH=$(echo -n "$TESLA_VIN" | sha1sum) + BLE_LOCAL_NAME=S${TESLA_VIN_HASH:0:16}C + bashio::log.info "BLE_LOCAL_NAME=$BLE_LOCAL_NAME" + + bashio::log.info "BLE_PRESENCE_ENABLE is true, initializing BLE listening loop counter" + ble_listen_counter=0 + + bashio::log.info "Entering main MQTT & BLE listening loop" + + else + bashio::log.notice "BLE_PRESENCE_ENABLE is false, Will not run proximity presence detection" + bashio::log.info "Entering main MQTT loop, not running BLE listening" + + ble_listen_counter=-1 +fi + +# Main loop while true do set +e listen_to_mqtt - ((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 + if [ $ble_listen_counter -ge 0 ]; then + ((ble_listen_counter++)) + if [[ $ble_listen_counter -gt 90 ]]; then + bashio::log.notice "Reached 90 MQTT loops (~3min): Launch BLE scanning for car presence" + listen_to_ble + ble_listen_counter=0 + fi fi sleep 2 done diff --git a/tesla_ble_mqtt/translations/en.yaml b/tesla_ble_mqtt/translations/en.yaml index 5375267..7c93613 100644 --- a/tesla_ble_mqtt/translations/en.yaml +++ b/tesla_ble_mqtt/translations/en.yaml @@ -14,12 +14,13 @@ configuration: 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 + ble_presence_enable: + name: BLE proximity presence detection + description: Controls if BLE proximity presence is enable or not + description: Detection over BLE of proximity presence diff --git a/tesla_ble_mqtt/translations/fr.yaml b/tesla_ble_mqtt/translations/fr.yaml index b0c66f1..fe17347 100644 --- a/tesla_ble_mqtt/translations/fr.yaml +++ b/tesla_ble_mqtt/translations/fr.yaml @@ -14,12 +14,12 @@ configuration: 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 + ble_presence_enable: + name: Détection de présence BLE par proximitée + description: Contrôle de la détection de présence par proximité basé sur BLE \ No newline at end of file