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)


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

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

#status_infoRuleStatusInfo? (readonly)

Returns:

  • (RuleStatusInfo, nil)


150
151
152
# File 'lib/openhab/core/rules/rule.rb', line 150

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

#enabled?true, false

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

Returns:

  • (true, false)


119
120
121
# File 'lib/openhab/core/rules/rule.rb', line 119

def enabled?
  !disabled?
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)


155
156
157
158
159
160
161
162
163
164
165
# File 'lib/openhab/core/rules/rule.rb', line 155

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 += " description=#{description.inspect}" if description
  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)


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

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)


168
169
170
# File 'lib/openhab/core/rules/rule.rb', line 168

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.



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/openhab/core/rules/rule.rb', line 180

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