HomeElixir.DSL.Generation (HomeElixir v0.1.0)

View Source

Macros for declaring Home Assistant entities and automations.

This module backs use HomeElixir.DSL — importing HomeElixir.DSL also imports every macro documented here (entity/2,3, automation/2, trigger/1, condition/1, action/1, and the trigger/condition/action forms nested inside them).

How it works

When a module uses this module:

  1. Module attributes (@input_boolean, @input_number, @input_select, @alarm_control_panel, @automation_steps) are registered with accumulate: true to collect declarations as the module body runs.
  2. entity/2,3 appends to the relevant entity attribute and, for every declared entity, injects get_*/set_* helper functions backed by HomeAssistantApiClient.
  3. automation/2 and the trigger/condition/action macros append ordered steps to @automation_steps.
  4. __before_compile__/1 reads those attributes, builds the automation intermediate representation, writes the corresponding YAML files, and injects __home_elixir_entities__/0, __home_elixir_automations__/0, and restart_home_assistant/0 into the module.

Generation happens automatically as soon as the module finishes compiling — there is nothing else to call.

Supported forms

Entities: :input_boolean, :input_number, :input_select, :alarm_control_panel.

Triggers: state/1, numeric_state/1, sun/1, time/1.

Conditions: state_condition/1, numeric_state_condition/1, logical_condition/2 (:and/:or).

Actions: service/3, persistent_notification/2.

See the project README for a complete tour with examples of each form.

Summary

Functions

Groups the action declarations of an automation/2 block.

Declares an automation.

Sets the alias (name) of the automation currently being declared.

Groups the condition declarations of an automation/2 block.

Declares a Home Assistant entity.

Wraps one or more conditions so they're combined with :and/:or instead of Home Assistant's default :and.

A numeric_state trigger, fired when a numeric entity crosses a threshold.

A numeric_state_condition, true when a numeric entity is within range.

A persistent_notification action, showing a notification in the Home Assistant UI.

A state trigger, fired when an entity's state changes.

A state_condition, true when an entity currently holds a given state.

A sun trigger, fired on a sun event.

A time trigger, fired at a specific time of day.

Groups the trigger declarations of an automation/2 block.

Functions

action(list)

(macro)

Groups the action declarations of an automation/2 block.

Accepts one or more action forms: service/3, persistent_notification/2.

automation(name, list)

(macro)

Declares an automation.

name becomes the automation's Home Assistant alias. The do block holds trigger/1, condition/1, and action/1 sections.

Example

automation "notify when test switch turns on" do
  trigger do
    state do
      affected_entities [{:input_boolean, "test_switch"}]
      to true
    end
  end

  action do
    persistent_notification "create" do
      message "The test switch is on"
    end
  end
end

automation_alias(name)

(macro)

Sets the alias (name) of the automation currently being declared.

Called automatically by automation/2 — you normally don't need to call this directly.

condition(list)

(macro)

Groups the condition declarations of an automation/2 block.

Accepts one or more condition forms: state_condition/1, numeric_state_condition/1, logical_condition/2.

entity(type, name, list)

(macro)

Declares a Home Assistant entity.

type is one of :input_boolean, :input_number, :input_select, or :alarm_control_panel; name is the entity's Home Assistant id. The do block accepts keyword-style attributes and its required keys depend on the entity type:

  • :input_boolean — no required keys (name, initial, icon, ...).
  • :input_number — requires min and max.
  • :input_select — requires options.
  • :alarm_control_panel — requires platform and code.

Declaring an entity also injects get_*/set_* helper functions into the enclosing module, named after the entity type and id, e.g. get_input_boolean_test_switch/0 and set_input_boolean_test_switch/1.

Examples

entity :input_boolean, "presence" do
  name "Presence"
  initial false
end

entity :input_number, "temperature_limit" do
  min 10
  max 30
  initial 20
end

entity :input_select, "cover_mode" do
  options ["open", "close"]
  initial "open"
end

entity :alarm_control_panel, "alarm" do
  platform :manual
  code "1234"
end

logical_condition(type, list)

(macro)

Wraps one or more conditions so they're combined with :and/:or instead of Home Assistant's default :and.

Example

logical_condition :or do
  state_condition do
    affected_entities [{:alarm_control_panel, "alarm"}]
    state_value "armed_home"
  end

  state_condition do
    affected_entities [{:alarm_control_panel, "alarm"}]
    state_value "armed_away"
  end
end

numeric_state(list)

(macro)

A numeric_state trigger, fired when a numeric entity crosses a threshold.

Requires :affected_entities; accepts optional :above and :below.

Example

numeric_state do
  affected_entities [{:input_number, "temperature_limit"}]
  above 20
end

numeric_state_condition(list)

(macro)

A numeric_state_condition, true when a numeric entity is within range.

Requires :affected_entities; accepts optional :above and :below.

Example

numeric_state_condition do
  affected_entities [{:input_number, "temperature_limit"}]
  above 20
end

persistent_notification(action, list)

(macro)

A persistent_notification action, showing a notification in the Home Assistant UI.

action is the notification service action (typically "create"). The do block requires :message and accepts an optional :title.

Example

persistent_notification "create" do
  message "The test switch is on"
end

state(list)

(macro)

A state trigger, fired when an entity's state changes.

Requires :affected_entities (a {domain, name} tuple or list of tuples); accepts optional :to and :from.

Example

state do
  affected_entities [{:input_boolean, "presence"}]
  to true
end

state_condition(list)

(macro)

A state_condition, true when an entity currently holds a given state.

Requires :affected_entities and :state_value.

Example

state_condition do
  affected_entities [{:alarm_control_panel, "alarm"}]
  state_value "armed_home"
end

sun(list)

(macro)

A sun trigger, fired on a sun event.

Requires :event (e.g. :sunrise, :sunset).

Example

sun do
  event :sunrise
end

time(list)

(macro)

A time trigger, fired at a specific time of day.

Requires :at; accepts an optional :weekday.

Example

time do
  at "22:00:00"
end

trigger(list)

(macro)

Groups the trigger declarations of an automation/2 block.

Accepts one or more trigger forms: state/1, numeric_state/1, sun/1, time/1.