Homex.Entity.Switch behaviour (homex v0.1.0)
View SourceA switch entity for Homex
Implements a Homex.Entity. See module for available callbacks.
Home Assistant docs: https://www.home-assistant.io/integrations/switch.mqtt
Options
:name(String.t/0) - Required. the name of the entity:update_interval- the interval in milliseconds in whichhandle_timer/1get's called. Can also be:neverto disable the timer callback The default value is:never.:retain(boolean/0) - if the last state should be retained The default value istrue.
Overridable Functions
The following functions can be overridden in your entity:
handle_init/1- FromHomex.Entityhandle_timer/1- FromHomex.Entityhandle_on/1- FromHomex.Entity.Switchhandle_off/1- FromHomex.Entity.Switch
Default Implementations
All overridable functions have safe default implementations that return the entity unchanged. You only need to override the functions you want to customize.
Example
defmodule MySwitch do
use Homex.Entity.Switch, name: "my-switch"
def handle_on(entity) do
IO.puts("Switch turned on")
entity
end
def handle_off(entity) do
IO.puts("Switch turned off")
entity
end
end
Summary
Callbacks
Gets called when the command topic receieves an off_payload
Gets called when the command topic receieves an on_payload
Sets the switch state to off
Sets the switch state to on
Callbacks
@callback handle_off(entity :: Homex.Entity.t()) :: entity :: Homex.Entity.t() | {:error, reason :: term()}
Gets called when the command topic receieves an off_payload
@callback handle_on(entity :: Homex.Entity.t()) :: entity :: Homex.Entity.t() | {:error, reason :: term()}
Gets called when the command topic receieves an on_payload
@callback set_off(entity :: Homex.Entity.t()) :: entity :: Homex.Entity.t()
Sets the switch state to off
@callback set_on(entity :: Homex.Entity.t()) :: entity :: Homex.Entity.t()
Sets the switch state to on