#!/bin/bash # Hyprland Monitor Control - Home Assistant Integration Installer # This script helps install the custom integration into Home Assistant set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Default paths DEFAULT_HA_CONFIG="$HOME/.homeassistant" DEFAULT_HA_CONFIG_ALT="/config" INTEGRATION_NAME="hyprmonitors" SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" print_header() { echo -e "${BLUE}================================================${NC}" echo -e "${BLUE} Hyprland Monitor Control - HA Integration${NC}" echo -e "${BLUE}================================================${NC}" echo } print_success() { echo -e "${GREEN}✓ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠ $1${NC}" } print_error() { echo -e "${RED}✗ $1${NC}" } print_info() { echo -e "${BLUE}ℹ $1${NC}" } detect_ha_config() { local config_dir="" # Try common Home Assistant config directories if [[ -d "$DEFAULT_HA_CONFIG" ]]; then config_dir="$DEFAULT_HA_CONFIG" elif [[ -d "$DEFAULT_HA_CONFIG_ALT" ]]; then config_dir="$DEFAULT_HA_CONFIG_ALT" elif [[ -d "/usr/share/hassio/homeassistant" ]]; then config_dir="/usr/share/hassio/homeassistant" elif [[ -d "/data" ]]; then config_dir="/data" fi echo "$config_dir" } check_ha_running() { if command -v systemctl &> /dev/null; then if systemctl is-active --quiet home-assistant || systemctl is-active --quiet homeassistant; then return 0 fi fi # Check for common HA processes if pgrep -f "homeassistant" &> /dev/null || pgrep -f "hass" &> /dev/null; then return 0 fi return 1 } create_backup() { local target_dir="$1" local backup_dir="${target_dir}.backup.$(date +%Y%m%d_%H%M%S)" if [[ -d "$target_dir" ]]; then print_info "Creating backup of existing integration..." cp -r "$target_dir" "$backup_dir" print_success "Backup created: $backup_dir" return 0 fi return 1 } install_integration() { local ha_config="$1" local custom_components_dir="$ha_config/custom_components" local target_dir="$custom_components_dir/$INTEGRATION_NAME" local source_components_dir="$SOURCE_DIR/custom_components/$INTEGRATION_NAME" # Verify source directory exists if [[ ! -d "$source_components_dir" ]]; then print_error "Source integration directory not found: $source_components_dir" exit 1 fi # Create custom_components directory if it doesn't exist mkdir -p "$custom_components_dir" print_success "Custom components directory ready: $custom_components_dir" # Create backup if integration already exists create_backup "$target_dir" # Copy integration files print_info "Installing Hyprland Monitor Control integration..." cp -r "$source_components_dir" "$target_dir" # Set appropriate permissions chmod -R 755 "$target_dir" print_success "Integration installed successfully!" print_info "Installation location: $target_dir" # List installed files echo print_info "Installed files:" find "$target_dir" -type f -name "*.py" -o -name "*.json" -o -name "*.yaml" | sort | while read -r file; do echo " - ${file#$target_dir/}" done } verify_installation() { local ha_config="$1" local target_dir="$ha_config/custom_components/$INTEGRATION_NAME" # Check required files local required_files=( "__init__.py" "manifest.json" "config_flow.py" "const.py" "sensor.py" "switch.py" "strings.json" "services.yaml" ) print_info "Verifying installation..." for file in "${required_files[@]}"; do if [[ -f "$target_dir/$file" ]]; then print_success "Found: $file" else print_error "Missing: $file" return 1 fi done # Check manifest.json structure if command -v python3 &> /dev/null; then if python3 -c "import json; json.load(open('$target_dir/manifest.json'))" 2>/dev/null; then print_success "manifest.json is valid JSON" else print_warning "manifest.json may have syntax errors" fi fi return 0 } check_hyprmonitors_server() { local host="${1:-localhost}" local port="${2:-3000}" print_info "Checking Hyprmonitors server connection..." if command -v curl &> /dev/null; then if curl -s --max-time 5 "http://$host:$port/health" > /dev/null 2>&1; then print_success "Hyprmonitors server is reachable at $host:$port" return 0 else print_warning "Hyprmonitors server not reachable at $host:$port" print_info "Make sure the Rust server is running: cargo run" return 1 fi else print_warning "curl not available, cannot test server connection" return 1 fi } show_next_steps() { echo print_info "Installation complete! Next steps:" echo echo "1. Restart Home Assistant" echo " - If using systemctl: sudo systemctl restart home-assistant" echo " - If using Docker: docker restart homeassistant" echo " - If using HAOS: Settings → System → Restart" echo echo "2. Add the integration:" echo " - Go to Settings → Devices & Services" echo " - Click 'Add Integration'" echo " - Search for 'Hyprland Monitor Control'" echo " - Configure with your server details" echo echo "3. Make sure your Hyprmonitors server is running:" echo " - cd $(dirname "$SOURCE_DIR")" echo " - cargo run" echo echo "4. Optional: Check the examples.yaml file for automation ideas" echo print_success "Enjoy controlling your monitors from Home Assistant! 🖥️" } usage() { echo "Usage: $0 [OPTIONS]" echo echo "Options:" echo " -c, --config-dir DIR Home Assistant configuration directory" echo " -h, --help Show this help message" echo " --check-server HOST:PORT Check if Hyprmonitors server is running" echo " --dry-run Show what would be done without making changes" echo echo "Examples:" echo " $0 # Auto-detect HA config directory" echo " $0 -c /config # Specify config directory" echo " $0 --check-server localhost:3000 # Test server connection" echo " $0 --dry-run # Preview installation" } main() { local ha_config="" local check_server="" local dry_run=false # Parse arguments while [[ $# -gt 0 ]]; do case $1 in -c|--config-dir) ha_config="$2" shift 2 ;; -h|--help) usage exit 0 ;; --check-server) check_server="$2" shift 2 ;; --dry-run) dry_run=true shift ;; *) print_error "Unknown option: $1" usage exit 1 ;; esac done print_header # Handle server check if [[ -n "$check_server" ]]; then IFS=':' read -r host port <<< "$check_server" check_hyprmonitors_server "$host" "$port" exit $? fi # Detect Home Assistant config directory if [[ -z "$ha_config" ]]; then ha_config=$(detect_ha_config) if [[ -z "$ha_config" ]]; then print_error "Could not find Home Assistant configuration directory" print_info "Please specify it with: $0 -c /path/to/homeassistant/config" exit 1 fi print_info "Auto-detected HA config: $ha_config" fi # Verify config directory exists if [[ ! -d "$ha_config" ]]; then print_error "Home Assistant config directory not found: $ha_config" exit 1 fi # Check if HA is running if check_ha_running; then print_warning "Home Assistant appears to be running" print_info "You'll need to restart it after installation" fi if [[ "$dry_run" == true ]]; then print_info "DRY RUN - Would install integration to:" print_info " Source: $SOURCE_DIR/custom_components/$INTEGRATION_NAME" print_info " Target: $ha_config/custom_components/$INTEGRATION_NAME" exit 0 fi # Install the integration install_integration "$ha_config" # Verify installation if verify_installation "$ha_config"; then print_success "Installation verification passed!" else print_error "Installation verification failed!" exit 1 fi # Check server connection check_hyprmonitors_server # Show next steps show_next_steps } # Run main function main "$@"