#!/bin/bash ################################################################################ # Home Assistant Chore Cleanup Script (Fast Sed Version) # Removes unused chore-related entities from YAML configuration files ################################################################################ # Configuration CONFIG_DIR="/config" BACKUP_TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_DIR="$CONFIG_DIR/backups/cleanup_$BACKUP_TIMESTAMP" # Color codes GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' NC='\033[0m' echo "======================================================================" echo "Home Assistant Chore Cleanup Script (Fast)" echo "======================================================================" echo "" # Create backup directory mkdir -p "$BACKUP_DIR" echo -e "${GREEN}✓${NC} Created backup: $BACKUP_DIR" echo "" # Entities to remove (regex patterns) BOOLEANS=( "task_lou_clean_desk_pending" "task_jess_clean_desk_pending" "task_william_clean_desk_pending" "task_xander_clean_desk_pending" "task_bella_clean_desk_pending" "task_lou_clean_room_pending" "task_jess_clean_room_pending" "task_william_clean_room_pending" "task_xander_clean_room_pending" "task_bella_clean_room_pending" "task_tidy_lounge_pending" "task_vacuum_room_pending" "task_get_school_ready_pending" "task_clean_desks_done_this_week" "task_clean_rooms_done_this_week" "task_tidy_lounge_done_this_week" "task_tidy_kitchen_done_today" "task_clean_dining_table_done_today" "task_vacuum_room_done_today" ) SCRIPTS=( "complete_task_dishwasher_unload" "complete_task_washing_machine_unload" "complete_task_dryer_unload" "complete_task_bins_out" "complete_task_bins_in" "complete_task_kitty_litter_clean" "complete_task_kitty_litter_change" "complete_tidy_lounge" "complete_vacuum_room" "complete_get_school_ready" ) remove_entity_block() { local file="$1" local entity="$2" # Use awk to remove the entity block awk -v entity="$entity" ' BEGIN { in_block=0; indent="" } { # Check if this line starts our target entity if ($0 ~ "^[[:space:]]*" entity ":") { in_block=1 # Capture indentation match($0, /^[[:space:]]*/) indent = substr($0, 1, RLENGTH) next } # If in block, check if we hit next entity at same level if (in_block) { if ($0 ~ "^" indent "[^[:space:]]" && $0 !~ "^" indent "[[:space:]]") { in_block=0 } else { next } } print } ' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file" } process_file() { local file="$1" local entity_type="$2" local -n entities=$3 if [ ! -f "$file" ]; then echo -e "${YELLOW}⚠${NC} File not found: $(basename "$file")" return fi echo -e "${BLUE}ℹ${NC} Processing $(basename "$file")..." # Backup cp "$file" "$BACKUP_DIR/$(basename "$file")" echo " ✓ Backed up" local count=0 for entity in "${entities[@]}"; do if grep -q "^[[:space:]]*${entity}:" "$file"; then remove_entity_block "$file" "$entity" ((count++)) echo " ✓ Removed: $entity" fi done if [ $count -gt 0 ]; then echo -e "${GREEN} ✓ Removed $count ${entity_type} entries${NC}" else echo " No items to remove" fi echo "" } # Process input_boolean files for pattern in "input_boolean.yaml" "input_booleans.yaml"; do if [ -f "$CONFIG_DIR/$pattern" ]; then process_file "$CONFIG_DIR/$pattern" "input_boolean" BOOLEANS fi done # Process script files for pattern in "scripts.yaml" "script.yaml"; do if [ -f "$CONFIG_DIR/$pattern" ]; then process_file "$CONFIG_DIR/$pattern" "script" SCRIPTS fi done # Create restore script cat > "$BACKUP_DIR/restore.sh" << EOF #!/bin/bash echo "Restoring from backup..." cp "$BACKUP_DIR"/*.yaml "$CONFIG_DIR/" echo "Files restored! Restart Home Assistant: ha core restart" EOF chmod +x "$BACKUP_DIR/restore.sh" echo "======================================================================" echo "CLEANUP COMPLETE" echo "======================================================================" echo "" echo "Backups: $BACKUP_DIR" echo "" echo "NEXT STEPS:" echo "1. Check config: ha core check" echo "2. Restart HA: ha core restart" echo "3. If issues: bash $BACKUP_DIR/restore.sh" echo "" echo "======================================================================"