Class: OpenHAB::DSL::Rules::Builder
- Inherits:
-
Object
- Object
- OpenHAB::DSL::Rules::Builder
- 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
-
#initialize(provider) ⇒ Builder
constructor
A new instance of Builder.
-
#rule(name = nil, id: nil, script: nil, binding: nil) {|rule| ... } ⇒ Core::Rules::Rule?
Create a new rule.
-
#scene(name = nil, description: nil, id: nil, tag: nil, tags: nil, script: nil) { ... } ⇒ Core::Rules::Rule
Create a new scene.
-
#script(name = nil, description: nil, id: nil, tag: nil, tags: nil, script: nil) { ... } ⇒ Core::Rules::Rule
Create a new script.
Methods included from Core::EntityLookup
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
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class OpenHAB::Core::EntityLookup
Instance Attribute Details
#provider ⇒ org.openhab.core.automation.RuleProvider (readonly)
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.
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.
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.("Scene", *Array.wrap(tag), *Array.wrap()) 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.
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.("Script", *Array.wrap(tag), *Array.wrap()) builder.name(name) builder.description(description) builder.script(&block) logger.trace { builder.inspect } builder.build(provider, script) end end |