Skip to main content
The Home Assistant WebSocket API provides a fast, real-time bidirectional communication channel for interacting with Home Assistant. It’s the preferred method for frontends and applications that need live updates.

Overview

The WebSocket API allows you to:
  • Subscribe to state changes and events in real-time
  • Call services
  • Query states and configuration
  • Execute commands with responses
  • Receive push notifications for updates
Source: homeassistant/components/websocket_api/__init__.py

Connection

Establishing a Connection

Connect to the WebSocket endpoint:

Authentication Flow

  1. Upon connection, you receive an auth_required message:
  1. Send an authentication message with your long-lived access token:
  1. If successful, you’ll receive:
If authentication fails:
Create long-lived access tokens in the Home Assistant UI under your user profile.

Message Format

Command Messages

All commands sent to Home Assistant follow this format:
  • id: Unique integer to match responses (required)
  • type: Command type (required)
  • Additional fields depend on the command

Result Messages

Home Assistant responds with either a success or error result: Success:
Error:

Error Codes

From homeassistant/components/websocket_api/const.py:
  • ERR_UNKNOWN_COMMAND: Command is not recognized
  • ERR_INVALID_FORMAT: Message format is invalid
  • ERR_NOT_FOUND: Requested resource not found
  • ERR_NOT_ALLOWED: Permission denied
  • ERR_HOME_ASSISTANT_ERROR: Internal Home Assistant error
  • ERR_UNKNOWN_ERROR: Unknown error occurred
  • ERR_UNAUTHORIZED: Authentication required
  • ERR_TIMEOUT: Command timed out
  • ERR_NOT_SUPPORTED: Feature not supported
  • ERR_TEMPLATE_ERROR: Template rendering error
  • ERR_SERVICE_VALIDATION_ERROR: Service call validation failed

Common Commands

Subscribe to Events

Subscribe to specific event types:
Subscribe to all events (omit event_type):
Event Messages:

Unsubscribe from Events

Get States

Get all entity states:
Response:

Get Config

Retrieve Home Assistant configuration:

Call Service

Execute a service:
With response data (Home Assistant 2024.4+):
Response:

Ping/Pong

Keep connection alive:
Response:

Registering Custom Commands

Integrations can register custom WebSocket commands using the @websocket_command decorator:
Register during integration setup:

Async Response Commands

For async operations, use the @async_response decorator:

Permission Requirements

Require admin permissions:

Current Connection

Access the current WebSocket connection in async code:

Message Helpers

From homeassistant/components/websocket_api/messages.py:

Connection Management

The ActiveConnection class manages WebSocket connections:

Best Practices

Handle Reconnection

Implement automatic reconnection with exponential backoff

Validate Messages

Always validate command parameters with voluptuous schemas

Use Callbacks

Use @callback for sync handlers to avoid blocking the event loop

Clean Up Subscriptions

Unsubscribe from events when no longer needed

Full Example

See Also

REST API

HTTP-based API for polling operations

Event System

Understanding Home Assistant events

Service Calls

Learn about calling services

Authentication

Authentication and permissions system