diff --git a/pi-gen-sources/00-teslausb-tweaks/files/teslausb_setup_variables.conf.sample b/pi-gen-sources/00-teslausb-tweaks/files/teslausb_setup_variables.conf.sample index d4f22a2f..fb50e3d2 100644 --- a/pi-gen-sources/00-teslausb-tweaks/files/teslausb_setup_variables.conf.sample +++ b/pi-gen-sources/00-teslausb-tweaks/files/teslausb_setup_variables.conf.sample @@ -250,7 +250,7 @@ export CAM_SIZE=40G # TeslaUSB. # # In order to keep the car awake, several methods have been implemented: -# TeslaFi, Tessie, and Bluetooth Low Energy (BLE). Please note that the previous +# TeslaFi, Tessie, Bluetooth Low Energy (BLE), and Webhooks. Please note that the previous # implementation using the (unofficial) Tesla API was removed. Tesla had # superseded it with their official Fleet API (a paid service for developers). # Consequently, TeslaFi and Tessie now enforce request rate limits. As a result, @@ -277,6 +277,12 @@ export CAM_SIZE=40G # - Some security vulnerability # - Requires paid 3rd-party service from TeslaFi or Tessie # +# Webhook Pros/Cons: +# + Does not require teslausb to store credentials that can directly +# control your vehicle +# - Requires another service like home assistant be setup to receive +# webhooks and carry out the required actions to keep the vehicle awake +# # DISCLAIMER: # All 3 methods have some level of security implications. For BLE, # key pair is stored on the device. For TeslaFi/Tessie, the API access token @@ -317,7 +323,7 @@ export CAM_SIZE=40G # compatibility notes. If applicable, also make a 1-time change (set and # forget) to your in-car menu: Safety -> Sentry Mode. # -# Case 1: [Compatible with TeslaFi, Tessie, or BLE] +# Case 1: [Compatible with TeslaFi, Tessie, BLE, or Webhook] # # You want Sentry Mode turned On everywhere, except when parked safely # at home. Upon arriving home, Sentry Mode will already be ON @@ -329,7 +335,7 @@ export CAM_SIZE=40G # >>>> IMPORTANT: Verify your in-car Sentry Mode is set to ON, and # 'Exclude Home' is UNCHECKED. # -# Case 2: [Compatible with TeslaFi, Tessie, or BLE] +# Case 2: [Compatible with TeslaFi, Tessie, BLE, or Webhook] # # You DON'T want Sentry Mode turned on anywhere, unless archiving. # Mode of operation: Upon arriving home, TeslaUSB will send a command @@ -340,7 +346,7 @@ export CAM_SIZE=40G # # >>>> IMPORTANT: Verify your in-car Sentry Mode is set to OFF. # -# Case 3: [Compatible with Tessie or BLE] +# Case 3: [Compatible with Tessie, BLE, or Webhook] # # Sentry Mode is not used by TeslaUSB. Instead a periodic command is # sent to the car to keep it awake. @@ -393,6 +399,16 @@ export CAM_SIZE=40G # Note: This does not prevent the car from locking, nor does it allow # someone to readily unlock or drive the car. An authroized key (card, # phone, fob) is still required. +# . . . . . . . . . . . . . . . . . . . . +# +# For webhook, provide the URL of the webhook that will receive awake messages: +#export KEEP_AWAKE_WEBHOOK_URL=http://domain/path +# +# A webhook message with a JSON body will be sent. The shape of the JSON will be { "awake_command": "" } +# Commands that will be sent to this webook: +# 1. start: Sent when SENTRY_CASE=2 and the archive process starts +# 2. stop: Sent when SENTRY_CASE=1 or 2 and the archive process stops +# 3. nudge: Sent when SENTRY_CASE=3 and the archive process is ongoing ################################################################################ # Temperature Monitor diff --git a/run/awake_start b/run/awake_start index b4daef6f..858ac106 100644 --- a/run/awake_start +++ b/run/awake_start @@ -1,6 +1,6 @@ #!/bin/bash -eu -if [[ ( -z "${TESLAFI_API_TOKEN:+x}" ) && ( -z "${TESSIE_API_TOKEN:+x}" ) && ( -z "${TESLA_BLE_VIN:+x}" ) ]] +if [[ ( -z "${TESLAFI_API_TOKEN:+x}" ) && ( -z "${TESSIE_API_TOKEN:+x}" ) && ( -z "${TESLA_BLE_VIN:+x}" ) && ( -z "${KEEP_AWAKE_WEBHOOK_URL:+x}" ) ]] then log "Not configured to keep car awake." else @@ -61,6 +61,26 @@ else fi fi fi + + elif [[ -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" ]] + # Use webhook to send nudge message + then + http_response=$(curl -s -w "%{http_code}" POST \ + -d "{\"awake_command\":\"nudge\"}" \ + -H "Content-Type: application/json" \ + "$KEEP_AWAKE_WEBHOOK_URL") + + log "Awake Webhook: Command sent \"nudge\"" + + # Extract HTTP status code + http_status="${http_response: -3}" + + # Check if HTTP status code is not 200 (OK) + if [ "$http_status" -lt 200 ] || [ "$http_status" -ge 300 ] + then + # Log an error + log "Awake Webhook: Failed to send command \"nudge\". Check if your KEEP_AWAKE_WEBHOOK_URL is set correctly. If it is, check the service receiving webhooks for logs." + fi fi sleep 300 @@ -160,6 +180,25 @@ else else log "Tesla BLE: Failed to set Sentry Mode." fi + elif [[ -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" ]] + # Use webhook to send start command + then + http_response=$(curl -s -w "%{http_code}" POST \ + -d "{\"awake_command\":\"start\"}" \ + -H "Content-Type: application/json" \ + "$KEEP_AWAKE_WEBHOOK_URL") + + log "Awake Webhook: Command sent \"start\"" + + # Extract HTTP status code + http_status="${http_response: -3}" + + # Check if HTTP status code is not 200 (OK) + if [ "$http_status" -lt 200 ] || [ "$http_status" -ge 300 ] + then + # Log an error + log "Awake Webhook: Failed to send command \"start\". Check if your KEEP_AWAKE_WEBHOOK_URL is set correctly. If it is, check the service receiving webhooks for logs." + fi fi ;; 3) diff --git a/run/awake_stop b/run/awake_stop index 723b5788..5c56764d 100644 --- a/run/awake_stop +++ b/run/awake_stop @@ -1,6 +1,6 @@ #!/bin/bash -eu -if [[ ( -z "${TESLAFI_API_TOKEN:+x}" ) && ( -z "${TESSIE_API_TOKEN:+x}" ) && ( -z "${TESLA_BLE_VIN:+x}" ) ]] +if [[ ( -z "${TESLAFI_API_TOKEN:+x}" ) && ( -z "${TESSIE_API_TOKEN:+x}" ) && ( -z "${TESLA_BLE_VIN:+x}" ) && ( -z "${KEEP_AWAKE_WEBHOOK_URL:+x}" ) ]] then exit fi @@ -94,6 +94,26 @@ case "${SENTRY_CASE}" in else log "Tesla BLE: Failed to set Sentry Mode." fi + + elif [[ -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" ]] + # Use webhook to send stop message + then + http_response=$(curl -s -w "%{http_code}" POST \ + -d "{\"awake_command\":\"stop\"}" \ + -H "Content-Type: application/json" \ + "$KEEP_AWAKE_WEBHOOK_URL") + + log "Awake Webhook: Command sent \"stop\"" + + # Extract HTTP status code + http_status="${http_response: -3}" + + # Check if HTTP status code is not 200 (OK) + if [ "$http_status" -lt 200 ] || [ "$http_status" -ge 300 ] + then + # Log an error + log "Awake Webhook: Failed to send command \"stop\". Check if your KEEP_AWAKE_WEBHOOK_URL is set correctly. If it is, check the service receiving webhooks for logs." + fi fi ;; 3) diff --git a/setup/pi/configure.sh b/setup/pi/configure.sh index e15cfb2f..a046bc4c 100644 --- a/setup/pi/configure.sh +++ b/setup/pi/configure.sh @@ -181,7 +181,10 @@ function pip3_install () { function check_at_most_one_wake_api () { if [[ ( -n "${TESSIE_API_TOKEN:+x}" && -n "${TESLAFI_API_TOKEN:+x}" ) || ( -n "${TESSIE_API_TOKEN:+x}" && -n "${TESLA_BLE_VIN:+x}" ) || - ( -n "${TESLAFI_API_TOKEN:+x}" && -n "${TESLA_BLE_VIN:+x}" ) ]] + ( -n "${TESLAFI_API_TOKEN:+x}" && -n "${TESLA_BLE_VIN:+x}" ) || + ( -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" && -n "${TESLAFI_API_TOKEN:+x}" ) || + ( -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" && -n "${TESLA_BLE_VIN:+x}" ) || + ( -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" && -n "${TESSIE_API_TOKEN:+x}" )]] then log_progress "STOP: You're trying to set up multiple control providers at the same time." log_progress "Only 1 can be enabled at a time." @@ -305,6 +308,21 @@ function check_and_configure_tesla_ble () { fi } +function check_awake_webhook () { + if [[ ( -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" ) ]] + then + check_variable "SENTRY_CASE" + if [[ "$SENTRY_CASE" != 1 && "$SENTRY_CASE" != 2 && "$SENTRY_CASE" != 3 ]]; then + log_progress "STOP: invalid SENTRY_CASE for Webhook." + exit 1 + fi + + log_progress "Awake Webhook enabled." + else + log_progress "Awake Webhook not enabled because no webhook URL was provided." + fi +} + function check_and_install_temperature_monitor () { local install_path="$1" @@ -757,6 +775,7 @@ then check_teslafi_api check_tessie_api check_and_configure_tesla_ble /root/bin + check_awake_webhook fi check_and_install_temperature_monitor /root/bin check_and_configure_pushover