Skip to content

Commit 95d2a53

Browse files
authored
add webhook option to keep awake scripts (#1002)
1 parent 648f7ef commit 95d2a53

4 files changed

Lines changed: 101 additions & 7 deletions

File tree

pi-gen-sources/00-teslausb-tweaks/files/teslausb_setup_variables.conf.sample

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export CAM_SIZE=40G
250250
# TeslaUSB.
251251
#
252252
# In order to keep the car awake, several methods have been implemented:
253-
# TeslaFi, Tessie, and Bluetooth Low Energy (BLE). Please note that the previous
253+
# TeslaFi, Tessie, Bluetooth Low Energy (BLE), and Webhooks. Please note that the previous
254254
# implementation using the (unofficial) Tesla API was removed. Tesla had
255255
# superseded it with their official Fleet API (a paid service for developers).
256256
# Consequently, TeslaFi and Tessie now enforce request rate limits. As a result,
@@ -277,6 +277,12 @@ export CAM_SIZE=40G
277277
# - Some security vulnerability
278278
# - Requires paid 3rd-party service from TeslaFi or Tessie
279279
#
280+
# Webhook Pros/Cons:
281+
# + Does not require teslausb to store credentials that can directly
282+
# control your vehicle
283+
# - Requires another service like home assistant be setup to receive
284+
# webhooks and carry out the required actions to keep the vehicle awake
285+
#
280286
# DISCLAIMER:
281287
# All 3 methods have some level of security implications. For BLE,
282288
# key pair is stored on the device. For TeslaFi/Tessie, the API access token
@@ -317,7 +323,7 @@ export CAM_SIZE=40G
317323
# compatibility notes. If applicable, also make a 1-time change (set and
318324
# forget) to your in-car menu: Safety -> Sentry Mode.
319325
#
320-
# Case 1: [Compatible with TeslaFi, Tessie, or BLE]
326+
# Case 1: [Compatible with TeslaFi, Tessie, BLE, or Webhook]
321327
#
322328
# You want Sentry Mode turned On everywhere, except when parked safely
323329
# at home. Upon arriving home, Sentry Mode will already be ON
@@ -329,7 +335,7 @@ export CAM_SIZE=40G
329335
# >>>> IMPORTANT: Verify your in-car Sentry Mode is set to ON, and
330336
# 'Exclude Home' is UNCHECKED.
331337
#
332-
# Case 2: [Compatible with TeslaFi, Tessie, or BLE]
338+
# Case 2: [Compatible with TeslaFi, Tessie, BLE, or Webhook]
333339
#
334340
# You DON'T want Sentry Mode turned on anywhere, unless archiving.
335341
# Mode of operation: Upon arriving home, TeslaUSB will send a command
@@ -340,7 +346,7 @@ export CAM_SIZE=40G
340346
#
341347
# >>>> IMPORTANT: Verify your in-car Sentry Mode is set to OFF.
342348
#
343-
# Case 3: [Compatible with Tessie or BLE]
349+
# Case 3: [Compatible with Tessie, BLE, or Webhook]
344350
#
345351
# Sentry Mode is not used by TeslaUSB. Instead a periodic command is
346352
# sent to the car to keep it awake.
@@ -393,6 +399,16 @@ export CAM_SIZE=40G
393399
# Note: This does not prevent the car from locking, nor does it allow
394400
# someone to readily unlock or drive the car. An authroized key (card,
395401
# phone, fob) is still required.
402+
# . . . . . . . . . . . . . . . . . . . .
403+
#
404+
# For webhook, provide the URL of the webhook that will receive awake messages:
405+
#export KEEP_AWAKE_WEBHOOK_URL=http://domain/path
406+
#
407+
# A webhook message with a JSON body will be sent. The shape of the JSON will be { "awake_command": "<command>" }
408+
# Commands that will be sent to this webook:
409+
# 1. start: Sent when SENTRY_CASE=2 and the archive process starts
410+
# 2. stop: Sent when SENTRY_CASE=1 or 2 and the archive process stops
411+
# 3. nudge: Sent when SENTRY_CASE=3 and the archive process is ongoing
396412
################################################################################
397413

398414
# Temperature Monitor

run/awake_start

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash -eu
22

3-
if [[ ( -z "${TESLAFI_API_TOKEN:+x}" ) && ( -z "${TESSIE_API_TOKEN:+x}" ) && ( -z "${TESLA_BLE_VIN:+x}" ) ]]
3+
if [[ ( -z "${TESLAFI_API_TOKEN:+x}" ) && ( -z "${TESSIE_API_TOKEN:+x}" ) && ( -z "${TESLA_BLE_VIN:+x}" ) && ( -z "${KEEP_AWAKE_WEBHOOK_URL:+x}" ) ]]
44
then
55
log "Not configured to keep car awake."
66
else
@@ -61,6 +61,26 @@ else
6161
fi
6262
fi
6363
fi
64+
65+
elif [[ -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" ]]
66+
# Use webhook to send nudge message
67+
then
68+
http_response=$(curl -s -w "%{http_code}" POST \
69+
-d "{\"awake_command\":\"nudge\"}" \
70+
-H "Content-Type: application/json" \
71+
"$KEEP_AWAKE_WEBHOOK_URL")
72+
73+
log "Awake Webhook: Command sent \"nudge\""
74+
75+
# Extract HTTP status code
76+
http_status="${http_response: -3}"
77+
78+
# Check if HTTP status code is not 200 (OK)
79+
if [ "$http_status" -lt 200 ] || [ "$http_status" -ge 300 ]
80+
then
81+
# Log an error
82+
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."
83+
fi
6484
fi
6585

6686
sleep 300
@@ -160,6 +180,25 @@ else
160180
else
161181
log "Tesla BLE: Failed to set Sentry Mode."
162182
fi
183+
elif [[ -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" ]]
184+
# Use webhook to send start command
185+
then
186+
http_response=$(curl -s -w "%{http_code}" POST \
187+
-d "{\"awake_command\":\"start\"}" \
188+
-H "Content-Type: application/json" \
189+
"$KEEP_AWAKE_WEBHOOK_URL")
190+
191+
log "Awake Webhook: Command sent \"start\""
192+
193+
# Extract HTTP status code
194+
http_status="${http_response: -3}"
195+
196+
# Check if HTTP status code is not 200 (OK)
197+
if [ "$http_status" -lt 200 ] || [ "$http_status" -ge 300 ]
198+
then
199+
# Log an error
200+
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."
201+
fi
163202
fi
164203
;;
165204
3)

run/awake_stop

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash -eu
22

3-
if [[ ( -z "${TESLAFI_API_TOKEN:+x}" ) && ( -z "${TESSIE_API_TOKEN:+x}" ) && ( -z "${TESLA_BLE_VIN:+x}" ) ]]
3+
if [[ ( -z "${TESLAFI_API_TOKEN:+x}" ) && ( -z "${TESSIE_API_TOKEN:+x}" ) && ( -z "${TESLA_BLE_VIN:+x}" ) && ( -z "${KEEP_AWAKE_WEBHOOK_URL:+x}" ) ]]
44
then
55
exit
66
fi
@@ -94,6 +94,26 @@ case "${SENTRY_CASE}" in
9494
else
9595
log "Tesla BLE: Failed to set Sentry Mode."
9696
fi
97+
98+
elif [[ -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" ]]
99+
# Use webhook to send stop message
100+
then
101+
http_response=$(curl -s -w "%{http_code}" POST \
102+
-d "{\"awake_command\":\"stop\"}" \
103+
-H "Content-Type: application/json" \
104+
"$KEEP_AWAKE_WEBHOOK_URL")
105+
106+
log "Awake Webhook: Command sent \"stop\""
107+
108+
# Extract HTTP status code
109+
http_status="${http_response: -3}"
110+
111+
# Check if HTTP status code is not 200 (OK)
112+
if [ "$http_status" -lt 200 ] || [ "$http_status" -ge 300 ]
113+
then
114+
# Log an error
115+
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."
116+
fi
97117
fi
98118
;;
99119
3)

setup/pi/configure.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ function pip3_install () {
181181
function check_at_most_one_wake_api () {
182182
if [[ ( -n "${TESSIE_API_TOKEN:+x}" && -n "${TESLAFI_API_TOKEN:+x}" ) ||
183183
( -n "${TESSIE_API_TOKEN:+x}" && -n "${TESLA_BLE_VIN:+x}" ) ||
184-
( -n "${TESLAFI_API_TOKEN:+x}" && -n "${TESLA_BLE_VIN:+x}" ) ]]
184+
( -n "${TESLAFI_API_TOKEN:+x}" && -n "${TESLA_BLE_VIN:+x}" ) ||
185+
( -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" && -n "${TESLAFI_API_TOKEN:+x}" ) ||
186+
( -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" && -n "${TESLA_BLE_VIN:+x}" ) ||
187+
( -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" && -n "${TESSIE_API_TOKEN:+x}" )]]
185188
then
186189
log_progress "STOP: You're trying to set up multiple control providers at the same time."
187190
log_progress "Only 1 can be enabled at a time."
@@ -305,6 +308,21 @@ function check_and_configure_tesla_ble () {
305308
fi
306309
}
307310

311+
function check_awake_webhook () {
312+
if [[ ( -n "${KEEP_AWAKE_WEBHOOK_URL:+x}" ) ]]
313+
then
314+
check_variable "SENTRY_CASE"
315+
if [[ "$SENTRY_CASE" != 1 && "$SENTRY_CASE" != 2 && "$SENTRY_CASE" != 3 ]]; then
316+
log_progress "STOP: invalid SENTRY_CASE for Webhook."
317+
exit 1
318+
fi
319+
320+
log_progress "Awake Webhook enabled."
321+
else
322+
log_progress "Awake Webhook not enabled because no webhook URL was provided."
323+
fi
324+
}
325+
308326
function check_and_install_temperature_monitor () {
309327
local install_path="$1"
310328

@@ -757,6 +775,7 @@ then
757775
check_teslafi_api
758776
check_tessie_api
759777
check_and_configure_tesla_ble /root/bin
778+
check_awake_webhook
760779
fi
761780
check_and_install_temperature_monitor /root/bin
762781
check_and_configure_pushover

0 commit comments

Comments
 (0)