Skip to content

Instantly share code, notes, and snippets.

@mieky
Last active March 28, 2024 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mieky/712f33f654240c5d76fb8c48bb1ea74b to your computer and use it in GitHub Desktop.
Save mieky/712f33f654240c5d76fb8c48bb1ea74b to your computer and use it in GitHub Desktop.
Fish script for toggling between Hue lighting scenes
# Sample fish shell script to toggle Hue scenes with curl using the home assistant hue service.
# See: https://www.home-assistant.io/integrations/hue/#service-hueactivate_scene
#
# Uses fish shell and jq, `brew install fish jq`.
#
# Usage:
# - export HA_KEY="<api_token_generated_in_home_assistant_ui>"
# - tweak the entity_ids to match yours (call /api/states to see available entities)
# - save this in fish/functions, and append "ha" to the end of your config.fish to load
function ha
# entry point to enable all the other commands
set --global ha_api_url http://homeassistant.local:8123/api
set --global cache_max_age_days 5
end
function ha-api-url
echo $ha_api_url
end
function hue-states
curl --silent \
-H "Authorization: Bearer $HA_KEY" \
-H "Content-Type: application/json" \
(ha-api-url)/states | jq
end
function hue-scenes -d "List available Hue scenes"
# note: $() syntax required around variables or the newlines will be eaten/converted to lists
set --local cache_key hue_scenes
set --local cache_content "$(_cache_get $cache_key)"
if test $status -eq 0
# echo "cache hit"
echo $cache_content
else
# echo "cache miss"
set --local curl_result "$(curl --silent \
-H "Authorization: Bearer $HA_KEY" \
-H "Content-Type: application/json" \
(ha-api-url)/states | jq | grep '"entity_id": "scene.tyohuone' | cut -d'.' -f2 | cut -d'"' -f1 | cut -d_ -f2- | sort)"
_cache_set $cache_key $curl_result
echo -e $curl_result
end
end
function hue-scene -d "Set Hue scene"
set --local scene $argv[1]
curl -X POST \
--silent \
-H "Authorization: Bearer $HA_KEY" \
-H "Content-Type: application/json" \
-d "{\"entity_id\": \"scene.tyohuone_$scene\", \"dynamic\": true }" \
(ha-api-url)/services/hue/activate_scene >/dev/null
end
function _cache_get
set --local cache_key $argv[1]
if not string length --quiet $cache_key
echo "missing cache key"
return 1
end
set --local cache_file (_get_cache_file $cache_key)
# echo "cache file: $cache_file"
if find $cache_file -mtime $cache_max_age_days 2>/dev/null
# echo "cache hit: found $cache_file less than $cache_max_age_days days old"
cat $cache_file
else
# cache miss
return 1
end
end
function _cache_set
set --local cache_key $argv[1]
if not string length --quiet $cache_key
echo "missing cache key"
return 1
end
set --local cache_file (_get_cache_file $cache_key)
set --local cache_content $argv[2..-1]
echo $cache_content >$cache_file
end
function _get_cache_file
set --local cache_key $argv[1]
echo /tmp/_fish_helpers_cache_$cache_key
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment