openhab-scripting 4.43.1 → 4.44.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openhab/core/entity_lookup.rb +2 -4
- data/lib/openhab/core/item_proxy.rb +1 -1
- data/lib/openhab/core/osgi.rb +0 -2
- data/lib/openhab/core/services.rb +0 -2
- data/lib/openhab/dsl/actions.rb +1 -5
- data/lib/openhab/dsl/group.rb +1 -1
- data/lib/openhab/dsl/items/item_registry.rb +2 -2
- data/lib/openhab/dsl/items/metadata.rb +9 -9
- data/lib/openhab/dsl/items/persistence.rb +2 -2
- data/lib/openhab/dsl/items/timed_command.rb +0 -2
- data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +10 -1
- data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +2 -2
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +5 -5
- data/lib/openhab/dsl/rules/automation_rule.rb +23 -0
- data/lib/openhab/dsl/rules/name_inference.rb +133 -0
- data/lib/openhab/dsl/rules/rule.rb +12 -13
- data/lib/openhab/dsl/rules/rule_config.rb +7 -2
- data/lib/openhab/dsl/rules/terse.rb +2 -29
- data/lib/openhab/dsl/rules/triggers/changed.rb +7 -5
- data/lib/openhab/dsl/rules/triggers/channel.rb +5 -2
- data/lib/openhab/dsl/rules/triggers/command.rb +3 -1
- data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +1 -1
- data/lib/openhab/dsl/rules/triggers/cron/cron.rb +7 -5
- data/lib/openhab/dsl/rules/triggers/updated.rb +3 -1
- data/lib/openhab/dsl/things.rb +2 -2
- data/lib/openhab/dsl/timers/timer.rb +1 -1
- data/lib/openhab/dsl/types/date_time_type.rb +1 -1
- data/lib/openhab/dsl/types/hsb_type.rb +1 -1
- data/lib/openhab/dsl/types/percent_type.rb +1 -1
- data/lib/openhab/dsl/types/point_type.rb +1 -1
- data/lib/openhab/dsl/types/quantity_type.rb +1 -1
- data/lib/openhab/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '091c2e6fd32317b8976246f8f69332c793e9ef3d3bfc1991e9b9065716e6e668'
|
4
|
+
data.tar.gz: adfc88ad7a7bcd999bdd49c104efb395cda2b9dd1c27bf439a941700840e7457
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fef562627789d792db873720ef7e13f0c5264e1c091f943ee4fc15c0313cd5037722271195afb9506359a7a3d5e4fa133940a8e38ccaa59d183c5c4233f0464a
|
7
|
+
data.tar.gz: 919b481338f0db3a842024f8fabed9493233430e310bd3453a388731350c2a033b4d8f4d6902a28cbe527db8c398bc8cfa705f045d85dfe943e7e353353622fb
|
@@ -73,9 +73,7 @@ module OpenHAB
|
|
73
73
|
return if name.count('_') < 3
|
74
74
|
|
75
75
|
name = name.tr('_', ':')
|
76
|
-
|
77
|
-
$things.get(Java::OrgOpenhabCoreThing::ThingUID.new(name))
|
78
|
-
# rubocop: enable Style/GlobalVars
|
76
|
+
$things.get(org.openhab.core.thing.ThingUID.new(name))
|
79
77
|
end
|
80
78
|
|
81
79
|
#
|
@@ -88,7 +86,7 @@ module OpenHAB
|
|
88
86
|
def self.lookup_item(name)
|
89
87
|
logger.trace("Looking up item(#{name})")
|
90
88
|
name = name.to_s if name.is_a? Symbol
|
91
|
-
item = $ir.get(name)
|
89
|
+
item = $ir.get(name)
|
92
90
|
ItemProxy.new(item) unless item.nil?
|
93
91
|
end
|
94
92
|
end
|
data/lib/openhab/core/osgi.rb
CHANGED
@@ -50,9 +50,7 @@ module OpenHAB
|
|
50
50
|
|
51
51
|
# Get the OSGI Bundle for ScriptExtension Class
|
52
52
|
def self.bundle
|
53
|
-
# rubocop: disable Style/GlobalVars
|
54
53
|
@bundle ||= FrameworkUtil.getBundle($scriptExtension.class)
|
55
|
-
# rubocop: enable Style/GlobalVars
|
56
54
|
end
|
57
55
|
private_class_method :bundle
|
58
56
|
end
|
@@ -11,7 +11,6 @@ module OpenHAB
|
|
11
11
|
|
12
12
|
# Get the OpenHAB automation manager
|
13
13
|
# @return [AutomationManager] OpenHAB Automation manager
|
14
|
-
# rubocop:disable Style/GlobalVars
|
15
14
|
def self.automation_manager
|
16
15
|
$scriptExtension.get('automationManager')
|
17
16
|
end
|
@@ -21,6 +20,5 @@ module OpenHAB
|
|
21
20
|
def self.rule_registry
|
22
21
|
$scriptExtension.get('ruleRegistry')
|
23
22
|
end
|
24
|
-
# rubocop:enable Style/GlobalVars
|
25
23
|
end
|
26
24
|
end
|
data/lib/openhab/dsl/actions.rb
CHANGED
@@ -29,23 +29,19 @@ module OpenHAB
|
|
29
29
|
# @return [Object] OpenHAB action
|
30
30
|
#
|
31
31
|
def actions(scope, thing_uid)
|
32
|
-
# rubocop: disable Style/GlobalVars
|
33
32
|
$actions.get(scope, thing_uid)
|
34
|
-
# rubocop: enable Style/GlobalVars
|
35
33
|
end
|
36
34
|
|
37
35
|
#
|
38
36
|
# Gets the list of action objects associated with a specific ThingUID
|
39
37
|
#
|
40
|
-
# @param [
|
38
|
+
# @param [org.openhab.core.thing.ThingUID] thing_uid to get associated actions for
|
41
39
|
#
|
42
40
|
# @return [Array] of action objects associated with thing_uid, may be empty
|
43
41
|
#
|
44
42
|
def actions_for_thing(thing_uid)
|
45
43
|
thing_uid = thing_uid.to_s
|
46
|
-
# rubocop: disable Style/GlobalVars
|
47
44
|
action_keys = $actions.action_keys
|
48
|
-
# rubocop: enable Style/GlobalVars
|
49
45
|
logger.trace("Registered actions: '#{action_keys}' for thing '#{thing_uid}'")
|
50
46
|
action_keys.map { |action_key| action_key.split('-', 2) }
|
51
47
|
.select { |action_pair| action_pair.last == thing_uid }
|
data/lib/openhab/dsl/group.rb
CHANGED
@@ -39,14 +39,14 @@ module OpenHAB
|
|
39
39
|
# @param name [String] Item name to check
|
40
40
|
# @return [Boolean] true if the item exists, false otherwise
|
41
41
|
def include?(name)
|
42
|
-
!$ir.getItems(name).empty?
|
42
|
+
!$ir.getItems(name).empty?
|
43
43
|
end
|
44
44
|
alias key? include?
|
45
45
|
|
46
46
|
# Explicit conversion to array
|
47
47
|
# @return [Array]
|
48
48
|
def to_a
|
49
|
-
$ir.items.to_a
|
49
|
+
$ir.items.to_a
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
@@ -14,8 +14,8 @@ module OpenHAB
|
|
14
14
|
module Metadata
|
15
15
|
include OpenHAB::Log
|
16
16
|
|
17
|
-
java_import
|
18
|
-
java_import
|
17
|
+
java_import org.openhab.core.items.Metadata
|
18
|
+
java_import org.openhab.core.items.MetadataKey
|
19
19
|
|
20
20
|
#
|
21
21
|
# Provide the interface to access namespace's value and configuration
|
@@ -43,7 +43,7 @@ module OpenHAB
|
|
43
43
|
#
|
44
44
|
# Delete the configuration with the given key
|
45
45
|
#
|
46
|
-
# @return [
|
46
|
+
# @return [org.openhab.core.items.Metadata] the old metadata
|
47
47
|
#
|
48
48
|
def delete(key)
|
49
49
|
configuration = {}.merge(@metadata&.configuration || {})
|
@@ -55,7 +55,7 @@ module OpenHAB
|
|
55
55
|
#
|
56
56
|
# Set the metadata value
|
57
57
|
#
|
58
|
-
# @return [
|
58
|
+
# @return [org.openhab.core.items.Metadata] the old metadata
|
59
59
|
#
|
60
60
|
def value=(value)
|
61
61
|
metadata = Metadata.new(@metadata&.uID, value&.to_s, @metadata&.configuration)
|
@@ -65,7 +65,7 @@ module OpenHAB
|
|
65
65
|
#
|
66
66
|
# Set the entire configuration to a hash
|
67
67
|
#
|
68
|
-
# @return [
|
68
|
+
# @return [org.openhab.core.items.Metadata] the old metadata
|
69
69
|
#
|
70
70
|
def config=(config)
|
71
71
|
config = config.to_hash if config.respond_to?(:to_hash)
|
@@ -114,8 +114,8 @@ module OpenHAB
|
|
114
114
|
# Convert the given object to Ruby equivalent
|
115
115
|
def self.to_ruby(value)
|
116
116
|
case value
|
117
|
-
when Hash,
|
118
|
-
when Array,
|
117
|
+
when Hash, java.util.Map then to_ruby_hash(value)
|
118
|
+
when Array, java.util.List then to_ruby_array(value)
|
119
119
|
when String then String.new(value)
|
120
120
|
else value
|
121
121
|
end
|
@@ -147,7 +147,7 @@ module OpenHAB
|
|
147
147
|
# Set the metadata namespace. If the namespace does not exist, it will be created
|
148
148
|
#
|
149
149
|
# @param value [Object] The assigned value can be a OpenHAB::DSL::Items::MetadataItem,
|
150
|
-
#
|
150
|
+
# org.openhab.core.items.Metadata, Array[2] of [value, configuration],
|
151
151
|
# A String to set the value and clear the configuration,
|
152
152
|
# or a Hash to set the configuration and set the value to nil
|
153
153
|
#
|
@@ -246,7 +246,7 @@ module OpenHAB
|
|
246
246
|
end
|
247
247
|
|
248
248
|
#
|
249
|
-
# @return [
|
249
|
+
# @return [org.openhab.core.items.MetadataRegistry]
|
250
250
|
#
|
251
251
|
def self.registry
|
252
252
|
@registry ||= OpenHAB::Core::OSGI.service('org.openhab.core.items.MetadataRegistry')
|
@@ -10,7 +10,7 @@ module OpenHAB
|
|
10
10
|
# When given a positive Duration, the timestamp will be calculated as ++now-Duration++
|
11
11
|
#
|
12
12
|
module Persistence
|
13
|
-
java_import
|
13
|
+
java_import org.openhab.core.types.util.UnitUtils
|
14
14
|
|
15
15
|
# A state class with an added timestamp attribute. This is used to hold OpenHAB's HistoricItem.
|
16
16
|
class HistoricState < SimpleDelegator
|
@@ -253,7 +253,7 @@ module OpenHAB
|
|
253
253
|
# @return [ZonedDateTime]
|
254
254
|
#
|
255
255
|
def to_zdt(timestamp)
|
256
|
-
timestamp = timestamp.negated if timestamp.is_a?
|
256
|
+
timestamp = timestamp.negated if timestamp.is_a? java.time.Duration
|
257
257
|
OpenHAB::DSL.to_zdt(timestamp)
|
258
258
|
end
|
259
259
|
|
@@ -177,10 +177,8 @@ module OpenHAB
|
|
177
177
|
logger.trace "Canceling implicit timer #{@timed_command_details.timer} for "\
|
178
178
|
"#{@timed_command_details.item.id} because received event #{inputs}"
|
179
179
|
@timed_command_details.timer.cancel
|
180
|
-
# rubocop: disable Style/GlobalVars
|
181
180
|
# Disabled due to OpenHAB design
|
182
181
|
$scriptExtension.get('ruleRegistry').remove(@timed_command_details.rule_uid)
|
183
|
-
# rubocop: enable Style/GlobalVars
|
184
182
|
TimedCommand.timed_commands.delete(@timed_command_details.item)
|
185
183
|
if @block
|
186
184
|
logger.trace 'Executing user supplied block on timed command cancelation'
|
@@ -9,10 +9,19 @@ module OpenHAB
|
|
9
9
|
# Patches OpenHAB actions
|
10
10
|
#
|
11
11
|
module Actions
|
12
|
+
begin
|
13
|
+
# openHAB 3.3 uses ScriptThingActionsImpl
|
14
|
+
java_import Java::OrgOpenhabCoreAutomationModuleScriptInternalDefaultscope::ScriptThingActionsImpl
|
15
|
+
rescue NameError
|
16
|
+
# openHAB 3.2 uses ScriptThingActions
|
17
|
+
java_import Java::OrgOpenhabCoreAutomationModuleScriptInternalDefaultscope::ScriptThingActions
|
18
|
+
ScriptThingActionsImpl = ScriptThingActions
|
19
|
+
end
|
20
|
+
|
12
21
|
#
|
13
22
|
# MonkeyPatching ScriptThingActions
|
14
23
|
#
|
15
|
-
class
|
24
|
+
class ScriptThingActionsImpl
|
16
25
|
field_reader :THING_ACTIONS_MAP
|
17
26
|
|
18
27
|
#
|
@@ -9,8 +9,8 @@ module OpenHAB
|
|
9
9
|
# Patches OpenHAB events
|
10
10
|
#
|
11
11
|
module Event
|
12
|
-
java_import
|
13
|
-
java_import
|
12
|
+
java_import org.openhab.core.thing.events.ThingStatusInfoChangedEvent
|
13
|
+
java_import org.openhab.core.thing.events.ThingStatusInfoEvent
|
14
14
|
#
|
15
15
|
# Monkey patch with ruby style accessors
|
16
16
|
#
|
@@ -11,7 +11,7 @@ module OpenHAB
|
|
11
11
|
#
|
12
12
|
# Create Duration with the specified unit
|
13
13
|
#
|
14
|
-
# @return [
|
14
|
+
# @return [java.time.Duration] Duration with number of units from self
|
15
15
|
#
|
16
16
|
%w[millis seconds minutes hours].each do |unit|
|
17
17
|
define_method(unit) { java.time.Duration.public_send("of_#{unit}", self) }
|
@@ -32,7 +32,7 @@ module OpenHAB
|
|
32
32
|
#
|
33
33
|
# Create Duration with number of milliseconds
|
34
34
|
#
|
35
|
-
# @return [
|
35
|
+
# @return [java.time.Duration] Duration truncated to an integral number of milliseconds from self
|
36
36
|
#
|
37
37
|
def millis
|
38
38
|
java.time.Duration.of_millis(to_i)
|
@@ -41,7 +41,7 @@ module OpenHAB
|
|
41
41
|
#
|
42
42
|
# Create Duration with number of seconds
|
43
43
|
#
|
44
|
-
# @return [
|
44
|
+
# @return [java.time.Duration] Duration with number of seconds from self
|
45
45
|
#
|
46
46
|
def seconds
|
47
47
|
(self * 1000).millis
|
@@ -50,7 +50,7 @@ module OpenHAB
|
|
50
50
|
#
|
51
51
|
# Create Duration with number of minutes
|
52
52
|
#
|
53
|
-
# @return [
|
53
|
+
# @return [java.time.Duration] Duration with number of minutes from self
|
54
54
|
#
|
55
55
|
def minutes
|
56
56
|
(self * 60).seconds
|
@@ -59,7 +59,7 @@ module OpenHAB
|
|
59
59
|
#
|
60
60
|
# Create Duration with number of hours
|
61
61
|
#
|
62
|
-
# @return [
|
62
|
+
# @return [java.time.Duration] Duration with number of hours from self
|
63
63
|
#
|
64
64
|
def hours
|
65
65
|
(self * 60).minutes
|
@@ -37,6 +37,7 @@ module OpenHAB
|
|
37
37
|
super()
|
38
38
|
set_name(config.name)
|
39
39
|
set_description(config.description)
|
40
|
+
set_tags(to_string_set(config.tags))
|
40
41
|
set_triggers(config.triggers)
|
41
42
|
self.uid = config.uid
|
42
43
|
@run_context = config.caller
|
@@ -263,6 +264,28 @@ module OpenHAB
|
|
263
264
|
@run_context.instance_exec(event, &task.block)
|
264
265
|
end
|
265
266
|
|
267
|
+
#
|
268
|
+
# Convert the given array to a set of strings.
|
269
|
+
# Convert Semantics classes to their simple name
|
270
|
+
#
|
271
|
+
# @example
|
272
|
+
# to_string_set("tag1", Semantics::LivingRoom)
|
273
|
+
#
|
274
|
+
# @param [Array] *tags An array of strings or Semantics classes
|
275
|
+
#
|
276
|
+
# @return [Set] A set of strings
|
277
|
+
#
|
278
|
+
def to_string_set(*tags)
|
279
|
+
tags = tags.flatten.map do |tag|
|
280
|
+
if tag.respond_to?(:java_class) && tag < org.openhab.core.semantics.Tag
|
281
|
+
tag.java_class.simple_name
|
282
|
+
else
|
283
|
+
tag.to_s
|
284
|
+
end
|
285
|
+
end
|
286
|
+
Set.new(tags)
|
287
|
+
end
|
288
|
+
|
266
289
|
#
|
267
290
|
# Create a new hash in which all elements are converted to strings
|
268
291
|
#
|
@@ -0,0 +1,133 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OpenHAB
|
4
|
+
module DSL
|
5
|
+
module Rules
|
6
|
+
# Contains helper methods for inferring a rule name from its triggers
|
7
|
+
module NameInference
|
8
|
+
# Trigger Type UIDs that we know how to generate a name for
|
9
|
+
KNOWN_TRIGGER_TYPES = [
|
10
|
+
'core.ChannelEventTrigger',
|
11
|
+
'core.GroupCommandTrigger',
|
12
|
+
'core.GroupStateChangeTrigger',
|
13
|
+
'core.GroupStateUpdateTrigger',
|
14
|
+
'core.ItemCommandTrigger',
|
15
|
+
'core.ItemStateChangeTrigger',
|
16
|
+
'core.ItemStateUpdateTrigger',
|
17
|
+
Triggers::Cron::CRON_TRIGGER_MODULE_ID
|
18
|
+
].freeze
|
19
|
+
private_constant :KNOWN_TRIGGER_TYPES
|
20
|
+
|
21
|
+
class << self
|
22
|
+
# get the block's source location, and simplify to a simple filename
|
23
|
+
def infer_rule_id_from_block(block)
|
24
|
+
file = File.basename(block.source_location.first)
|
25
|
+
"#{file}:#{block.source_location.last}"
|
26
|
+
end
|
27
|
+
|
28
|
+
# formulate a readable rule name such as "TestSwitch received command ON" if possible
|
29
|
+
def infer_rule_name(config) # rubocop:disable Metrics
|
30
|
+
known_triggers, unknown_triggers = config.triggers.partition do |t|
|
31
|
+
KNOWN_TRIGGER_TYPES.include?(t.type_uid)
|
32
|
+
end
|
33
|
+
return nil unless unknown_triggers.empty?
|
34
|
+
|
35
|
+
cron_triggers = known_triggers.select { |t| t.type_uid == 'jsr223.jruby.CronTrigger' }
|
36
|
+
ruby_every_triggers = config.ruby_triggers.select { |t| t.first == :every }
|
37
|
+
|
38
|
+
# makes sure there aren't any true cron triggers cause we can't format them
|
39
|
+
return nil unless cron_triggers.length == ruby_every_triggers.length
|
40
|
+
return nil unless config.ruby_triggers.length == 1
|
41
|
+
|
42
|
+
infer_rule_name_from_trigger(*config.ruby_triggers.first)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# formulate a readable rule name from a single trigger if possible
|
48
|
+
def infer_rule_name_from_trigger(trigger, items, kwargs)
|
49
|
+
case trigger
|
50
|
+
when :every
|
51
|
+
infer_rule_name_from_every_trigger(items, **kwargs)
|
52
|
+
when :channel
|
53
|
+
infer_rule_name_from_channel_trigger(items, **kwargs)
|
54
|
+
when :changed, :updated, :received_command
|
55
|
+
infer_rule_name_from_item_trigger(trigger, items, kwargs)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# formulate a readable rule name from an item-type trigger
|
60
|
+
def infer_rule_name_from_item_trigger(trigger, items, kwargs) # rubocop:disable Metrics
|
61
|
+
kwargs.delete(:command) if kwargs[:command] == [nil]
|
62
|
+
return unless items.length <= 3 &&
|
63
|
+
(kwargs.keys - %i[from to command duration]).empty?
|
64
|
+
return if kwargs.values_at(:from, :to, :command).compact.any? do |v|
|
65
|
+
next false if v.is_a?(Array) && v.length <= 4 # arbitrary length
|
66
|
+
next false if v.is_a?(Range)
|
67
|
+
|
68
|
+
v.is_a?(Proc) || v.is_a?(Enumerable)
|
69
|
+
end
|
70
|
+
|
71
|
+
trigger_name = trigger.to_s.tr('_', ' ')
|
72
|
+
item_names = items.map do |item|
|
73
|
+
if item.is_a?(GroupItem::GroupMembers)
|
74
|
+
"#{item.group.name}.members"
|
75
|
+
else
|
76
|
+
item.name
|
77
|
+
end
|
78
|
+
end
|
79
|
+
name = "#{format_beginning_of_sentence_array(item_names)} #{trigger_name}"
|
80
|
+
|
81
|
+
name += " from #{format_inspected_array(kwargs[:from])}" if kwargs[:from]
|
82
|
+
name += " to #{format_inspected_array(kwargs[:to])}" if kwargs[:to]
|
83
|
+
name += " #{format_inspected_array(kwargs[:command])}" if kwargs[:command]
|
84
|
+
name += " for #{kwargs[:duration]}" if kwargs[:duration]
|
85
|
+
name.freeze
|
86
|
+
end
|
87
|
+
|
88
|
+
# formulate a readable rule name from an every-style cron trigger
|
89
|
+
def infer_rule_name_from_every_trigger(value, at:)
|
90
|
+
name = "Every #{value}"
|
91
|
+
name += " at #{at}" if at
|
92
|
+
name
|
93
|
+
end
|
94
|
+
|
95
|
+
# formulate a readable rule name from a channel trigger
|
96
|
+
def infer_rule_name_from_channel_trigger(channels, triggers:)
|
97
|
+
triggers = [] if triggers == [nil]
|
98
|
+
name = "#{format_beginning_of_sentence_array(channels)} triggered"
|
99
|
+
name += " #{format_inspected_array(triggers)}" unless triggers.empty?
|
100
|
+
name
|
101
|
+
end
|
102
|
+
|
103
|
+
# format an array of words that will be the beginning of a sentence
|
104
|
+
def format_beginning_of_sentence_array(array)
|
105
|
+
result = format_array(array)
|
106
|
+
if array.length > 2
|
107
|
+
result = result.dup
|
108
|
+
result[0] = 'A'
|
109
|
+
result.freeze
|
110
|
+
end
|
111
|
+
result
|
112
|
+
end
|
113
|
+
|
114
|
+
# format an array of items that need to be inspected individually
|
115
|
+
def format_inspected_array(array)
|
116
|
+
return array.inspect if array.is_a?(Range)
|
117
|
+
|
118
|
+
array = [array] unless array.is_a?(Array)
|
119
|
+
format_array(array.map(&:inspect))
|
120
|
+
end
|
121
|
+
|
122
|
+
# format an array of words in a friendly way
|
123
|
+
def format_array(array)
|
124
|
+
return array[0] if array.length == 1
|
125
|
+
return "#{array[0]} or #{array[1]}" if array.length == 2
|
126
|
+
|
127
|
+
"any of #{array.join(', ')}"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -8,6 +8,7 @@ require 'openhab/log/logger'
|
|
8
8
|
require_relative 'rule_config'
|
9
9
|
require_relative 'automation_rule'
|
10
10
|
require_relative 'guard'
|
11
|
+
require_relative 'name_inference'
|
11
12
|
|
12
13
|
module OpenHAB
|
13
14
|
#
|
@@ -34,32 +35,30 @@ module OpenHAB
|
|
34
35
|
|
35
36
|
module_function
|
36
37
|
|
37
|
-
# get the block's source location, and simplify to a simple filename
|
38
|
-
def self.infer_rule_id_from_block(block)
|
39
|
-
file = File.basename(block.source_location.first)
|
40
|
-
"#{file}:#{block.source_location.last}"
|
41
|
-
end
|
42
|
-
|
43
38
|
#
|
44
39
|
# Create a new rule
|
45
40
|
#
|
46
|
-
# @param [String] rule_name
|
41
|
+
# @param [String] rule_name The rule name
|
47
42
|
# @yield [] Block executed in context of a RuleConfig
|
48
43
|
#
|
49
44
|
#
|
50
45
|
def rule(rule_name = nil, id: nil, script: nil, &block) # rubocop:disable Metrics
|
51
|
-
id ||=
|
52
|
-
rule_name ||= id
|
46
|
+
id ||= NameInference.infer_rule_id_from_block(block)
|
53
47
|
script ||= block.source rescue nil # rubocop:disable Style/RescueModifier
|
54
48
|
|
55
49
|
OpenHAB::Core::ThreadLocal.thread_local(RULE_NAME: rule_name) do
|
56
50
|
@rule_name = rule_name
|
57
51
|
|
58
|
-
config = RuleConfig.new(
|
52
|
+
config = RuleConfig.new(block.binding)
|
59
53
|
config.uid(id)
|
60
54
|
config.instance_exec(config, &block)
|
61
55
|
config.guard = Guard::Guard.new(run_context: config.caller, only_if: config.only_if,
|
62
56
|
not_if: config.not_if)
|
57
|
+
|
58
|
+
rule_name ||= NameInference.infer_rule_name(config)
|
59
|
+
rule_name ||= id
|
60
|
+
|
61
|
+
config.name(rule_name)
|
63
62
|
logger.trace { config.inspect }
|
64
63
|
process_rule_config(config, script)
|
65
64
|
end
|
@@ -89,7 +88,7 @@ module OpenHAB
|
|
89
88
|
rule = AutomationRule.new(config: config)
|
90
89
|
Rule.script_rules << rule
|
91
90
|
added_rule = add_rule(rule)
|
92
|
-
|
91
|
+
# add config so that MainUI can show the script
|
93
92
|
added_rule.actions.first.configuration.put('type', 'application/x-ruby')
|
94
93
|
added_rule.actions.first.configuration.put('script', script)
|
95
94
|
|
@@ -142,13 +141,13 @@ module OpenHAB
|
|
142
141
|
#
|
143
142
|
# Add a rule to the automation managed
|
144
143
|
#
|
145
|
-
# @param [
|
144
|
+
# @param [org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule] rule to add
|
146
145
|
#
|
147
146
|
#
|
148
147
|
def add_rule(rule)
|
149
148
|
base_uid = rule.uid
|
150
149
|
duplicate_index = 1
|
151
|
-
while $rules.get(rule.uid)
|
150
|
+
while $rules.get(rule.uid)
|
152
151
|
duplicate_index += 1
|
153
152
|
rule.uid = "#{base_uid} (#{duplicate_index})"
|
154
153
|
end
|
@@ -36,6 +36,9 @@ module OpenHAB
|
|
36
36
|
# @return [Object] object that invoked rule method
|
37
37
|
attr_accessor :caller
|
38
38
|
|
39
|
+
# @return [Array] Of trigger definitions as passed in Ruby
|
40
|
+
attr_reader :ruby_triggers
|
41
|
+
|
39
42
|
#
|
40
43
|
# Struct holding a run block
|
41
44
|
#
|
@@ -64,6 +67,7 @@ module OpenHAB
|
|
64
67
|
prop :uid
|
65
68
|
prop :name
|
66
69
|
prop :description
|
70
|
+
prop :tags
|
67
71
|
prop :enabled
|
68
72
|
prop :between
|
69
73
|
|
@@ -73,12 +77,13 @@ module OpenHAB
|
|
73
77
|
# @param [Object] caller_binding The object initializing this configuration.
|
74
78
|
# Used to execute within the object's context
|
75
79
|
#
|
76
|
-
def initialize(
|
80
|
+
def initialize(caller_binding)
|
77
81
|
@rule_triggers = RuleTriggers.new
|
78
82
|
@caller = caller_binding.eval 'self'
|
79
|
-
|
83
|
+
@ruby_triggers = []
|
80
84
|
enabled(true)
|
81
85
|
on_start(false)
|
86
|
+
tags([])
|
82
87
|
end
|
83
88
|
|
84
89
|
#
|
@@ -7,10 +7,8 @@ module OpenHAB
|
|
7
7
|
module TerseRule
|
8
8
|
%i[changed channel cron every updated received_command].each do |trigger|
|
9
9
|
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
10
|
-
def #{trigger}(*args, name: nil, **kwargs, &block)
|
11
|
-
|
12
|
-
id = Rule.infer_rule_id_from_block(block) # id = Rule.infer_rule_id_from_block(block)
|
13
|
-
name ||= id # name ||= id
|
10
|
+
def #{trigger}(*args, name: nil, id: nil, **kwargs, &block) # def changed(*args, name: nil, id: nil, **kwargs, &block)
|
11
|
+
id ||= NameInference.infer_rule_id_from_block(block) # id ||= NameInference.infer_rule_id_from_block(block)
|
14
12
|
script = block.source rescue nil # script = block.source rescue nil
|
15
13
|
rule name, id: id, script: script do # rule name, id: id, script: script do
|
16
14
|
#{trigger}(*args, **kwargs) # changed(*args, **kwargs)
|
@@ -20,31 +18,6 @@ module OpenHAB
|
|
20
18
|
module_function #{trigger.inspect} # module_function :changed
|
21
19
|
RUBY
|
22
20
|
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
# formulate a readable rule name such as "TestSwitch received command ON" if possible
|
27
|
-
def infer_rule_name(trigger, args, kwargs) # rubocop:disable Metrics
|
28
|
-
return unless %i[changed updated received_command].include?(trigger) &&
|
29
|
-
args.length == 1 &&
|
30
|
-
(kwargs.keys - %i[from to command]).empty?
|
31
|
-
return if kwargs[:from].is_a?(Enumerable)
|
32
|
-
return if kwargs[:to].is_a?(Enumerable)
|
33
|
-
return if kwargs[:command].is_a?(Enumerable)
|
34
|
-
|
35
|
-
trigger_name = trigger.to_s.tr('_', ' ')
|
36
|
-
name = if args.first.is_a?(GroupItem::GroupMembers) # rubocop:disable Style/CaseLikeIf === doesn't work with GenericItem
|
37
|
-
"#{args.first.group.name}.members #{trigger_name}"
|
38
|
-
elsif args.first.is_a?(GenericItem)
|
39
|
-
"#{args.first.name} #{trigger_name}"
|
40
|
-
end
|
41
|
-
return unless name
|
42
|
-
|
43
|
-
name += " from #{kwargs[:from].inspect}" if kwargs[:from]
|
44
|
-
name += " to #{kwargs[:to].inspect}" if kwargs[:to]
|
45
|
-
name += " #{kwargs[:command].inspect}" if kwargs[:command]
|
46
|
-
name
|
47
|
-
end
|
48
21
|
end
|
49
22
|
end
|
50
23
|
end
|
@@ -28,14 +28,16 @@ module OpenHAB
|
|
28
28
|
#
|
29
29
|
def changed(*items, to: nil, from: nil, for: nil, attach: nil)
|
30
30
|
changed = Changed.new(rule_triggers: @rule_triggers)
|
31
|
-
|
32
|
-
|
31
|
+
# for is a reserved word in ruby, so use local_variable_get :for
|
32
|
+
duration = binding.local_variable_get(:for)
|
33
33
|
|
34
|
-
|
35
|
-
|
34
|
+
flattened_items = Changed.flatten_items(items)
|
35
|
+
@ruby_triggers << [:changed, flattened_items, { to: to, from: from, duration: duration }]
|
36
|
+
flattened_items.map do |item|
|
37
|
+
logger.trace("Creating changed trigger for entity(#{item}), to(#{to}), from(#{from})")
|
36
38
|
|
37
39
|
Changed.each_state(from, to) do |from_state, to_state|
|
38
|
-
changed.trigger(item: item, from: from_state, to: to_state, duration:
|
40
|
+
changed.trigger(item: item, from: from_state, to: to_state, duration: duration, attach: attach)
|
39
41
|
end
|
40
42
|
end.flatten
|
41
43
|
end
|
@@ -26,8 +26,11 @@ module OpenHAB
|
|
26
26
|
#
|
27
27
|
def channel(*channels, thing: nil, triggered: nil, attach: nil)
|
28
28
|
channel_trigger = Channel.new(rule_triggers: @rule_triggers)
|
29
|
-
Channel.channels(channels: channels, thing: thing)
|
30
|
-
|
29
|
+
flattened_channels = Channel.channels(channels: channels, thing: thing)
|
30
|
+
triggers = [triggered].flatten
|
31
|
+
@ruby_triggers << [:channel, flattened_channels, { triggers: triggers }]
|
32
|
+
flattened_channels.each do |channel|
|
33
|
+
triggers.each do |trigger|
|
31
34
|
channel_trigger.trigger(channel: channel, trigger: trigger, attach: attach)
|
32
35
|
end
|
33
36
|
end
|
@@ -28,8 +28,10 @@ module OpenHAB
|
|
28
28
|
|
29
29
|
# Combine command and commands, doing union so only a single nil will be in the combined array.
|
30
30
|
combined_commands = Command.combine_commands(command: command, commands: commands)
|
31
|
+
flattened_items = Command.flatten_items(items)
|
32
|
+
@ruby_triggers << [:received_command, flattened_items, { command: combined_commands }]
|
31
33
|
|
32
|
-
|
34
|
+
flattened_items.map do |item|
|
33
35
|
combined_commands.map do |cmd|
|
34
36
|
logger.states 'Creating received command trigger', item: item, command: cmd
|
35
37
|
|
@@ -86,7 +86,7 @@ module OpenHAB
|
|
86
86
|
#
|
87
87
|
# Converts a ThingStatus object to a ruby Symbol
|
88
88
|
#
|
89
|
-
# @param [
|
89
|
+
# @param [org.openhab.core.thing.ThingStatus] status A ThingStatus instance
|
90
90
|
#
|
91
91
|
# @return [Symbol] A corresponding symbol, in lower case
|
92
92
|
#
|
@@ -21,10 +21,12 @@ module OpenHAB
|
|
21
21
|
def every(value, at: nil, attach: nil)
|
22
22
|
return every(MonthDay.parse(value), at: at, attach: attach) if value.is_a? String
|
23
23
|
|
24
|
+
@ruby_triggers << [:every, value, { at: at }]
|
25
|
+
|
24
26
|
cron_expression = case value
|
25
27
|
when Symbol then Cron.from_symbol(value, at)
|
26
|
-
when
|
27
|
-
when
|
28
|
+
when java.time.Duration then Cron.from_duration(value, at)
|
29
|
+
when java.time.MonthDay then Cron.from_monthday(value, at)
|
28
30
|
else raise ArgumentError, 'Unknown interval'
|
29
31
|
end
|
30
32
|
cron(cron_expression, attach: attach)
|
@@ -110,7 +112,7 @@ module OpenHAB
|
|
110
112
|
#
|
111
113
|
# Create a cron map from a duration
|
112
114
|
#
|
113
|
-
# @param [
|
115
|
+
# @param [java.time.Duration] duration
|
114
116
|
# @param [Object] at TimeOfDay or String representing time of day
|
115
117
|
#
|
116
118
|
# @return [Hash] map describing cron expression
|
@@ -124,7 +126,7 @@ module OpenHAB
|
|
124
126
|
#
|
125
127
|
# Create a cron map from a MonthDay
|
126
128
|
#
|
127
|
-
# @param [
|
129
|
+
# @param [java.time.MonthDay] monthday a {MonthDay} object
|
128
130
|
# @param [Object] at TimeOfDay or String representing time of day
|
129
131
|
#
|
130
132
|
# @return [Hash] map describing cron expression
|
@@ -176,7 +178,7 @@ module OpenHAB
|
|
176
178
|
#
|
177
179
|
# Convert a Java Duration to a map for the map_to_cron method
|
178
180
|
#
|
179
|
-
# @param duration [
|
181
|
+
# @param duration [java.time.Duration] The duration object
|
180
182
|
#
|
181
183
|
# @return [Hash] a map suitable for map_to_cron
|
182
184
|
#
|
@@ -23,7 +23,9 @@ module OpenHAB
|
|
23
23
|
#
|
24
24
|
def updated(*items, to: nil, attach: nil)
|
25
25
|
updated = Updated.new(rule_triggers: @rule_triggers)
|
26
|
-
Updated.flatten_items(items)
|
26
|
+
flattened_items = Updated.flatten_items(items)
|
27
|
+
@ruby_triggers << [:updated, flattened_items, { to: to }]
|
28
|
+
flattened_items.map do |item|
|
27
29
|
logger.trace("Creating updated trigger for item(#{item}) to(#{to})")
|
28
30
|
[to].flatten.map do |to_state|
|
29
31
|
updated.trigger(item: item, to: to_state, attach: attach)
|
data/lib/openhab/dsl/things.rb
CHANGED
@@ -108,7 +108,7 @@ module OpenHAB
|
|
108
108
|
# @return Thing specified by name/UID or nil if name/UID does not exist in thing registry
|
109
109
|
def [](uid)
|
110
110
|
uid = generate_thing_uid(uid) unless uid.is_a?(org.openhab.core.thing.ThingUID)
|
111
|
-
thing = $things.get(uid)
|
111
|
+
thing = $things.get(uid)
|
112
112
|
return unless thing
|
113
113
|
|
114
114
|
logger.trace("Retrieved Thing(#{thing}) from registry for uid: #{uid}")
|
@@ -119,7 +119,7 @@ module OpenHAB
|
|
119
119
|
|
120
120
|
# explicit conversion to array
|
121
121
|
def to_a
|
122
|
-
$things.getAll.map { |thing| Thing.new(thing) }
|
122
|
+
$things.getAll.map { |thing| Thing.new(thing) }
|
123
123
|
end
|
124
124
|
|
125
125
|
private
|
@@ -103,7 +103,7 @@ module OpenHAB
|
|
103
103
|
return unless timestamp
|
104
104
|
|
105
105
|
case timestamp
|
106
|
-
when
|
106
|
+
when java.time.temporal.TemporalAmount then ZonedDateTime.now.plus(timestamp)
|
107
107
|
when ZonedDateTime then timestamp
|
108
108
|
when Time then timestamp.to_java(ZonedDateTime)
|
109
109
|
else
|
@@ -10,7 +10,7 @@ module OpenHAB
|
|
10
10
|
DateTimeType = org.openhab.core.library.types.DateTimeType
|
11
11
|
|
12
12
|
# global alias - required for jrubyscripting addon <= OH3.2.0
|
13
|
-
::DateTimeType = DateTimeType if ::DateTimeType.is_a?(
|
13
|
+
::DateTimeType = DateTimeType if ::DateTimeType.is_a?(java.lang.Class)
|
14
14
|
|
15
15
|
# @deprecated
|
16
16
|
# Backwards-compatible alias
|
@@ -9,7 +9,7 @@ module OpenHAB
|
|
9
9
|
HSBType = org.openhab.core.library.types.HSBType
|
10
10
|
|
11
11
|
# global alias - required for jrubyscripting addon <= OH3.2.0
|
12
|
-
::HSBType = HSBType if ::HSBType.is_a?(
|
12
|
+
::HSBType = HSBType if ::HSBType.is_a?(java.lang.Class)
|
13
13
|
|
14
14
|
# Adds methods to core OpenHAB HSBType to make it more natural in Ruby
|
15
15
|
class HSBType < PercentType
|
@@ -8,7 +8,7 @@ module OpenHAB
|
|
8
8
|
PercentType = org.openhab.core.library.types.PercentType
|
9
9
|
|
10
10
|
# global alias - required for jrubyscripting addon <= OH3.2.0
|
11
|
-
::PercentType = PercentType if ::PercentType.is_a?(
|
11
|
+
::PercentType = PercentType if ::PercentType.is_a?(java.lang.Class)
|
12
12
|
|
13
13
|
# Adds methods to core OpenHAB PercentType to make it more natural in Ruby
|
14
14
|
class PercentType < DecimalType
|
@@ -7,7 +7,7 @@ module OpenHAB
|
|
7
7
|
|
8
8
|
# global scope - required for jrubyscripting addon <= OH3.2.0
|
9
9
|
# @!visibility private
|
10
|
-
::PointType = PointType if ::PointType.is_a?(
|
10
|
+
::PointType = PointType if ::PointType.is_a?(java.lang.Class)
|
11
11
|
|
12
12
|
# Adds methods to core OpenHAB PointType to make it more natural in Ruby
|
13
13
|
class PointType
|
@@ -8,7 +8,7 @@ module OpenHAB
|
|
8
8
|
QuantityType = org.openhab.core.library.types.QuantityType
|
9
9
|
|
10
10
|
# global alias - required for jrubyscripting addon <= OH3.2.0
|
11
|
-
::QuantityType = QuantityType if ::QuantityType.is_a?(
|
11
|
+
::QuantityType = QuantityType if ::QuantityType.is_a?(java.lang.Class)
|
12
12
|
|
13
13
|
# @deprecated
|
14
14
|
# Backwards-compatible alias
|
data/lib/openhab/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openhab-scripting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.44.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian O'Connell
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-07-
|
11
|
+
date: 2022-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- lib/openhab/dsl/rules/automation_rule.rb
|
117
117
|
- lib/openhab/dsl/rules/guard.rb
|
118
118
|
- lib/openhab/dsl/rules/item_event.rb
|
119
|
+
- lib/openhab/dsl/rules/name_inference.rb
|
119
120
|
- lib/openhab/dsl/rules/property.rb
|
120
121
|
- lib/openhab/dsl/rules/rule.rb
|
121
122
|
- lib/openhab/dsl/rules/rule_config.rb
|