Class: OpenHAB::DSL::Rules::Builder

Inherits:
Object
  • Object
show all
Includes:
Core::EntityLookup, Terse
Defined in:
lib/openhab/dsl/rules/builder.rb

Overview

A rules builder allows you to create openHAB rules.

Note that all methods on this module are also availabe directly on OpenHAB::DSL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Core::EntityLookup

#method_missing

Methods included from Terse

#changed, #channel, #channel_linked, #channel_unlinked, #cron, #every, #item_added, #item_removed, #item_updated, #on_start, #received_command, #thing_added, #thing_removed, #thing_updated, #updated

Constructor Details

#initialize(provider) ⇒ Builder

Returns a new instance of Builder.



33
34
35
# File 'lib/openhab/dsl/rules/builder.rb', line 33

def initialize(provider)
  @provider = Core::Rules::Provider.current(provider)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OpenHAB::Core::EntityLookup

Instance Attribute Details

#providerorg.openhab.core.automation.RuleProvider (readonly)

Returns:

  • (org.openhab.core.automation.RuleProvider)


31
32
33
# File 'lib/openhab/dsl/rules/builder.rb', line 31

def provider
  @provider
end

Instance Method Details

#rule(name = nil, id: nil, script: nil, binding: nil) {|rule| ... } ⇒ Core::Rules::Rule?

Create a new rule

The rule must have at least one trigger and one execution block. To create a “script” without any triggers, use script.

Examples:

require "openhab/dsl"

rule "name" do
  <one or more triggers>
  <one or more execution blocks>
  <zero or more guards>
end

Parameters:

  • name (String) (defaults to: nil)

    The rule name

Yields:

Yield Parameters:

  • rule (Rules::BuilderDSL)

    Optional parameter to access the rule configuration from within execution blocks and guards.

Returns:

Raises:

  • (ArgumentError)

See Also:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/openhab/dsl/rules/builder.rb', line 61

def rule(name = nil, id: nil, script: nil, binding: nil, &block)
  raise ArgumentError, "Block is required" unless block

  id ||= NameInference.infer_rule_id_from_block(block)
  script ||= block.source rescue nil # rubocop:disable Style/RescueModifier

  builder = nil

  ThreadLocal.thread_local(openhab_rule_type: "rule", openhab_rule_uid: id) do
    builder = BuilderDSL.new(binding || block.binding)
    builder.uid(id)
    builder.instance_exec(builder, &block)
    builder.guard = Guard.new(run_context: builder.caller,
                              only_if: builder.only_if,
                              not_if: builder.not_if)

    name ||= NameInference.infer_rule_name(builder)
    name ||= id

    builder.name(name)
    logger.trace { builder.inspect }
    builder.build(provider, script)
  end
end

#scene(name = nil, description: nil, id: nil, tag: nil, tags: nil, script: nil) { ... } ⇒ Core::Rules::Rule

Create a new scene

A scene is a rule with no triggers. It can be called by various other actions, such as the Run Rules action.

Parameters:

Yields:

  • Block executed when the script is executed.

Returns:

Raises:

  • (ArgumentError)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/openhab/dsl/rules/builder.rb', line 164

def scene(name = nil, description: nil, id: nil, tag: nil, tags: nil, script: nil, &block)
  raise ArgumentError, "Block is required" unless block

  id ||= NameInference.infer_rule_id_from_block(block)
  name ||= id
  script ||= block.source rescue nil # rubocop:disable Style/RescueModifier

  builder = nil
  ThreadLocal.thread_local(openhab_rule_type: "script", openhab_rule_uid: id) do
    builder = BuilderDSL.new(block.binding)
    builder.uid(id)
    builder.tags("Scene", *Array.wrap(tag), *Array.wrap(tags))
    builder.name(name)
    builder.description(description)
    builder.script(&block)
    logger.trace { builder.inspect }
    builder.build(provider, script)
  end
end

#script(name = nil, description: nil, id: nil, tag: nil, tags: nil, script: nil) { ... } ⇒ Core::Rules::Rule

Create a new script

A script is a rule with no triggers. It can be called by various other actions, such as the Run Rules action, or by calling Core::Rules::Rule#trigger.

Scripts can be executed with some additional context, similar to method parameters (see Core::Rules::Rule#trigger). The context can be accessed from within the script’s execution block as a “local” variable.

Examples:

A simple script

# return the script object into a variable
door_check = script "Check all doors", id: "door_check", tags: :security do
  open_doors = gDoors.members.select(&:open?).map(&:label).join(", ")
  notify("The following doors are open: #{open_doors}") unless open_doors.empty?
end

# run is an alias of trigger
door_check.run

A script with context

# This script expects to be called with `message` as context/parameter
DESTINATION_EMAIL = "[email protected]"
script "Send Notifications", id: "send_alert" do
  notify(message)
  things["mail:smtp:local"].send_mail(DESTINATION_EMAIL, "OpenHAB Alert", message)
end

rules.scripts["send_alert"].run(message: "The door is open!")

Parameters:

Yields:

  • Block executed when the script is executed.

Returns:

Raises:

  • (ArgumentError)

See Also:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/openhab/dsl/rules/builder.rb', line 128

def script(name = nil, description: nil, id: nil, tag: nil, tags: nil, script: nil, &block)
  raise ArgumentError, "Block is required" unless block

  id ||= NameInference.infer_rule_id_from_block(block)
  name ||= id
  script ||= block.source rescue nil # rubocop:disable Style/RescueModifier

  builder = nil
  ThreadLocal.thread_local(openhab_rule_type: "script", openhab_rule_uid: id) do
    builder = BuilderDSL.new(block.binding)
    builder.uid(id)
    builder.tags("Script", *Array.wrap(tag), *Array.wrap(tags))
    builder.name(name)
    builder.description(description)
    builder.script(&block)
    logger.trace { builder.inspect }
    builder.build(provider, script)
  end
end