HomeElixir.DSL.Generation (HomeElixir v0.1.0)
View SourceMacros 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:
- Module attributes (
@input_boolean,@input_number,@input_select,@alarm_control_panel,@automation_steps) are registered withaccumulate: trueto collect declarations as the module body runs. entity/2,3appends to the relevant entity attribute and, for every declared entity, injectsget_*/set_*helper functions backed byHomeAssistantApiClient.automation/2and the trigger/condition/action macros append ordered steps to@automation_steps.__before_compile__/1reads those attributes, builds the automation intermediate representation, writes the corresponding YAML files, and injects__home_elixir_entities__/0,__home_elixir_automations__/0, andrestart_home_assistant/0into 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
Groups the action declarations of an automation/2 block.
Accepts one or more action forms: service/3,
persistent_notification/2.
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
Sets the alias (name) of the automation currently being declared.
Called automatically by automation/2 — you normally don't need to
call this directly.
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.
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— requiresminandmax.:input_select— requiresoptions.:alarm_control_panel— requiresplatformandcode.
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
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
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
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
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
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
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
A sun trigger, fired on a sun event.
Requires :event (e.g. :sunrise, :sunset).
Example
sun do
event :sunrise
end
A time trigger, fired at a specific time of day.
Requires :at; accepts an optional :weekday.
Example
time do
at "22:00:00"
end
Groups the trigger declarations of an automation/2 block.
Accepts one or more trigger forms: state/1, numeric_state/1,
sun/1, time/1.