Pure functions for parsing and encoding Home Assistant WebSocket frames.
No side effects, no processes — data in, data out. Hassock.Connection
uses these to translate between JSON on the wire and tagged Elixir terms.
HA WebSocket protocol summary
All messages are JSON objects with a type field. After authentication,
client commands carry an integer id for response correlation.
Server → Client
auth_required,auth_ok,auth_invalidresult— response to a command (keyed byid)event— subscription event (state_changed, entities deltas, etc.)pong— response to ping
Client → Server
auth— send access tokensubscribe_events— subscribe to a generic event typesubscribe_entities— efficient targeted entity subscriptionunsubscribe_events— cancel any subscriptioncall_service,get_states,get_services,ping
Summary
Functions
Encode an authentication message.
Encode a call_service command.
Encode a get_services command.
Encode a get_states command.
Encode a ping command.
Encode a subscribe_entities command. Pass nil to subscribe to every
entity, or a list of entity_ids to scope the subscription.
Encode a subscribe_events command for a given event type.
Encode an unsubscribe_events command (works for both subscription types).
Parse a raw JSON string from the HA WebSocket into a tagged term.
True if a new state differs from the prior in a meaningful way (state value
or attributes). Returns true when old is nil. last_updated-only churn
returns false.
Types
@type entities_payload() :: %{ added: %{required(String.t()) => Hassock.EntityState.t()}, changed: %{required(String.t()) => entity_diff()}, removed: [String.t()] }
@type parsed() :: :auth_required | :auth_ok | {:auth_invalid, String.t()} | {:result, integer(), boolean(), term()} | {:state_changed, integer(), Hassock.EntityState.t(), Hassock.EntityState.t() | nil} | {:entities, integer(), entities_payload()} | {:event, integer(), String.t(), map()} | {:states, integer(), [Hassock.EntityState.t()]} | {:services, integer(), %{required(String.t()) => [String.t()]}} | :pong | {:unknown, map()}
Functions
Encode an authentication message.
@spec encode_call_service(integer(), Hassock.ServiceCall.t()) :: String.t()
Encode a call_service command.
Encode a get_services command.
Encode a get_states command.
Encode a ping command.
Encode a subscribe_entities command. Pass nil to subscribe to every
entity, or a list of entity_ids to scope the subscription.
Encode a subscribe_events command for a given event type.
Encode an unsubscribe_events command (works for both subscription types).
Parse a raw JSON string from the HA WebSocket into a tagged term.
@spec state_changed?(Hassock.EntityState.t() | nil, Hassock.EntityState.t()) :: boolean()
True if a new state differs from the prior in a meaningful way (state value
or attributes). Returns true when old is nil. last_updated-only churn
returns false.