WebSocket client for Home Assistant.
The process that calls start_link/1 becomes the controlling process
and receives async messages of the shape {:hassock, conn, payload}:
{:hassock, conn, :connected}— once, afterauth_ok{:hassock, conn, {:event, payload}}— for each inbound HA event{:hassock, conn, {:disconnected, reason}}— on socket loss{:hassock, conn, {:auth_failed, reason}}— onauth_invalid
Event payload variants:
{:state_changed, new_state, old_state}— fromsubscribe_events("state_changed"){:entities, %{added: _, changed: _, removed: _}}— fromsubscribe_entities/2{:other, event_type, raw_event}— from any othersubscribe_events/2type
Use controlling_process/2 to transfer ownership to a different pid.
This module performs the auth handshake and nothing else automatically. It
does not subscribe to anything on its own — callers drive subscriptions
explicitly via subscribe_entities/2, subscribe_events/2, etc.
Summary
Functions
Call a Home Assistant service and wait for the result.
True if authenticated and the underlying socket is connected.
Transfer the controlling process. Only the current controller may transfer ownership.
Fetch all available service domains and their services.
Fetch a one-shot snapshot of all entity states.
Open a Home Assistant WebSocket connection.
Subscribe to entity state updates. Pass nil to subscribe to all entities,
or a list of entity_ids to scope. Strictly preferred over
subscribe_events("state_changed").
Subscribe to a non-entity HA event type (e.g. "automation_triggered").
Don't use this with "state_changed" — use subscribe_entities/2 instead.
Cancel a subscription returned by subscribe_entities/2 or subscribe_events/2.
Functions
@spec call_service(pid() | atom(), Hassock.ServiceCall.t()) :: {:ok, term()} | {:error, term()}
Call a Home Assistant service and wait for the result.
True if authenticated and the underlying socket is connected.
Transfer the controlling process. Only the current controller may transfer ownership.
@spec get_services(pid() | atom()) :: {:ok, %{required(String.t()) => [String.t()]}} | {:error, term()}
Fetch all available service domains and their services.
@spec get_states(pid() | atom()) :: {:ok, [Hassock.EntityState.t()]} | {:error, term()}
Fetch a one-shot snapshot of all entity states.
Open a Home Assistant WebSocket connection.
Options:
:config—%Hassock.Config{}(required):name— register the process under this name (optional)
The calling process becomes the controlling process. If start_link/1
returns {:ok, conn}, the controller will receive a {:hassock, ^conn, :connected} message once authentication completes.
Subscribe to entity state updates. Pass nil to subscribe to all entities,
or a list of entity_ids to scope. Strictly preferred over
subscribe_events("state_changed").
Returns {:ok, subscription_id} on success.
Subscribe to a non-entity HA event type (e.g. "automation_triggered").
Don't use this with "state_changed" — use subscribe_entities/2 instead.
Cancel a subscription returned by subscribe_entities/2 or subscribe_events/2.