Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
feat(rule): add tags to rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng committed Jul 29, 2022
1 parent 9636e26 commit d5c29de
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 34 deletions.
33 changes: 17 additions & 16 deletions docs/usage/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@ end

## All of the properties that are available to the rule resource are

| Property | Type | Last/Multiple | Options | Default | Description | Examples |
| ---------------- | ----------------------------------------------------------------------- | ------------- | ------------------------------------- | ------- | --------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| description | String | Single | | | Set the rule description | |
| every | Symbol or Duration | Multiple | at: String or TimeOfDay | | When to execute rule | Symbol (:second, :minute, :hour, :day, :week, :month, :year, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday) or duration (5.minutes, 20.seconds, 14.hours), at: '5:15' or TimeOfDay(h:5, m:15) |
| cron | String | Multiple | | | OpenHAB Style Cron Expression | '* * * * * * ?' |
| Property | Type | Last/Multiple | Options | Default | Description | Examples |
| ---------------- | ------------------------------------------------------------------------- | ------------- | ------------------------------------- | ------- | --------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| description | String | Single | | | Set the rule description | |
| tags | String or Array of String or Semantics class | Single | | | Set the rule tags | tags 'lighting', 'security' |
| every | Symbol or Duration | Multiple | at: String or TimeOfDay | | When to execute rule | Symbol (:second, :minute, :hour, :day, :week, :month, :year, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday) or duration (5.minutes, 20.seconds, 14.hours), at: '5:15' or TimeOfDay(h:5, m:15) |
| cron | String | Multiple | | | OpenHAB Style Cron Expression | '* * * * * * ?' |
| changed | Item or Item Array[] or Group or Group.members or Thing or Thing Array [] | Multiple | from: State, to: State, for: Duration | | Execute rule on item state change | BedroomLightSwitch, from: OFF to ON |
| updated | Item or Item Array[] or Group or Group.members or Thing or Thing Array [] | Multiple | to: State | | Execute rule on item update | BedroomLightSwitch, to: ON |
| received_command | Item or Item Array[] or Group or Group.members | Multiple | command: | | Execute rule on item command | BedroomLightSwitch command: ON |
| channel | Channel | Multiple | triggered: | | Execute rule on channel trigger | `'astro:sun:home:rise#event', triggered: 'START'` |
| on_start | Boolean | Single | | false | Execute rule on system start | on_start |
| run | Block passed event | Multiple | | | Code to execute on rule trigger | |
| triggered | Block passed item | Multiple | | | Code with triggering item to execute on rule trigger | |
| delay | Duration | Multiple | | | Duration to wait between or after run blocks | delay 5.seconds |
| otherwise | Block passed event | Multiple | | | Code to execute on rule trigger if guards are not satisfied | |
| between | Range of TimeOfDay or String Objects | Single | | | Only execute rule if current time is between supplied time ranges | '6:05'..'14:05:05' (Include end) or '6:05'...'14:05:05' (Excludes end second) or TimeOfDay.new(h:6,m:5)..TimeOfDay.new(h:14,m:15,s:5) |
| only_if | Item or Item Array, or Block | Multiple | | | Only execute rule if all supplied items are "On" and/or block returns true | BedroomLightSwitch, BackyardLightSwitch or {BedroomLightSwitch.state == ON} |
| not_if | Item or Item Array, or Block | Multiple | | | Do **NOT** execute rule if any of the supplied items or blocks returns true | BedroomLightSwitch |
| enabled | Boolean | Single | | true | Enable or disable the rule from executing | |
| received_command | Item or Item Array[] or Group or Group.members | Multiple | command: | | Execute rule on item command | BedroomLightSwitch command: ON |
| channel | Channel | Multiple | triggered: | | Execute rule on channel trigger | `'astro:sun:home:rise#event', triggered: 'START'` |
| on_start | Boolean | Single | | false | Execute rule on system start | on_start |
| run | Block passed event | Multiple | | | Code to execute on rule trigger | |
| triggered | Block passed item | Multiple | | | Code with triggering item to execute on rule trigger | |
| delay | Duration | Multiple | | | Duration to wait between or after run blocks | delay 5.seconds |
| otherwise | Block passed event | Multiple | | | Code to execute on rule trigger if guards are not satisfied | |
| between | Range of TimeOfDay or String Objects | Single | | | Only execute rule if current time is between supplied time ranges | '6:05'..'14:05:05' (Include end) or '6:05'...'14:05:05' (Excludes end second) or TimeOfDay.new(h:6,m:5)..TimeOfDay.new(h:14,m:15,s:5) |
| only_if | Item or Item Array, or Block | Multiple | | | Only execute rule if all supplied items are "On" and/or block returns true | BedroomLightSwitch, BackyardLightSwitch or {BedroomLightSwitch.state == ON} |
| not_if | Item or Item Array, or Block | Multiple | | | Do **NOT** execute rule if any of the supplied items or blocks returns true | BedroomLightSwitch |
| enabled | Boolean | Single | | true | Enable or disable the rule from executing | |

Last means that last value for the property is used <br>
Multiple indicates that multiple entries of the same property can be used in aggregate
Expand Down
17 changes: 0 additions & 17 deletions features/description.feature

This file was deleted.

26 changes: 26 additions & 0 deletions features/rule_language.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ Feature: rule_language
Background:
Given Clean OpenHAB with latest Ruby Libraries

Scenario: Rules support description
Given a rule:
"""
rule 'Test rule' do
description 'This is the rule description'
on_start
run {}
end
"""
When I deploy the rule
Then The rule 'Test rule' should have 'This is the rule description' as its description

Scenario: Rules support tags
Given a rule:
"""
myrule = rule 'Test rule' do
tags "tag1", "tag2", Semantics::LivingRoom
on_start
run {}
end
logger.info myrule.tags.to_a.sort
"""
When I deploy the rule
Then It should log '["LivingRoom", "tag1", "tag2"]' within 5 seconds

Scenario: Call function from rule
Given code in a rules file
"""
Expand Down
23 changes: 23 additions & 0 deletions lib/openhab/dsl/rules/automation_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def initialize(config:) # rubocop:disable Metrics
super()
set_name(config.name)
set_description(config.description)
set_tags(to_string_set(config.tags))
set_triggers(config.triggers)
self.uid = config.uid
@run_context = config.caller
Expand Down Expand Up @@ -263,6 +264,28 @@ def process_run_task(event, task)
@run_context.instance_exec(event, &task.block)
end

#
# Convert the given array to a set of strings.
# Convert Semantics classes to their simple name
#
# @example
# to_string_set("tag1", Semantics::LivingRoom)
#
# @param [Array] *tags An array of strings or Semantics classes
#
# @return [Set] A set of strings
#
def to_string_set(*tags)
tags = tags.flatten.map do |tag|
if tag.respond_to?(:java_class) && tag < org.openhab.core.semantics.Tag
tag.java_class.simple_name
else
tag.to_s
end
end
Set.new(tags)
end

#
# Create a new hash in which all elements are converted to strings
#
Expand Down
2 changes: 1 addition & 1 deletion lib/openhab/dsl/rules/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def self.infer_rule_id_from_block(block)
#
# Create a new rule
#
# @param [String] rule_name <description>
# @param [String] rule_name The rule name
# @yield [] Block executed in context of a RuleConfig
#
#
Expand Down
2 changes: 2 additions & 0 deletions lib/openhab/dsl/rules/rule_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class RuleConfig
prop :uid
prop :name
prop :description
prop :tags
prop :enabled
prop :between

Expand All @@ -79,6 +80,7 @@ def initialize(rule_name, caller_binding)
name(rule_name)
enabled(true)
on_start(false)
tags([])
end

#
Expand Down

0 comments on commit d5c29de

Please sign in to comment.