View Source Grizzly.VirtualDevices.Device behaviour (grizzly v4.0.0)

Behaviour for implementing virtual device specifics

Link to this section Summary

Types

t()

A module that implements this behaviour

Callbacks

Handle a Z-Wave command

Handle messages outside of the direct Z-Wave command processing

Initialize the device

Link to this section Types

@type t() :: module()

A module that implements this behaviour

Link to this section Callbacks

Link to this callback

handle_command(t, state)

View Source
@callback handle_command(Grizzly.ZWave.Command.t(), state :: term()) ::
  {:reply, Grizzly.ZWave.Command.t() | :ack_response, state :: term()}
  | {:noreply, state :: term()}
  | {:notify, Grizzly.ZWave.Command.t(), state :: term()}

Handle a Z-Wave command

When handling a command you can reply, notify, or do nothing.

In Z-Wave if your device does not understand the command sent it ignores the command. For this case you'd return {:noreply, state}.

When a command is received and you want to reply back to the sender, this is when you will use :reply. Normally for a "get" kinda of command you will reply back with a "report" command within that same command class. When a "set" kind of command comes in your will respond with :ack_response

Often times, if your device reports changes that have been made due to handling a command, you can return :notify. This happens is "set" operations and the appropriate :ack_response will be sent to the issuer of the "set" command for you.

Link to this callback

handle_info(msg, state)

View Source (optional)
@callback handle_info(msg :: term(), state :: term()) ::
  {:noreply, state :: term()}
  | {:notify, Grizzly.ZWave.Command.t(), state :: term()}

Handle messages outside of the direct Z-Wave command processing

An example is if you implement a multilevel sensor for a temperature sensor you could use this callback to handle messages from the sensor and notify the Z-Wave network about the change in temperature reading.any()

def init() do
  current_temp = read_temp()
  listen_to_temperature_changes()

  {:ok, %{temp: current_temp}}
end

def handle_info({:temp, new_temp}, state) do
  if state.current_temp == new_temp do
    {:noreply, state}
  else
    {:ok, report} = Grizzly.ZWave.Commands.MultiLevelSensorReport.new(
      sensor_type: :temperature,
      scale: 1,
      value: new_temp
    )
    {:notify, report, %{state | temp: new_temp}}
end
@callback init(args :: term()) ::
  {:ok, state :: term(), Grizzly.ZWave.DeviceClass.t()} | {:error, term()}

Initialize the device