-
-
Save r15ch13/8bbd87f9ad6f70eefea044c45dc7cd53 to your computer and use it in GitHub Desktop.
Simple Octoprint PSU Control script to toggle a switch in Home Assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HA_HOST=http://myhomeassistant:8123 | |
HA_TOKEN=long lived token | |
HA_SWITCH=entity_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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