Module: OpenHAB::Core::Rules::Rule

Defined in:
lib/openhab/core/rules/rule.rb

Overview

A Rule is a chunk of code that can execute when certain conditions are met, enabling the core dynamic functionality of openHAB.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionString? (readonly)

Returns The rule’s description.

Returns:

  • (String, nil)

    The rule’s description



# File 'lib/openhab/core/rules/rule.rb', line 17

#nameString? (readonly)

Returns The rule’s human-readable name.

Returns:

  • (String, nil)

    The rule’s human-readable name



# File 'lib/openhab/core/rules/rule.rb', line 14

#statusRuleStatus? (readonly)

Returns:

  • (RuleStatus, nil)


133
134
135
# File 'lib/openhab/core/rules/rule.rb', line 133

def status
  Rules.manager&.get_status(uid)
end

#status_infoRuleStatusInfo? (readonly)

Returns:

  • (RuleStatusInfo, nil)


141
142
143
# File 'lib/openhab/core/rules/rule.rb', line 141

def status_info
  Rules.manager&.get_status_info(uid)
end

#tagsArray<Tag> (readonly)

Returns The rule’s list of tags.

Returns:

  • (Array<Tag>)

    The rule’s list of tags



# File 'lib/openhab/core/rules/rule.rb', line 20

Instance Method Details

#disablevoid

This method returns an undefined value.

Disable the Rule



100
101
102
# File 'lib/openhab/core/rules/rule.rb', line 100

def disable
  enable(enabled: false)
end

#disabled?true, false

Check if the rule’s status detail == ‘DISABLED`

Returns:

  • (true, false)


109
110
111
112
# File 'lib/openhab/core/rules/rule.rb', line 109

def disabled?
  info = status_info
  info.nil? || info.status_detail == RuleStatusDetail::DISABLED
end

#enable(enabled: true) ⇒ void

This method returns an undefined value.

Enable the Rule

Parameters:

  • enabled (true, false) (defaults to: true)


91
92
93
# File 'lib/openhab/core/rules/rule.rb', line 91

def enable(enabled: true)
  Rules.manager.set_enabled(uid, enabled)
end

#expert?true, false

Check if visibility == ‘EXPERT`

Returns:

  • (true, false)


# File 'lib/openhab/core/rules/rule.rb', line 35

#hidden?true, false

Check if visibility == ‘HIDDEN`

Returns:

  • (true, false)


# File 'lib/openhab/core/rules/rule.rb', line 29

#idle?true, false

Check if rule status == ‘IDLE`

Returns:

  • (true, false)


57
58
59
60
61
62
63
# File 'lib/openhab/core/rules/rule.rb', line 57

Visibility.constants.each do |visibility|
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
    def #{visibility.to_s.downcase}?           # def visibile?
      visibility == Visibility::#{visibility}  #   visibility == Visibility::VISIBLE
    end                                        # end
  RUBY
end

#initializing?true, false

Check if rule status == ‘INITIALIZING`

Returns:

  • (true, false)


57
58
59
60
61
62
63
# File 'lib/openhab/core/rules/rule.rb', line 57

Visibility.constants.each do |visibility|
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
    def #{visibility.to_s.downcase}?           # def visibile?
      visibility == Visibility::#{visibility}  #   visibility == Visibility::VISIBLE
    end                                        # end
  RUBY
end

#inspectString

Returns:

  • (String)


146
147
148
149
150
151
152
153
154
155
# File 'lib/openhab/core/rules/rule.rb', line 146

def inspect
  r = "#<OpenHAB::Core::Rules::Rule #{uid}"
  r += " #{name.inspect}" if name
  r += " #{visibility}" unless visible?
  r += " #{status || "<detached>"}"
  r += " (#{status_info.status_detail})" if status_info && status_info.status_detail != RuleStatusDetail::NONE
  r += " tags=#{tags.to_a.inspect}" unless tags.empty?
  r += " configuration=#{configuration.properties.to_h}" if configuration && !configuration.properties.empty?
  "#{r}>"
end

#running?true, false

Check if rule status == ‘RUNNING`

Returns:

  • (true, false)


57
58
59
60
61
62
63
# File 'lib/openhab/core/rules/rule.rb', line 57

Visibility.constants.each do |visibility|
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
    def #{visibility.to_s.downcase}?           # def visibile?
      visibility == Visibility::#{visibility}  #   visibility == Visibility::VISIBLE
    end                                        # end
  RUBY
end

#tagged?(*tags) ⇒ Boolean

Checks if this rule has at least one of the given tags.

(see Items::Item#tagged)

Examples:

Find rules tagged with “Halloween”

rules.tagged?("Halloweed")

Returns:

  • (Boolean)


122
123
124
125
126
127
# File 'lib/openhab/core/rules/rule.rb', line 122

def tagged?(*tags)
  tags.map! do |tag|
    tag.is_a?(::Module) ? tag.simple_name : tag # ::Module to distinguish against Rule::Module!
  end
  !(self.tags.to_a & tags).empty?
end

#to_sString

Returns:

  • (String)


158
159
160
# File 'lib/openhab/core/rules/rule.rb', line 158

def to_s
  uid
end

#trigger(event = nil, consider_conditions: false, **context) ⇒ Hash Also known as: run

Manually trigger the rule

Parameters:

  • event (Object, nil) (defaults to: nil)

    The event to pass to the rule’s execution blocks.

  • consider_conditions (Boolean) (defaults to: false)

    Whether to check the conditions of the called rules.

  • context (kwargs)

    The context to pass to the conditions and the actions of the rule.

Returns:

  • (Hash)

    A copy of the rule context, including possible return values.



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/openhab/core/rules/rule.rb', line 170

def trigger(event = nil, consider_conditions: false, **context)
  begin
    event ||= org.openhab.core.automation.events.AutomationEventFactory
                 .createExecutionEvent(uid, nil, "manual")
  rescue NameError
    # @deprecated OH3.4 doesn't have AutomationEventFactory
  end
  context.transform_keys!(&:to_s)
  # Unwrap any proxies and pass raw objects (items, things)
  context.transform_values! { |value| value.is_a?(Delegator) ? value.__getobj__ : value }
  context["event"] = event if event # @deprecated OH3.4 - remove if guard. In OH4 `event` will never be nil
  Rules.manager.run_now(uid, consider_conditions, context)
end

#uninitialized?true, false

Check if rule status == ‘UNINITIALIZED`

Returns:

  • (true, false)


80
81
82
83
# File 'lib/openhab/core/rules/rule.rb', line 80

def uninitialized?
  s = status
  s.nil? || s == RuleStatus::UNINITIALIZED
end

#visible?true, false

Check if visibility == ‘VISIBLE`

Returns:

  • (true, false)


# File 'lib/openhab/core/rules/rule.rb', line 23