Hassock (hassock v0.2.0)

Copy Markdown View Source

Home Assistant WebSocket client for Elixir.

Two layers, opt-in:

  • Hassock.Connection — the WebSocket itself. Always required.
  • Hassock.Cache — optional ETS-backed full entity cache, kept in sync via HA's subscribe_entities command.

Both follow the controlling-process pattern (à la :gen_tcp, Circuits.UART): the process that calls start_link/1 is the default recipient of async messages. Use controlling_process/2 to hand off.

Bare connection

{:ok, conn} =
  Hassock.connect(
    config: %Hassock.Config{url: "http://homeassistant.local:8123", token: "..."}
  )

receive do
  {:hassock, ^conn, :connected} -> :ok
end

{:ok, sub} = Hassock.subscribe_entities(conn, ["light.kitchen"])
# handle {:hassock, ^conn, {:event, {:entities, _}}} in your handle_info/2

With entity cache

{:ok, conn} = Hassock.connect(config: config)
{:ok, cache} = Hassock.Cache.start_link(connection: conn)

receive do
  {:hassock_cache, ^cache, :ready} -> :ok
end

Hassock.cached_state(cache, "light.kitchen")

Summary

Functions

All cached entities under a domain prefix (e.g. "light").

Look up a cached entity by id (requires a Hassock.Cache).

All cached entities.

Call a Home Assistant service.

Open a WebSocket connection. See Hassock.Connection.start_link/1.

True if the connection is authenticated and the socket is open.

Transfer connection ownership.

Fetch all available service domains and their services.

One-shot fetch of all entity states.

Subscribe to entity state updates. Pass nil for all entities.

Subscribe to a non-entity HA event type.

Functions

cached_domain(cache, domain)

All cached entities under a domain prefix (e.g. "light").

cached_state(cache, entity_id)

Look up a cached entity by id (requires a Hassock.Cache).

cached_states(cache)

All cached entities.

call_service(conn, service_call)

Call a Home Assistant service.

connect(opts)

Open a WebSocket connection. See Hassock.Connection.start_link/1.

connected?(conn)

True if the connection is authenticated and the socket is open.

controlling_process(conn, pid)

Transfer connection ownership.

get_services(conn)

Fetch all available service domains and their services.

get_states(conn)

One-shot fetch of all entity states.

subscribe_entities(conn, entity_ids \\ nil)

Subscribe to entity state updates. Pass nil for all entities.

subscribe_events(conn, event_type)

Subscribe to a non-entity HA event type.

unsubscribe_events(conn, subscription_id)

Cancel a subscription.