summaryrefslogtreecommitdiff
path: root/register_app.sh
blob: 9cec53be71548ee3ff79f8c241407f6f61b3ff00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env bash
set -euo pipefail

source functions.sh

application_name=tom-hue-stuff

read_vars

if [[ -z $var_ip || -z $var_bridgeid ]]; then
  echo >&2 "Variables ip and bridgeid required in my_bridge.txt"
  exit 1
fi

echo "Registering a new user on the Hue Bridge (using API V1)."
echo "Application name: $application_name"

read -r -p "Device name (e.g. iphone peter): " device_name

if [[ -z $device_name ]]; then
  echo >&2 "Device name required"
  exit 1
fi
if [[ -n "$(echo "$device_name" | tr -d 'a-zA-Z90-9 _-')" ]]; then
  echo >&2 "Invalid characters in device name"
  exit 1
fi

echo "Now press the button on the Bridge, and press enter here."
echo "Please press enter within 30 seconds of pressing the button."

read

reqdata=$(jq -c --arg app "$application_name" --arg dev "$device_name" '{"devicetype": ($app + "#" + $dev)}' <<<0)
# reqdata=$(jq -c --arg app "$application_name" --arg dev "$device_name" '{}' <<<0)
response=$(curl -s --cacert signify_root_cert.pem --resolve "$var_bridgeid:443:$var_ip" -d "$reqdata" https://"$var_bridgeid"/api)

if [[ $(jq '.[0] | has("success")' <<<"$response") = true ]]; then
  username=$(jq '.[0].success.username' <<<"$response")
  echo "username=$username"
else
  echo "Registration failed:"
  echo "$response"
  exit 1
fi