Ok, I’ve been messing with this for hours now, and I just cannot get it to work. I’m still pretty new to Home Assistant, so I’m doing most things in the UI yet, with minimal yaml when absolutely necessary.
Because of that, I couldn’t use the secrets file because you cannot access !secret from within the automations UI - and when I attempted to manually add the automations to the yaml file, it blew up ALL my automations and I got nothing but error 500.
So, I tried typing out all the required fields in the data of the service call in automations, so my automation looks like this:
- id: '164228015xxxx'
alias: Disable HA Port fwd
description: ''
trigger:
- platform: state
entity_id: input_boolean.everybody_home
from: 'off'
to: 'on'
condition: []
action:
- service: shell_command.unifi_portfwd
data:
username: root
password: password!
baseurl: 10.x.x.x
name: HA
enabled: 'false'
dst_port: '8123'
ip: 10.x.x.x
fwd_port: '8123'
proto: tcp
id: 654sfghbsfg6h54sfg6b5th
unifi_site: default
mode: single
The script I copied from your git:
#!/bin/sh
cookie=$(mktemp)
headers=$(mktemp)
curl_cmd="curl --silent --output /dev/null --cookie ${cookie} --cookie-jar ${cookie} --insecure"
portfwd() {
# authenticate against unifi controller
${curl_cmd} -H 'Content-Type: application/json' -D ${headers} -d "{\"username\":\"$1\", \"password\":\"$2\"}" https://${3}/api/auth/login
# grab the `x-csrf-token` and strip the newline (added when upgraded to controller 6.1.26)
csrf="$(awk -v FS=': ' '/^x-csrf-token/{print $2}' "${headers}" | tr -d '\r')"
# enable/disable firewall rule
${curl_cmd} -k -X PUT https://${3}/proxy/network/api/s/default/rest/portforward/${10} -H "Content-Type: application/json" -H "x-csrf-token: ${csrf}" -d @- <<-EOF
{
"name":"$4",
"enabled":$5,
"src":"any",
"dst_port":"$6",
"fwd":"$7",
"fwd_port":"$8",
"proto":"$9",
"log":false,
"_id":"$10",
"site_id":"$11",
"pfwd_interface":"wan",
"destination_ip":"any"
}
EOF
}
"$@"
The line I added to configuration.yaml:
shell_command:
unifi_portfwd: /bin/bash /config/scripts/unifi.sh portfwd {{ username }} {{ password }} {{ baseurl }} {{ name }} {{ enabled }} {{ dst_port }} {{ ip }} {{ fwd_port }} {{ proto }} {{ id }} {{ unifi_site }}