Hassock.RegistryCache (hassock v0.3.0)

Copy Markdown View Source

Optional ETS-backed cache of Home Assistant's floor, area, device, entity and label registries.

On every :connected (the first and each reconnect) it subscribes to the five *_registry_updated events (once; the connection restores them after a reconnect) and lists all five registries. When an update event arrives it re-lists that registry, debounced (default 500 ms) so a burst of updates, such as an integration adding fifty entities, costs one list. Failed loads and re-lists are retried while connected.

It doesn't take ownership of the connection (it uses Hassock.Connection.watch/1), so it runs beside a Hassock.Cache or any other controller. Lists run in a linked task, so the cache stays responsive (and stops promptly) while a large registry loads.

The controlling process (default: the caller of start_link/1) receives:

  • {:hassock_registry, cache, :ready} - after all five registries are loaded, again after each reconnect
  • {:hassock_registry, cache, {:changed, kind}} - after a registry was re-listed because it changed

Reads (list/2, get/3) go to ETS directly, with no GenServer call. Data is kept while disconnected.

Summary

Functions

Returns a specification to start this module under a supervisor.

Transfer cache ownership. Only the current controller may transfer, or anyone once the controller has exited.

One cached item by kind and id, or nil.

All cached items of one registry (Hassock.Registries.kinds/0), sorted by id.

Start a registry cache for a connection.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

controlling_process(cache, new_pid)

@spec controlling_process(GenServer.server(), pid()) ::
  :ok | {:error, :not_owner | :not_alive}

Transfer cache ownership. Only the current controller may transfer, or anyone once the controller has exited.

get(cache, kind, id)

One cached item by kind and id, or nil.

list(cache, kind)

All cached items of one registry (Hassock.Registries.kinds/0), sorted by id.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Start a registry cache for a connection.

Options:

  • :connection - pid or registered name of a Hassock.Connection (required)
  • :controlling_pid - receiver of registry messages (default: caller)
  • :name - register the cache under this name (optional)
  • :debounce - ms to wait after an update event before re-listing (default 500)
  • :retry - ms before retrying a failed load or re-list (default 5000)