From 76e9e832ce930dafde8b5726a8c35f0ad33ddf9e Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Sun, 9 Nov 2025 09:52:01 +0000 Subject: [PATCH] Adds very basic hass script --- bin/.bin/hass | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 bin/.bin/hass diff --git a/bin/.bin/hass b/bin/.bin/hass new file mode 100755 index 00000000..0da3dd94 --- /dev/null +++ b/bin/.bin/hass @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +URL="https://hass.hodgson.one" +TOKEN="$(pass websites/homeassistant-token)" + +get(){ + curl --silent --header "Authorization: Bearer $TOKEN" "$URL$1" +} + +post(){ + curl --silent --header "content-type: application/json" --header "Authorization: Bearer $TOKEN" --data-binary @- "$URL$1" +} + +listEntities(){ + get '/api/states' | + jq -r '.[] | [.entity_id, .attributes.friendly_name // .entity_id, .state] | @tsv' +} + +listServices(){ + get '/api/services' +} + +selectEntity(){ + listEntities | fzf | cut -d $'\t' -f 1 +} + + +toggleEntity(){ + local entity; + if [ -n "$1" ]; then + entity="$1" + else + entity="$(cat -)" + fi + + local domain="${entity%%.*}" + + echo "{\"entity_id\":\"$entity\"}" | post /api/services/$domain/toggle + +} + +if [ -n "$1" ]; then + case "$1" in + "--listEntities") listEntities ;; + "--listServices") listServices ;; + *) toggleEntity "$1" + esac +else + selectEntity | toggleEntity +fi