Skip to main content

Python CLI and library for managing Home Assistant automations

Project description

Home Assistant Automation Management

A Python CLI and library for managing Home Assistant automations programmatically.

Designed for AI Assistants: This toolkit provides clean APIs that AI assistants (like Claude Code, Codex) can use to help users create and manage Home Assistant automations from natural language descriptions.

Architecture

User (natural language) → AI Assistant → Python Toolkit → Home Assistant

Features

  • 🔍 Device discovery and search (supports Chinese and English)
  • 🚀 Create and update automations directly via Home Assistant REST API
  • 📋 List, enable/disable, trigger, and delete automations
  • 🔄 Sync a directory of Python automation scripts to Home Assistant
  • 🎨 Beautiful terminal output with Rich

Installation

pip install ha-automation

Once installed, the ha-automation CLI command will be available globally.

Quick Start

# Initialize a new workspace
ha-automation init
ha-automation init --lang en

# Follow the prompts to configure your HA URL and token,
# then sync your scripts to Home Assistant
ha-automation sync

Configuration

Credentials are stored in .ha-config in your workspace directory (created by ha-automation init), or in ~/.config/ha-automation/config as a fallback.

HA_URL=http://192.168.1.100:8123
HA_TOKEN=your_long_lived_access_token

Getting a Long-Lived Access Token

  1. Go to your Home Assistant profile: http://your-ha-url:8123/profile
  2. Scroll to "Long-Lived Access Tokens"
  3. Click "Create Token", give it a name, and copy it

CLI Usage

Workspace

# Initialize a new automation workspace
ha-automation init [directory]
ha-automation init [directory] --lang [zh|en]

# Test connection to Home Assistant
ha-automation test

Device Discovery

ha-automation discover
ha-automation discover --force          # Force refresh cache

ha-automation devices "motion"
ha-automation devices "走廊"            # Chinese search supported
ha-automation devices --type light
ha-automation devices --area "Living Room"
ha-automation devices --json

Automations

ha-automation list
ha-automation list --state on
ha-automation show automation.my_automation

# Enable/disable/toggle accept entity_id or automation_id
ha-automation enable my_automation_id
ha-automation disable automation.my_automation
ha-automation toggle my_automation_id
ha-automation trigger my_automation_id
ha-automation trigger my_automation_id --skip-condition

ha-automation delete 1234567890
ha-automation delete 1234567890 --force
ha-automation reload

Scripts

# List local automation scripts and their status
ha-automation scripts
ha-automation scripts --directory /path/to/automations

# Run a single script
ha-automation run my_script.py

# Sync all enabled scripts to Home Assistant
ha-automation sync
ha-automation sync --dry-run
ha-automation sync --clean              # Remove orphaned automations

# Enable/disable a script (controls whether it's synced)
ha-automation script-enable my_script.py
ha-automation script-disable my_script.py

Python API

from ha_automation import HAClient, DeviceDiscovery, AutomationManager

client = HAClient()  # Reads credentials automatically
discovery = DeviceDiscovery(client)
manager = AutomationManager(client)

# Discover devices
discovery.discover_all()
lights = discovery.search("living room light")
motion = discovery.search("走廊 motion")

# Create or update an automation
config = {
    "id": "my_automation",
    "alias": "My Automation",
    "trigger": [{"platform": "state", "entity_id": motion[0].entity_id, "to": "on"}],
    "action": [{"service": "light.turn_on", "target": {"entity_id": lights[0].entity_id}}],
    "mode": "single"
}
automation_id, was_created = manager.create_or_update(config)

For AI Assistants

See AGENTS.md for comprehensive documentation including automation patterns, trigger/condition/action reference, and workflow examples.

Requirements

  • Python 3.8+
  • Home Assistant with REST API enabled
  • Long-lived access token

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ha_automation-1.0.4.tar.gz (32.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ha_automation-1.0.4-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

Details for the file ha_automation-1.0.4.tar.gz.

File metadata

  • Download URL: ha_automation-1.0.4.tar.gz
  • Upload date:
  • Size: 32.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ha_automation-1.0.4.tar.gz
Algorithm Hash digest
SHA256 e8b8c71bf2aef40f2c2bfc98d42cd76548951462ff3d4bb3e9ed74cf7c5186ae
MD5 9023f7f1ec080246f1355686b1ea4c7f
BLAKE2b-256 e160684fcaee5a2e9786a1c42a740d02d00c4a2c2a7f9d7ea4c1cf042947fc2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ha_automation-1.0.4.tar.gz:

Publisher: publish-pypi.yml on keli/ha-automation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ha_automation-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: ha_automation-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 33.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ha_automation-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 aafbb088f792e69be4fa16e29512c861b104b6caf4dd21fdf72843c0583a44fb
MD5 04490aab43f8de25fd96f3e21230905a
BLAKE2b-256 5f70f28f16004b7f68b082d73b993930111bf32ff72b5c26cef1e529bd4b5bb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for ha_automation-1.0.4-py3-none-any.whl:

Publisher: publish-pypi.yml on keli/ha-automation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page