Hassock (hassock v0.3.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.

Send any HA WebSocket command and wait for its result. See Hassock.Connection.command/4.

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.

Connection status and next retry time. See Hassock.Connection.status/1.

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

Subscribe to a non-entity HA event type.

Stop receiving the connection's lifecycle messages. See Hassock.Connection.unwatch/1.

Also receive the connection's lifecycle messages. See Hassock.Connection.watch/1.

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, opts \\ [])

Call a Home Assistant service. See Hassock.Connection.call_service/3.

command(conn, type, payload \\ %{}, opts \\ [])

Send any HA WebSocket command and wait for its result. See Hassock.Connection.command/4.

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.

status(conn)

Connection status and next retry time. See Hassock.Connection.status/1.

subscribe_entities(conn, entity_ids \\ nil, opts \\ [])

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

subscribe_events(conn, event_type, opts \\ [])

Subscribe to a non-entity HA event type.

unsubscribe_events(conn, subscription_id)

Cancel a subscription.

unwatch(conn)

Stop receiving the connection's lifecycle messages. See Hassock.Connection.unwatch/1.

watch(conn)

Also receive the connection's lifecycle messages. See Hassock.Connection.watch/1.