From d5c29deabb793d23880463f86285bf6764dfb5c7 Mon Sep 17 00:00:00 2001 From: Jimmy Tanagra Date: Fri, 29 Jul 2022 11:37:50 +1000 Subject: [PATCH] feat(rule): add tags to rules --- docs/usage/rule.md | 33 ++++++++++++------------ features/description.feature | 17 ------------ features/rule_language.feature | 26 +++++++++++++++++++ lib/openhab/dsl/rules/automation_rule.rb | 23 +++++++++++++++++ lib/openhab/dsl/rules/rule.rb | 2 +- lib/openhab/dsl/rules/rule_config.rb | 2 ++ 6 files changed, 69 insertions(+), 34 deletions(-) delete mode 100644 features/description.feature diff --git a/docs/usage/rule.md b/docs/usage/rule.md index 94c8b29c8..b19e9b1c1 100644 --- a/docs/usage/rule.md +++ b/docs/usage/rule.md @@ -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
Multiple indicates that multiple entries of the same property can be used in aggregate diff --git a/features/description.feature b/features/description.feature deleted file mode 100644 index 32925990d..000000000 --- a/features/description.feature +++ /dev/null @@ -1,17 +0,0 @@ -Feature: description - Rule languages supports description feature - - Background: - Given Clean OpenHAB with latest Ruby Libraries - - Scenario: Set the rule 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 diff --git a/features/rule_language.feature b/features/rule_language.feature index 291811608..2dc944066 100644 --- a/features/rule_language.feature +++ b/features/rule_language.feature @@ -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 """ diff --git a/lib/openhab/dsl/rules/automation_rule.rb b/lib/openhab/dsl/rules/automation_rule.rb index 051f3eb87..2569dbd7e 100644 --- a/lib/openhab/dsl/rules/automation_rule.rb +++ b/lib/openhab/dsl/rules/automation_rule.rb @@ -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 @@ -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 # diff --git a/lib/openhab/dsl/rules/rule.rb b/lib/openhab/dsl/rules/rule.rb index d306227b8..f2132fbc1 100644 --- a/lib/openhab/dsl/rules/rule.rb +++ b/lib/openhab/dsl/rules/rule.rb @@ -43,7 +43,7 @@ def self.infer_rule_id_from_block(block) # # Create a new rule # - # @param [String] rule_name + # @param [String] rule_name The rule name # @yield [] Block executed in context of a RuleConfig # # diff --git a/lib/openhab/dsl/rules/rule_config.rb b/lib/openhab/dsl/rules/rule_config.rb index 9cba82146..9d9ce7575 100644 --- a/lib/openhab/dsl/rules/rule_config.rb +++ b/lib/openhab/dsl/rules/rule_config.rb @@ -64,6 +64,7 @@ class RuleConfig prop :uid prop :name prop :description + prop :tags prop :enabled prop :between @@ -79,6 +80,7 @@ def initialize(rule_name, caller_binding) name(rule_name) enabled(true) on_start(false) + tags([]) end #