Skip to content

Instantly share code, notes, and snippets.

@leshy
Created September 28, 2024 10:07
Show Gist options
  • Select an option

  • Save leshy/3115593a59cd5ae2349238a903397ce4 to your computer and use it in GitHub Desktop.

Select an option

Save leshy/3115593a59cd5ae2349238a903397ce4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Function to handle entities (switches and scenes)
handle_entities() {
local action=$1
local name=$2
# Get the list of all entities once
local all_entities=$(hass-cli entity list)
# Filter for switches and scenes containing the given name in the entity ID
local switches=$(echo "$all_entities" | grep -E "^switch\.[^ ]*$name[^ ]*" | grep -v "^switch\.automation_")
local scenes=$(echo "$all_entities" | grep -E "^scene\.[^ ]*$name[^ ]*")
if [ -z "$switches" ] && [ -z "$scenes" ]; then
echo "No matching switches or scenes found for '$name'"
exit 1
fi
# Handle switches
if [ -n "$switches" ]; then
echo "Matching switches:"
echo "$switches" | awk '{print $1}'
echo "$switches" | while read -r entity; do
entity_id=$(echo "$entity" | awk '{print $1}')
if [ "$action" = "toggle" ] || [ -z "$action" ]; then
echo "Toggling $entity_id"
hass-cli state toggle "$entity_id"
else
echo "Turning $action $entity_id"
hass-cli state "turn_$action" "$entity_id"
fi
done
fi
# Handle scenes
if [ -n "$scenes" ]; then
echo "Matching scenes:"
echo "$scenes" | awk '{print $1}'
local apt_scene=$(echo "$scenes" | grep "^scene\.apt_" | head -n 1)
local scene_to_activate=""
if [ -n "$apt_scene" ]; then
scene_to_activate=$(echo "$apt_scene" | awk '{print $1}')
echo "Activating prioritized apt_ scene: $scene_to_activate"
elif [ $(echo "$scenes" | wc -l) -eq 1 ]; then
scene_to_activate=$(echo "$scenes" | awk '{print $1}')
echo "Activating single matching scene: $scene_to_activate"
else
echo "Multiple scenes found. Please specify a more precise scene name."
fi
if [ -n "$scene_to_activate" ]; then
hass-cli service call --arguments entity_id=$scene_to_activate scene.turn_on
fi
fi
}
# Function to list entities
list_entities() {
local name=$1
echo "Listing all switches (excluding automation switches) and scenes:"
if [ -z "$name" ]; then
hass-cli entity list | grep -E "^(switch|scene)\.[^ ]*" | grep -v "^switch\.automation_"
else
hass-cli entity list | grep -E "^(switch|scene)\.[^ ]*$name[^ ]*" | grep -v "^switch\.automation_"
fi
}
# Function to print help
print_help() {
echo "Usage: $0 [command] <name>"
echo "Commands:"
echo " on <name> - Turn on switches and activate scenes matching <name>"
echo " off <name> - Turn off switches matching <name>"
echo " toggle <name> - Toggle switches matching <name>"
echo " list [name] - List all switches (excluding automation switches) and scenes, optionally filtered by [name]"
echo " <name> - Toggle switches and turn on scenes matching <name> (default behavior)"
exit 1
}
# Main script
if [ $# -lt 1 ]; then
print_help
fi
if [ "$1" = "list" ]; then
list_entities "$2"
elif [ "$1" = "on" ] || [ "$1" = "off" ] || [ "$1" = "toggle" ]; then
if [ $# -lt 2 ]; then
print_help
fi
handle_entities "$1" "$2"
else
handle_entities "" "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment