- Add complete homeassistant skill source to skills/ directory - Includes all scripts, references, and automation templates - Matches format of other skills in repository
4.5 KiB
Home Assistant API Reference
REST API
Authentication
All API requests require a Long-Lived Access Token passed in the Authorization header:
Authorization: Bearer YOUR_TOKEN_HERE
Obtain tokens from: http://YOUR_HA_URL:8123/profile
Base URL
http://YOUR_HA_URL:8123/api/
Key Endpoints
Get All Entity States
GET /api/states
Returns array of all entities with their current states, attributes, and timestamps.
Get Specific Entity State
GET /api/states/<entity_id>
Example: /api/states/light.living_room
Returns single entity object with state, attributes, last_changed, last_updated.
Call a Service
POST /api/services/<domain>/<service>
Example: /api/services/light/turn_on
Request body (optional):
{
"entity_id": "light.living_room",
"brightness": 255,
"color_name": "blue"
}
Returns array of entity states that were changed by the service call.
List All Services
GET /api/services
Returns object with domains as keys, each containing available services with descriptions and field definitions.
Get Configuration
GET /api/config
Returns Home Assistant configuration including version, location, unit system, time zone, etc.
Get Error Log
GET /api/error_log
Returns text content of the error log.
Response Codes
200- Success (existing resource)201- Created (new resource)400- Bad Request401- Unauthorized404- Not Found405- Method Not Allowed
WebSocket API
Connection
Connect to: ws://YOUR_HA_URL:8123/api/websocket
Authentication Flow
- Client connects
- Server sends
auth_requiredmessage:
{
"type": "auth_required",
"ha_version": "2024.1.0"
}
- Client sends
authmessage:
{
"type": "auth",
"access_token": "YOUR_TOKEN_HERE"
}
- Server responds with either:
{"type": "auth_ok"}
or
{
"type": "auth_invalid",
"message": "Invalid access token"
}
Message Format
All messages use JSON. Command phase messages require an id field for request/response correlation.
Common Commands
Get All States
{
"id": 1,
"type": "get_states"
}
Response:
{
"id": 1,
"type": "result",
"success": true,
"result": [
{
"entity_id": "light.living_room",
"state": "on",
"attributes": {...},
"last_changed": "2024-01-15T10:30:00+00:00",
"last_updated": "2024-01-15T10:30:00+00:00"
}
]
}
Call Service
{
"id": 2,
"type": "call_service",
"domain": "light",
"service": "turn_on",
"target": {
"entity_id": "light.kitchen"
},
"service_data": {
"brightness": 255,
"color_name": "warm_white"
},
"return_response": true
}
Response:
{
"id": 2,
"type": "result",
"success": true,
"result": {
"context": {
"id": "326ef27d19415c60c492fe330945f954",
"parent_id": null,
"user_id": "31ddb597e03147118cf8d2f8fbea5553"
},
"response": null
}
}
Subscribe to Events
Subscribe to state changes:
{
"id": 3,
"type": "subscribe_events",
"event_type": "state_changed"
}
Server confirms subscription:
{
"id": 3,
"type": "result",
"success": true
}
Then sends events as they occur:
{
"id": 3,
"type": "event",
"event": {
"event_type": "state_changed",
"data": {
"entity_id": "sensor.temperature",
"old_state": {...},
"new_state": {...}
}
}
}
Unsubscribe from Events
{
"id": 4,
"type": "unsubscribe_events",
"subscription": 3
}
Error Responses
{
"id": 5,
"type": "result",
"success": false,
"error": {
"code": "invalid_format",
"message": "Message incorrectly formatted"
}
}
Common Entity Domains
light- Lightsswitch- Switchessensor- Sensors (read-only)binary_sensor- Binary sensors (on/off, open/closed)climate- Climate control (thermostats, AC)cover- Covers (blinds, garage doors)fan- Fansmedia_player- Media playerscamera- Cameraslock- Door locksalarm_control_panel- Alarm systemsvacuum- Vacuum cleanersautomation- Automations themselvesscript- Scriptsscene- Scenes
Common Service Patterns
Most domains support these services:
turn_on- Turn entity onturn_off- Turn entity offtoggle- Toggle entity state
Domain-specific services may include additional parameters like brightness, color_temp, position, etc.