openhab-scripting 2.16.2 → 2.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab.rb +12 -16
  3. data/lib/openhab/core/entity_lookup.rb +173 -0
  4. data/lib/openhab/core/openhab_setup.rb +31 -0
  5. data/lib/openhab/core/osgi.rb +61 -0
  6. data/lib/openhab/dsl/actions.rb +105 -0
  7. data/lib/openhab/dsl/dsl.rb +49 -0
  8. data/lib/openhab/{core/dsl → dsl}/gems.rb +0 -1
  9. data/lib/openhab/dsl/group.rb +100 -0
  10. data/lib/openhab/dsl/items/datetime_item.rb +97 -0
  11. data/lib/openhab/dsl/items/items.rb +46 -0
  12. data/lib/openhab/dsl/items/number_item.rb +352 -0
  13. data/lib/openhab/dsl/items/rollershutter_item.rb +179 -0
  14. data/lib/openhab/dsl/items/string_item.rb +120 -0
  15. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +4 -0
  16. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +32 -0
  17. data/lib/openhab/dsl/monkey_patch/events/events.rb +5 -0
  18. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +23 -0
  19. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +35 -0
  20. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +33 -0
  21. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +61 -0
  22. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +193 -0
  23. data/lib/openhab/dsl/monkey_patch/items/group_item.rb +37 -0
  24. data/lib/openhab/dsl/monkey_patch/items/items.rb +133 -0
  25. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +281 -0
  26. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +70 -0
  27. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +95 -0
  28. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +39 -0
  29. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +47 -0
  30. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +8 -0
  31. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +41 -0
  32. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +32 -0
  33. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +70 -0
  34. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +51 -0
  35. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +36 -0
  36. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +32 -0
  37. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +69 -0
  38. data/lib/openhab/dsl/monkey_patch/types/types.rb +9 -0
  39. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +33 -0
  40. data/lib/openhab/dsl/persistence.rb +25 -0
  41. data/lib/openhab/dsl/rules/automation_rule.rb +342 -0
  42. data/lib/openhab/dsl/rules/guard.rb +134 -0
  43. data/lib/openhab/dsl/rules/property.rb +102 -0
  44. data/lib/openhab/dsl/rules/rule.rb +116 -0
  45. data/lib/openhab/dsl/rules/rule_config.rb +151 -0
  46. data/lib/openhab/dsl/rules/triggers/changed.rb +143 -0
  47. data/lib/openhab/dsl/rules/triggers/channel.rb +53 -0
  48. data/lib/openhab/dsl/rules/triggers/command.rb +104 -0
  49. data/lib/openhab/dsl/rules/triggers/cron.rb +177 -0
  50. data/lib/openhab/dsl/rules/triggers/trigger.rb +124 -0
  51. data/lib/openhab/dsl/rules/triggers/updated.rb +98 -0
  52. data/lib/openhab/dsl/states.rb +61 -0
  53. data/lib/openhab/dsl/things.rb +91 -0
  54. data/lib/openhab/dsl/time_of_day.rb +232 -0
  55. data/lib/openhab/dsl/timers.rb +77 -0
  56. data/lib/openhab/dsl/types/datetime.rb +326 -0
  57. data/lib/openhab/dsl/types/quantity.rb +290 -0
  58. data/lib/openhab/dsl/units.rb +39 -0
  59. data/lib/openhab/log/configuration.rb +21 -0
  60. data/lib/openhab/log/logger.rb +172 -0
  61. data/lib/openhab/version.rb +1 -1
  62. metadata +60 -58
  63. data/lib/openhab/configuration.rb +0 -16
  64. data/lib/openhab/core/cron.rb +0 -27
  65. data/lib/openhab/core/debug.rb +0 -34
  66. data/lib/openhab/core/dsl.rb +0 -51
  67. data/lib/openhab/core/dsl/actions.rb +0 -107
  68. data/lib/openhab/core/dsl/entities.rb +0 -147
  69. data/lib/openhab/core/dsl/group.rb +0 -102
  70. data/lib/openhab/core/dsl/items/items.rb +0 -51
  71. data/lib/openhab/core/dsl/items/number_item.rb +0 -323
  72. data/lib/openhab/core/dsl/items/string_item.rb +0 -122
  73. data/lib/openhab/core/dsl/monkey_patch/actions/actions.rb +0 -4
  74. data/lib/openhab/core/dsl/monkey_patch/actions/script_thing_actions.rb +0 -22
  75. data/lib/openhab/core/dsl/monkey_patch/events.rb +0 -5
  76. data/lib/openhab/core/dsl/monkey_patch/events/item_command.rb +0 -13
  77. data/lib/openhab/core/dsl/monkey_patch/events/item_state_changed.rb +0 -25
  78. data/lib/openhab/core/dsl/monkey_patch/events/thing_status_info.rb +0 -26
  79. data/lib/openhab/core/dsl/monkey_patch/items/contact_item.rb +0 -54
  80. data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +0 -182
  81. data/lib/openhab/core/dsl/monkey_patch/items/group_item.rb +0 -27
  82. data/lib/openhab/core/dsl/monkey_patch/items/items.rb +0 -132
  83. data/lib/openhab/core/dsl/monkey_patch/items/metadata.rb +0 -283
  84. data/lib/openhab/core/dsl/monkey_patch/items/persistence.rb +0 -72
  85. data/lib/openhab/core/dsl/monkey_patch/items/switch_item.rb +0 -87
  86. data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +0 -41
  87. data/lib/openhab/core/dsl/monkey_patch/ruby/range.rb +0 -47
  88. data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +0 -7
  89. data/lib/openhab/core/dsl/monkey_patch/ruby/string.rb +0 -43
  90. data/lib/openhab/core/dsl/monkey_patch/types/decimal_type.rb +0 -60
  91. data/lib/openhab/core/dsl/monkey_patch/types/on_off_type.rb +0 -41
  92. data/lib/openhab/core/dsl/monkey_patch/types/open_closed_type.rb +0 -25
  93. data/lib/openhab/core/dsl/monkey_patch/types/percent_type.rb +0 -23
  94. data/lib/openhab/core/dsl/monkey_patch/types/quantity_type.rb +0 -58
  95. data/lib/openhab/core/dsl/monkey_patch/types/types.rb +0 -8
  96. data/lib/openhab/core/dsl/persistence.rb +0 -27
  97. data/lib/openhab/core/dsl/property.rb +0 -96
  98. data/lib/openhab/core/dsl/rule/automation_rule.rb +0 -345
  99. data/lib/openhab/core/dsl/rule/guard.rb +0 -136
  100. data/lib/openhab/core/dsl/rule/rule.rb +0 -117
  101. data/lib/openhab/core/dsl/rule/rule_config.rb +0 -153
  102. data/lib/openhab/core/dsl/rule/triggers/changed.rb +0 -145
  103. data/lib/openhab/core/dsl/rule/triggers/channel.rb +0 -55
  104. data/lib/openhab/core/dsl/rule/triggers/command.rb +0 -106
  105. data/lib/openhab/core/dsl/rule/triggers/cron.rb +0 -160
  106. data/lib/openhab/core/dsl/rule/triggers/trigger.rb +0 -126
  107. data/lib/openhab/core/dsl/rule/triggers/updated.rb +0 -100
  108. data/lib/openhab/core/dsl/states.rb +0 -63
  109. data/lib/openhab/core/dsl/things.rb +0 -93
  110. data/lib/openhab/core/dsl/time_of_day.rb +0 -231
  111. data/lib/openhab/core/dsl/timers.rb +0 -79
  112. data/lib/openhab/core/dsl/types/quantity.rb +0 -292
  113. data/lib/openhab/core/dsl/units.rb +0 -41
  114. data/lib/openhab/core/log.rb +0 -170
  115. data/lib/openhab/core/patch_load_path.rb +0 -7
  116. data/lib/openhab/core/startup_delay.rb +0 -23
  117. data/lib/openhab/osgi.rb +0 -59
@@ -1,117 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'core/dsl/rule/rule_config'
4
- require 'core/dsl/rule/automation_rule'
5
- require 'core/dsl/rule/guard'
6
-
7
- module OpenHAB
8
- module Core
9
- #
10
- # Contains code to create an OpenHAB DSL
11
- #
12
- module DSL
13
- #
14
- # Creates and manages OpenHAB Rules
15
- #
16
- module Rule
17
- #
18
- # Create a new rule
19
- #
20
- # @param [String] rule_name <description>
21
- # @yield [] Block executed in context of a RuleConfig
22
- #
23
- #
24
- def rule(rule_name, &block)
25
- config = RuleConfig.new(rule_name, block.binding)
26
- config.instance_eval(&block)
27
- config.guard = Guard::Guard.new(only_if: config.only_if, not_if: config.not_if)
28
- logger.trace { config.inspect }
29
- process_rule_config(config)
30
- end
31
-
32
- #
33
- # Create a logger where name includes rule name if name is set
34
- #
35
- # @return [Logging::Logger] Logger with name that appended with rule name if rule name is set
36
- #
37
- def logger
38
- if name
39
- Logging.logger(name.chomp.gsub(/\s+/, '_'))
40
- else
41
- super
42
- end
43
- end
44
-
45
- private
46
-
47
- #
48
- # Process a rule based on the supplied configuration
49
- #
50
- # @param [RuleConfig] config for rule
51
- #
52
- #
53
- def process_rule_config(config)
54
- return unless create_rule?(config)
55
-
56
- rule = AutomationRule.new(config: config)
57
- add_rule(rule)
58
- rule.execute if config.on_start?
59
- end
60
-
61
- #
62
- # Should a rule be created based on rule configuration
63
- #
64
- # @param [RuleConfig] config to check
65
- #
66
- # @return [Boolean] true if it should be created, false otherwise
67
- #
68
- def create_rule?(config)
69
- if !triggers?(config)
70
- logger.warn "Rule '#{config.name}' has no triggers, not creating rule"
71
- elsif !execution_blocks?(config)
72
- logger.warn "Rule '#{config.name}' has no execution blocks, not creating rule"
73
- elsif !config.enabled
74
- logger.debug "Rule '#{config.name}' marked as disabled, not creating rule."
75
- else
76
- return true
77
- end
78
- false
79
- end
80
-
81
- #
82
- # Check if the rule has any triggers
83
- #
84
- # @param [RuleConfig] config to check for triggers
85
- #
86
- # @return [Boolean] True if rule has triggers, false otherwise
87
- #
88
- def triggers?(config)
89
- config.on_start? || config.triggers.length.positive?
90
- end
91
-
92
- #
93
- # Check if the rule has any execution blocks
94
- #
95
- # @param [RuleConfig] config to check for triggers
96
- #
97
- # @return [Boolean] True if rule has execution blocks, false otherwise
98
- #
99
- def execution_blocks?(config)
100
- (config.run || []).length.positive?
101
- end
102
-
103
- #
104
- # Add a rule to the automation managed
105
- #
106
- # @param [Java::OrgOpenhabCoreAutomationModuleScriptRulesupportSharedSimple::SimpleRule] rule to add
107
- #
108
- #
109
- def add_rule(rule)
110
- # rubocop: disable Style/GlobalVars
111
- $scriptExtension.get('automationManager').addRule(rule)
112
- # rubocop: enable Style/GlobalVars
113
- end
114
- end
115
- end
116
- end
117
- end
@@ -1,153 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'pp'
5
- require 'core/dsl/property'
6
- require 'core/dsl/rule/triggers/cron'
7
- require 'core/dsl/rule/triggers/changed'
8
- require 'core/dsl/rule/triggers/channel'
9
- require 'core/dsl/rule/triggers/command'
10
- require 'core/dsl/rule/triggers/updated'
11
- require 'core/dsl/rule/guard'
12
- require 'core/dsl/entities'
13
- require 'core/dsl/time_of_day'
14
- require 'core/dsl'
15
- require 'core/dsl/timers'
16
-
17
- module OpenHAB
18
- module Core
19
- module DSL
20
- #
21
- # Creates and manages OpenHAB Rules
22
- #
23
- module Rule
24
- #
25
- # Rule configuration for OpenHAB Rules engine
26
- #
27
- class RuleConfig
28
- include EntityLookup
29
- include OpenHAB::Core::DSL::Rule::Triggers
30
- include Guard
31
- include DSLProperty
32
- include Logging
33
- extend OpenHAB::Core::DSL
34
-
35
- java_import org.openhab.core.library.items.SwitchItem
36
-
37
- # @return [Array] Of triggers
38
- attr_reader :triggers
39
-
40
- # @return [Array] Of trigger delays
41
- attr_reader :trigger_delays
42
-
43
- # @return [Array] Of trigger guards
44
- attr_accessor :guard
45
-
46
- #
47
- # Struct holding a run block
48
- #
49
- Run = Struct.new(:block)
50
-
51
- #
52
- # Struct holding a Triggered block
53
- #
54
- Trigger = Struct.new(:block)
55
-
56
- #
57
- # Struct holding an otherwise block
58
- #
59
- Otherwise = Struct.new(:block)
60
-
61
- #
62
- # Struct holding rule delays
63
- #
64
- Delay = Struct.new(:duration)
65
-
66
- prop_array :run, array_name: :run_queue, wrapper: Run
67
- prop_array :triggered, array_name: :run_queue, wrapper: Trigger
68
- prop_array :delay, array_name: :run_queue, wrapper: Delay
69
- prop_array :otherwise, array_name: :run_queue, wrapper: Otherwise
70
-
71
- prop :name
72
- prop :description
73
- prop :enabled
74
- prop :between
75
-
76
- #
77
- # Create a new RuleConfig
78
- #
79
- # @param [Object] caller_binding The object initializing this configuration.
80
- # Used to execute within the object's context
81
- #
82
- def initialize(rule_name, caller_binding)
83
- @triggers = []
84
- @trigger_delays = {}
85
- @caller = caller_binding.eval 'self'
86
- enabled(true)
87
- on_start(false)
88
- name(rule_name)
89
- end
90
-
91
- #
92
- # Start this rule on system startup
93
- #
94
- # @param [Boolean] run_on_start Run this rule on start, defaults to True
95
- #
96
- #
97
- # rubocop: disable Style/OptionalBooleanParameter
98
- # Disabled cop due to use in a DSL
99
- def on_start(run_on_start = true)
100
- @on_start = run_on_start
101
- end
102
- # rubocop: enable Style/OptionalBooleanParameter
103
-
104
- #
105
- # Checks if this rule should run on start
106
- #
107
- # @return [Boolean] True if rule should run on start, false otherwise.
108
- #
109
- def on_start?
110
- @on_start
111
- end
112
-
113
- #
114
- # Run the supplied block inside the object instance of the object that created the rule config
115
- #
116
- # @yield [] Block executed in context of the object creating the rule config
117
- #
118
- #
119
- def my(&block)
120
- @caller.instance_eval(&block)
121
- end
122
-
123
- #
124
- # Create a logger where name includes rule name if name is set
125
- #
126
- # @return [Logging::Logger] Logger with name that appended with rule name if rule name is set
127
- #
128
- def logger
129
- if name
130
- Logging.logger(name.chomp.gsub(/\s+/, '_'))
131
- else
132
- super
133
- end
134
- end
135
-
136
- #
137
- # Inspect the config object
138
- #
139
- # @return [String] details of the config object
140
- #
141
- def inspect
142
- "Name: (#{name}) " \
143
- "Triggers: (#{triggers}) " \
144
- "Run blocks: (#{run}) " \
145
- "on_start: (#{on_start?}) " \
146
- "Trigger Waits: #{trigger_delays} " \
147
- "Trigger UIDs: #{triggers.map(&:id).join(', ')}"
148
- end
149
- end
150
- end
151
- end
152
- end
153
- end
@@ -1,145 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'core/log'
4
-
5
- module OpenHAB
6
- module Core
7
- module DSL
8
- module Rule
9
- #
10
- # Module holds rule triggers
11
- #
12
- module Triggers
13
- include Logging
14
-
15
- #
16
- # Struct capturing data necessary for a conditional trigger
17
- #
18
- TriggerDelay = Struct.new(:to, :from, :duration, :timer, :tracking_to, keyword_init: true) do
19
- def timer_active?
20
- timer&.is_active
21
- end
22
- end
23
-
24
- #
25
- # Creates a trigger item, group and thing changed
26
- #
27
- # @param [Object] items array of objects to create trigger for
28
- # @param [to] to state for object to change for
29
- # @param [from] from <description>
30
- # @param [OpenHAB::Core::Duration] for Duration to delay trigger until to state is met
31
- #
32
- # @return [Trigger] OpenHAB trigger
33
- #
34
- def changed(*items, to: nil, from: nil, for: nil)
35
- items.flatten.each do |item|
36
- logger.trace("Creating changed trigger for entity(#{item}), to(#{to}), from(#{from})")
37
- # for is a reserved word in ruby, so use local_variable_get :for
38
- if (wait_duration = binding.local_variable_get(:for))
39
- changed_wait(item, to: to, from: from, duration: wait_duration)
40
- else
41
- # Place in array and flatten to support multiple to elements or single or nil
42
- [to].flatten.each do |to_state|
43
- create_changed_trigger(item, from, to_state)
44
- end
45
- end
46
- end
47
- end
48
-
49
- private
50
-
51
- #
52
- # Create a TriggerDelay for for an item or group that is changed for a specific duration
53
- #
54
- # @param [Object] item to create trigger delay for
55
- # @param [OpenHAB::Core::Duration] duration to delay trigger for until condition is met
56
- # @param [Item State] to OpenHAB Item State item or group needs to change to
57
- # @param [Item State] from OpenHAB Item State item or group needs to be coming from
58
- #
59
- # @return [Array] Array of current TriggerDelay objects
60
- #
61
- def changed_wait(item, duration:, to: nil, from: nil)
62
- # If GroupItems specified, use the group state trigger instead
63
- if item.is_a? GroupItems
64
- config = { 'groupName' => item.group.name }
65
- trigger = Trigger::GROUP_STATE_CHANGE
66
- else
67
- config = { 'itemName' => item.name }
68
- trigger = Trigger::ITEM_STATE_CHANGE
69
- end
70
- logger.trace("Creating Changed Wait Change Trigger for #{config}")
71
- trigger = append_trigger(trigger, config)
72
- @trigger_delays[trigger.id] = TriggerDelay.new(to: to, from: from, duration: duration)
73
- end
74
-
75
- #
76
- # Create a changed trigger
77
- #
78
- # @param [Object] item to create changed trigger on
79
- # @param [String] from state to restrict trigger to
80
- # @param [String] to state restrict trigger to
81
- #
82
- #
83
- def create_changed_trigger(item, from, to)
84
- trigger, config = case item
85
- when GroupItems then create_group_changed_trigger(item, from, to)
86
- when Thing then create_thing_changed_trigger(item, from, to)
87
- else create_item_changed_trigger(item, from, to)
88
- end
89
- append_trigger(trigger, config)
90
- end
91
-
92
- #
93
- # Create a changed trigger for a thing
94
- #
95
- # @param [Thing] thing to detected changed states on
96
- # @param [String] from state to restrict trigger to
97
- # @param [String] to state to restrict trigger to
98
- #
99
- # @return [Array<Hash,String>] first element is a String specifying trigger type
100
- # second element is a Hash configuring trigger
101
- #
102
- def create_thing_changed_trigger(thing, from, to)
103
- trigger_for_thing(thing, Trigger::THING_CHANGE, to, from)
104
- end
105
-
106
- #
107
- # Create a changed trigger for an item
108
- #
109
- # @param [Item] item to detected changed states on
110
- # @param [String] from state to restrict trigger to
111
- # @param [String] to to restrict trigger to
112
- #
113
- # @return [Array<Hash,String>] first element is a String specifying trigger type
114
- # second element is a Hash configuring trigger
115
- #
116
- def create_item_changed_trigger(item, from, to)
117
- config = { 'itemName' => item.name }
118
- config['state'] = to.to_s if to
119
- config['previousState'] = from.to_s if from
120
- trigger = Trigger::ITEM_STATE_CHANGE
121
- [trigger, config]
122
- end
123
-
124
- #
125
- # Create a changed trigger for group items
126
- #
127
- # @param [Group] group to detected changed states on
128
- # @param [String] from state to restrict trigger to
129
- # @param [String] to to restrict trigger to
130
- #
131
- # @return [Array<Hash,String>] first element is a String specifying trigger type
132
- # second element is a Hash configuring trigger
133
- #
134
- def create_group_changed_trigger(group, from, to)
135
- config = { 'groupName' => group.group.name }
136
- config['state'] = to.to_s if to
137
- config['previousState'] = from.to_s if from
138
- trigger = Trigger::GROUP_STATE_CHANGE
139
- [trigger, config]
140
- end
141
- end
142
- end
143
- end
144
- end
145
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'core/log'
4
- require 'openhab/core/dsl/rule/triggers/trigger'
5
-
6
- module OpenHAB
7
- module Core
8
- module DSL
9
- module Rule
10
- #
11
- # Channel triggers
12
- #
13
- module Triggers
14
- include Logging
15
-
16
- #
17
- # Creates a channel trigger
18
- #
19
- # @param [Array] channels array to create triggers for on form of 'binding_id:type_id:thing_id#channel_id'
20
- # or 'channel_id' if thing is provided
21
- # @param [thing] thing to create trigger for if not specified with the channel
22
- # @param [String] triggered specific triggering condition to match for trigger
23
- #
24
- #
25
- def channel(*channels, thing: nil, triggered: nil)
26
- channels.flatten.each do |channel|
27
- channel = [thing, channel].join(':') if thing
28
- logger.trace("Creating channel trigger for channel(#{channel}), thing(#{thing}), trigger(#{triggered})")
29
- [triggered].flatten.each do |trigger|
30
- create_channel_trigger(channel, trigger)
31
- end
32
- end
33
- end
34
-
35
- private
36
-
37
- #
38
- # Create a trigger for a channel
39
- #
40
- # @param [Channel] channel to look for triggers
41
- # @param [Trigger] trigger specific channel trigger to match
42
- #
43
- #
44
- def create_channel_trigger(channel, trigger)
45
- config = { 'channelUID' => channel }
46
- config['event'] = trigger.to_s unless trigger.nil?
47
- config['channelUID'] = channel
48
- logger.trace("Creating Change Trigger for #{config}")
49
- @triggers << Trigger.trigger(type: Trigger::CHANNEL_EVENT, config: config)
50
- end
51
- end
52
- end
53
- end
54
- end
55
- end