Skip to content

Instantly share code, notes, and snippets.

@r15ch13
Created May 22, 2021 09:30
Show Gist options
  • Save r15ch13/8bbd87f9ad6f70eefea044c45dc7cd53 to your computer and use it in GitHub Desktop.
Save r15ch13/8bbd87f9ad6f70eefea044c45dc7cd53 to your computer and use it in GitHub Desktop.
Simple Octoprint PSU Control script to toggle a switch in Home Assistant
HA_HOST=http://myhomeassistant:8123
HA_TOKEN=long lived token
HA_SWITCH=entity_id
#!/bin/bash
source $(dirname $0)/.ha
HA_BODY="{\"entity_id\": \"${HA_SWITCH}\"}"
case "$1" in
on)
curl -s -X POST -H "Authorization: Bearer ${HA_TOKEN}" \
-H "Content-Type: application/json" \
-d "$HA_BODY" \
"${HA_HOST}/api/services/switch/turn_on" > /dev/null
;;
off)
curl -s -X POST -H "Authorization: Bearer ${HA_TOKEN}" \
-H "Content-Type: application/json" \
-d "$HA_BODY" \
"${HA_HOST}/api/services/switch/turn_off" > /dev/null
;;
status)
curl -s -X GET -H "Authorization: Bearer ${HA_TOKEN}" \
-H "Content-Type: application/json" \
"${HA_HOST}/api/states/${HA_SWITCH}" | grep '"state": "on"'
exit $?
;;
*)
echo "Usage $0 on|off|status"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment