openhab-scripting 4.43.0 → 4.43.3
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 +1 -3
- 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 +0 -4
- data/lib/openhab/dsl/group.rb +1 -1
- data/lib/openhab/dsl/items/dimmer_item.rb +8 -0
- data/lib/openhab/dsl/items/item_registry.rb +2 -2
- data/lib/openhab/dsl/items/numeric_item.rb +2 -7
- data/lib/openhab/dsl/items/rollershutter_item.rb +8 -0
- 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/rules/automation_rule.rb +4 -1
- data/lib/openhab/dsl/rules/rule.rb +26 -5
- data/lib/openhab/dsl/rules/rule_config.rb +1 -0
- data/lib/openhab/dsl/rules/terse.rb +36 -9
- data/lib/openhab/dsl/things.rb +2 -2
- data/lib/openhab/dsl/types/decimal_type.rb +2 -2
- data/lib/openhab/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70ffac6f2b96c4cd2c758abaf5ca9b7ebedd83e7a1a520cd4fa971a4104ffc0a
|
4
|
+
data.tar.gz: a1738c2713e5b7d444e785be6e2d990fc58a548bb0487e9677aeafb6cfb61f98
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce3b43639725c24ad4109850f7bdea1e395c2e94dc63ff924458ad5788bc9453431262af1e779f9f1ab61cfd2771dcc66c24f0e739e271141f667b00b3a1b8e7
|
7
|
+
data.tar.gz: 856d1f63ea019d7e07791f7c0bcc588659d53295472edb3c7fa04a655ded3baf78027054b9381fa1d7cbc8aa17dc31862c6f67399be1d92a134f8c37a3160899
|
@@ -73,9 +73,7 @@ module OpenHAB
|
|
73
73
|
return if name.count('_') < 3
|
74
74
|
|
75
75
|
name = name.tr('_', ':')
|
76
|
-
# rubocop: disable Style/GlobalVars
|
77
76
|
$things.get(Java::OrgOpenhabCoreThing::ThingUID.new(name))
|
78
|
-
# rubocop: enable Style/GlobalVars
|
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,9 +29,7 @@ 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
|
#
|
@@ -43,9 +41,7 @@ module OpenHAB
|
|
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
@@ -51,6 +51,14 @@ module OpenHAB
|
|
51
51
|
# @!method decrease
|
52
52
|
# Send the +DECREASE+ command to the item
|
53
53
|
# @return [DimmerItem] +self+
|
54
|
+
|
55
|
+
# raw numbers translate directly to PercentType, not a DecimalType
|
56
|
+
# @!visibility private
|
57
|
+
def format_type(command)
|
58
|
+
return Types::PercentType.new(command) if command.is_a?(Numeric)
|
59
|
+
|
60
|
+
super
|
61
|
+
end
|
54
62
|
end
|
55
63
|
end
|
56
64
|
end
|
@@ -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
|
@@ -64,15 +64,10 @@ module OpenHAB
|
|
64
64
|
return [other, state] if other.is_a?(Types::NumericType) || other.respond_to?(:to_d)
|
65
65
|
end
|
66
66
|
|
67
|
-
#
|
67
|
+
# raw numbers translate directly to DecimalType, not a string
|
68
68
|
# @!visibility private
|
69
69
|
def format_type(command)
|
70
|
-
|
71
|
-
if command.instance_of?(Types::DecimalType) || command.instance_of?(Types::PercentType)
|
72
|
-
return command.to_big_decimal.strip_trailing_zeros.to_plain_string
|
73
|
-
end
|
74
|
-
# BigDecimal types have trailing zeros stripped
|
75
|
-
return command.to_java.strip_trailing_zeros.to_plain_string if command.is_a?(BigDecimal)
|
70
|
+
return Types::DecimalType.new(command) if command.is_a?(Numeric)
|
76
71
|
|
77
72
|
super
|
78
73
|
end
|
@@ -37,6 +37,14 @@ module OpenHAB
|
|
37
37
|
# @!method move
|
38
38
|
# Send the +MOVE+ command to the item
|
39
39
|
# @return [RollershutterItem] +self+
|
40
|
+
|
41
|
+
# raw numbers translate directly to PercentType, not a DecimalType
|
42
|
+
# @!visibility private
|
43
|
+
def format_type(command)
|
44
|
+
return Types::PercentType.new(command) if command.is_a?(Numeric)
|
45
|
+
|
46
|
+
super
|
47
|
+
end
|
40
48
|
end
|
41
49
|
end
|
42
50
|
end
|
@@ -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
|
#
|
@@ -24,18 +24,21 @@ module OpenHAB
|
|
24
24
|
include OpenHAB::Core::ThreadLocal
|
25
25
|
include OpenHAB::DSL::Between
|
26
26
|
|
27
|
+
field_writer :uid
|
28
|
+
|
27
29
|
#
|
28
30
|
# Create a new Rule
|
29
31
|
#
|
30
32
|
# @param [Config] config Rule configuration
|
31
33
|
#
|
32
34
|
# Constructor sets a number of variables, no further decomposition necessary
|
33
|
-
def initialize(config:) # rubocop:disable Metrics
|
35
|
+
def initialize(config:) # rubocop:disable Metrics
|
34
36
|
# Metrics disabled because only setters are called or defaults set.
|
35
37
|
super()
|
36
38
|
set_name(config.name)
|
37
39
|
set_description(config.description)
|
38
40
|
set_triggers(config.triggers)
|
41
|
+
self.uid = config.uid
|
39
42
|
@run_context = config.caller
|
40
43
|
@run_queue = config.run
|
41
44
|
@guard = config.guard
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'method_source'
|
4
|
+
|
3
5
|
require 'openhab/core/thread_local'
|
4
6
|
require 'openhab/core/services'
|
5
7
|
require 'openhab/log/logger'
|
@@ -32,6 +34,12 @@ module OpenHAB
|
|
32
34
|
|
33
35
|
module_function
|
34
36
|
|
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
|
+
|
35
43
|
#
|
36
44
|
# Create a new rule
|
37
45
|
#
|
@@ -39,21 +47,25 @@ module OpenHAB
|
|
39
47
|
# @yield [] Block executed in context of a RuleConfig
|
40
48
|
#
|
41
49
|
#
|
42
|
-
# rubocop:disable Metrics
|
43
|
-
|
50
|
+
def rule(rule_name = nil, id: nil, script: nil, &block) # rubocop:disable Metrics
|
51
|
+
id ||= Rule.infer_rule_id_from_block(block)
|
52
|
+
rule_name ||= id
|
53
|
+
script ||= block.source rescue nil # rubocop:disable Style/RescueModifier
|
54
|
+
|
44
55
|
OpenHAB::Core::ThreadLocal.thread_local(RULE_NAME: rule_name) do
|
45
56
|
@rule_name = rule_name
|
57
|
+
|
46
58
|
config = RuleConfig.new(rule_name, block.binding)
|
59
|
+
config.uid(id)
|
47
60
|
config.instance_exec(config, &block)
|
48
61
|
config.guard = Guard::Guard.new(run_context: config.caller, only_if: config.only_if,
|
49
62
|
not_if: config.not_if)
|
50
63
|
logger.trace { config.inspect }
|
51
|
-
process_rule_config(config)
|
64
|
+
process_rule_config(config, script)
|
52
65
|
end
|
53
66
|
rescue StandardError => e
|
54
67
|
logger.log_exception(e, @rule_name)
|
55
68
|
end
|
56
|
-
# rubocop:enable Metrics/MethodLength
|
57
69
|
|
58
70
|
#
|
59
71
|
# Cleanup rules in this script file
|
@@ -71,12 +83,15 @@ module OpenHAB
|
|
71
83
|
# @param [RuleConfig] config for rule
|
72
84
|
#
|
73
85
|
#
|
74
|
-
def process_rule_config(config)
|
86
|
+
def process_rule_config(config, script) # rubocop:disable Metrics
|
75
87
|
return unless create_rule?(config)
|
76
88
|
|
77
89
|
rule = AutomationRule.new(config: config)
|
78
90
|
Rule.script_rules << rule
|
79
91
|
added_rule = add_rule(rule)
|
92
|
+
# add config so that MainUI can show the script
|
93
|
+
added_rule.actions.first.configuration.put('type', 'application/x-ruby')
|
94
|
+
added_rule.actions.first.configuration.put('script', script)
|
80
95
|
|
81
96
|
rule.execute(nil, { 'event' => Struct.new(:attachment).new(config.start_attachment) }) if config.on_start?
|
82
97
|
added_rule
|
@@ -131,6 +146,12 @@ module OpenHAB
|
|
131
146
|
#
|
132
147
|
#
|
133
148
|
def add_rule(rule)
|
149
|
+
base_uid = rule.uid
|
150
|
+
duplicate_index = 1
|
151
|
+
while $rules.get(rule.uid)
|
152
|
+
duplicate_index += 1
|
153
|
+
rule.uid = "#{base_uid} (#{duplicate_index})"
|
154
|
+
end
|
134
155
|
logger.trace("Adding rule: #{rule.inspect}")
|
135
156
|
Rule.automation_manager.addRule(rule)
|
136
157
|
end
|
@@ -7,17 +7,44 @@ 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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
def #{trigger}(*args, name: nil, **kwargs, &block) # def changed(*args, name: nil, **kwargs, &block)
|
11
|
+
name ||= infer_rule_name(#{trigger.inspect}, args, kwargs) # name ||= infer_rule_name(:changed, args, kwargs)
|
12
|
+
id = Rule.infer_rule_id_from_block(block) # id = Rule.infer_rule_id_from_block(block)
|
13
|
+
name ||= id # name ||= id
|
14
|
+
script = block.source rescue nil # script = block.source rescue nil
|
15
|
+
rule name, id: id, script: script do # rule name, id: id, script: script do
|
16
|
+
#{trigger}(*args, **kwargs) # changed(*args, **kwargs)
|
17
|
+
run(&block) # run(&block)
|
18
|
+
end # end
|
19
|
+
end # end
|
20
|
+
module_function #{trigger.inspect} # module_function :changed
|
19
21
|
RUBY
|
20
22
|
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
|
21
48
|
end
|
22
49
|
end
|
23
50
|
end
|
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
|
@@ -36,14 +36,14 @@ module OpenHAB
|
|
36
36
|
if value.is_a?(java.math.BigDecimal)
|
37
37
|
super
|
38
38
|
elsif value.is_a?(BigDecimal)
|
39
|
-
super(value.to_java)
|
39
|
+
super(value.to_java.strip_trailing_zeros)
|
40
40
|
elsif value.is_a?(DecimalType)
|
41
41
|
super(value.to_big_decimal)
|
42
42
|
elsif value.is_a?(Items::NumericItem) ||
|
43
43
|
(value.is_a?(Items::GroupItem) && value.base_item.is_a?(Items::NumericItem))
|
44
44
|
super(value.state)
|
45
45
|
elsif value.respond_to?(:to_d)
|
46
|
-
super(value.to_d.to_java)
|
46
|
+
super(value.to_d.to_java.strip_trailing_zeros)
|
47
47
|
else # rubocop:disable Lint/DuplicateBranch
|
48
48
|
# duplicates the Java BigDecimal branch, but that needs to go first
|
49
49
|
# in order to avoid unnecessary conversions
|
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.43.
|
4
|
+
version: 4.43.3
|
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-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: method_source
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
41
55
|
description: JRuby Helper Libraries for OpenHAB Scripting
|
42
56
|
email:
|
43
57
|
- broconne@gmail.com
|