Skip to content

Commit e02c408

Browse files
committed
✨ Adds automatic configuration of the Hassio API token
1 parent e82abb7 commit e02c408

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/with-contenv bash
2+
# ==============================================================================
3+
# Community Hass.io Add-ons: Node-RED
4+
# Ensures the correct API key is in the Node-RED configuration
5+
# ==============================================================================
6+
# shellcheck disable=SC1091
7+
source /usr/lib/hassio-addons/base.sh
8+
9+
readonly CONFIG_FILE="/config/node-red/flows.json"
10+
11+
declare TMP_FILE
12+
declare url
13+
declare key
14+
declare base_query
15+
declare query
16+
17+
base_query='.[] | select(.url=="http://hassio/homeassistant" and .type=="server")'
18+
19+
url=$(jq -r "${base_query} | .url" "${CONFIG_FILE}")
20+
key=$(jq -r "${base_query} | .pass" "${CONFIG_FILE}")
21+
22+
if [[ "${url}" = "http://hassio/homeassistant"
23+
&& "${key}" != "${HASSIO_TOKEN}"
24+
]];
25+
then
26+
TMP_FILE=$(mktemp)
27+
28+
hass.log.info 'API token is incorrect in the Node-RED configuration, fixing...'
29+
30+
jq "(${base_query}) = ((${base_query}) + {\"pass\": \"${HASSIO_TOKEN}\"})" "${CONFIG_FILE}" > "${TMP_FILE}" \
31+
|| hass.die 'Failed to set Hass.io API token into the Node-RED config'
32+
33+
mv "${TMP_FILE}" "${CONFIG_FILE}" \
34+
|| hass.die 'Failed updating the Hass.io configuration'
35+
fi

0 commit comments

Comments
 (0)