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'ssubscribe_entitiescommand.
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/2With 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.
Cancel a subscription.
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.
Cancel a subscription.