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.

Revisions

  1. r15ch13 created this gist May 22, 2021.
    3 changes: 3 additions & 0 deletions .ha
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    HA_HOST=http://myhomeassistant:8123
    HA_TOKEN=long lived token
    HA_SWITCH=entity_id
    28 changes: 28 additions & 0 deletions psu_switch.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/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