AgentSkillsCN

home-assistant

控制 Home Assistant 智能家居设备与自动化场景。适用场景:(1) 用户请求开关灯、切换开关或控制设备;(2) 用户询问传感器、设备或实体的状态;(3) 用户说“打开灯”、“设置温度”、“锁上门”;(4) 用户要求执行某个场景或脚本;(5) 用户询问“有哪些设备已开启?”或“门是否已锁好?”;(6) 用户提及智能家居、物联网或家庭自动化。始终使用 Home Assistant 进行智能家居控制。

SKILL.md
--- frontmatter
name: home-assistant
description: |
  Control Home Assistant smart home devices and automations. Use when:
  (1) User asks to turn on/off lights, switches, or devices,
  (2) User asks about the state of sensors, devices, or entities,
  (3) User says "turn on the lights", "set temperature", "lock the door",
  (4) User asks to run a scene or script,
  (5) User asks "what devices are on?" or "is the door locked?",
  (6) User mentions smart home, IoT, or home automation.
  Always use Home Assistant for smart home control.
author: Claude Code
version: 1.0.0
date: 2025-01-25

Home Assistant Control

Problem

Need to control smart home devices, check sensor states, or run automations via Home Assistant.

Context / Trigger Conditions

  • User asks to control lights, switches, covers, climate, etc.
  • User asks about device states ("is the light on?", "what's the temperature?")
  • User wants to run a scene or script
  • User mentions turning things on/off
  • User asks about smart home devices

Prerequisites

  • The ~/.venvs/claude virtualenv must have requests package installed
  • Environment variables HOME_ASSISTANT_URL and HOME_ASSISTANT_TOKEN must be set in the venv activation script

Solution

Script Location

code
/home/wizard/code/infra/.claude/home-assistant.py

Execution Pattern (CRITICAL)

Always activate the venv to get environment variables, then run via /remote skill:

bash
/remote source ~/.venvs/claude/bin/activate && cd /home/wizard/code/infra && python .claude/home-assistant.py [command] [options]

Available Commands

List Entities

bash
# List all entities
python .claude/home-assistant.py list

# List by domain
python .claude/home-assistant.py list --domain light
python .claude/home-assistant.py list --domain switch
python .claude/home-assistant.py list --domain sensor
python .claude/home-assistant.py list --domain climate
python .claude/home-assistant.py list --domain cover

# JSON output
python .claude/home-assistant.py list --json

Search Entities

bash
# Search by name or ID
python .claude/home-assistant.py search "living room"
python .claude/home-assistant.py search "temperature"
python .claude/home-assistant.py search "door"

Get Entity State

bash
python .claude/home-assistant.py state light.living_room
python .claude/home-assistant.py state sensor.temperature
python .claude/home-assistant.py state --json light.living_room

Control Entities

bash
# Turn on/off
python .claude/home-assistant.py on light.living_room
python .claude/home-assistant.py off switch.tv
python .claude/home-assistant.py toggle light.bedroom

# Set values
python .claude/home-assistant.py set light.living_room 75        # brightness %
python .claude/home-assistant.py set climate.thermostat 22       # temperature
python .claude/home-assistant.py set cover.blinds 50             # position %
python .claude/home-assistant.py set input_number.volume 80      # numeric value
python .claude/home-assistant.py set input_boolean.away_mode on  # boolean
python .claude/home-assistant.py set input_select.mode "Night"   # select option

Run Scenes and Scripts

bash
# Activate a scene
python .claude/home-assistant.py scene movie_night
python .claude/home-assistant.py scene scene.good_morning

# Run a script
python .claude/home-assistant.py script bedtime_routine
python .claude/home-assistant.py script script.welcome_home

Call Any Service

bash
# Generic service call
python .claude/home-assistant.py service light turn_on --entity light.kitchen --data '{"brightness": 255}'
python .claude/home-assistant.py service climate set_hvac_mode --entity climate.living_room --data '{"hvac_mode": "heat"}'
python .claude/home-assistant.py service media_player play_media --entity media_player.tv --data '{"media_content_id": "...", "media_content_type": "video"}'

List Services

bash
# List all available services
python .claude/home-assistant.py services

# Filter by domain
python .claude/home-assistant.py services --domain light
python .claude/home-assistant.py services --domain climate

Send Notifications

bash
python .claude/home-assistant.py notify "Door left open!"
python .claude/home-assistant.py notify "Motion detected" --title "Security Alert"
python .claude/home-assistant.py notify "Hello" --target notify.mobile_app

Complete Example

To turn on the living room light:

bash
/remote source ~/.venvs/claude/bin/activate && cd /home/wizard/code/infra && python .claude/home-assistant.py on light.living_room

Common Entity Domains

DomainDescriptionCommon Actions
lightLightson, off, toggle, set brightness
switchSwitcheson, off, toggle
sensorSensorsstate (read-only)
binary_sensorBinary sensorsstate (read-only)
climateThermostatsset temperature, set mode
coverBlinds/coversopen, close, set position
lockLockslock, unlock
media_playerMedia devicesplay, pause, volume
input_booleanHelper toggleson, off
input_numberHelper numbersset value
input_selectHelper dropdownsselect option
scriptScriptsrun
sceneScenesactivate
automationAutomationstrigger, on, off

Verification

  • Commands print confirmation message on success
  • Use state command to verify entity changed
  • Exit code 0 = success, 1 = error

Common Errors

ErrorCauseFix
HOME_ASSISTANT_URL and HOME_ASSISTANT_TOKEN must be setDidn't source venv activationUse source ~/.venvs/claude/bin/activate && python ...
404 Not FoundEntity doesn't existUse search command to find correct entity ID
401 UnauthorizedToken invalid/expiredGenerate new long-lived token in HA
Connection refusedHA not reachableCheck URL and network connectivity

Notes

  1. Entity IDs are case-sensitive - use search to find exact IDs
  2. Token must have sufficient permissions - ensure token has access to all entities
  3. Some entities require specific data - use services command to see required fields
  4. HA URL: https://ha-london.viktorbarzin.me