openhab-scripting 2.10.1 → 2.13.1
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/dsl/items/items.rb +10 -0
- data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +42 -0
- data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +7 -35
- data/lib/openhab/core/dsl/rule/cron.rb +26 -3
- data/lib/openhab/core/dsl/rule/item.rb +1 -0
- data/lib/openhab/core/dsl/rule/rule.rb +19 -2
- data/lib/openhab/core/dsl/timers.rb +2 -4
- data/lib/openhab/version.rb +1 -1
- metadata +2 -3
- data/lib/openhab/core/duration.rb +0 -69
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '089e0d97f175f04c563675de122c80d0f5988be5ea38be8833736e4e73341a75'
|
4
|
+
data.tar.gz: 4b98810dd2e4f01772272fc10e457b57e81ade54c19b1dc59a72afd85c368b5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67abc23128c151fdeb509f7a6c770696e0221b6a41a6e8af36628c891aa959c72e7ed7123d1e6d9711da3fe862eba2adaac9e3692e1026193cc7638e11de926a
|
7
|
+
data.tar.gz: 4348d10e590f9e8179c3559c05ae2417cd9a75150d069e98f1566316e60a5d5ed12b4057b0d67272e97d92c813cbf337c97dac8d69b1cc5538b06576aa3084f7
|
@@ -22,7 +22,17 @@ module OpenHAB
|
|
22
22
|
item = $ir.getItem(name)
|
23
23
|
# rubocop: enable Style/GlobalVars
|
24
24
|
(item.is_a? GroupItem) ? nil : item
|
25
|
+
rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
|
26
|
+
nil
|
25
27
|
end
|
28
|
+
|
29
|
+
# Returns true if the given item name exists
|
30
|
+
# @param name [String] Item name to check
|
31
|
+
# @return [Boolean] true if the item exists, false otherwise
|
32
|
+
def include?(name)
|
33
|
+
!$ir.getItems(name).empty?
|
34
|
+
end
|
35
|
+
alias key? include?
|
26
36
|
end
|
27
37
|
|
28
38
|
java_import org.openhab.core.items.GroupItem
|
@@ -14,6 +14,7 @@ Dimmer = DimmerItem
|
|
14
14
|
#
|
15
15
|
# rubocop:disable Style/ClassAndModuleChildren
|
16
16
|
class Java::OrgOpenhabCoreLibraryItems::DimmerItem
|
17
|
+
include Comparable
|
17
18
|
# rubocop:enable Style/ClassAndModuleChildren
|
18
19
|
java_import org.openhab.core.library.types.DecimalType
|
19
20
|
java_import org.openhab.core.library.types.IncreaseDecreaseType
|
@@ -85,6 +86,38 @@ class Java::OrgOpenhabCoreLibraryItems::DimmerItem
|
|
85
86
|
target
|
86
87
|
end
|
87
88
|
|
89
|
+
#
|
90
|
+
# Compare DimmerItem to supplied object
|
91
|
+
#
|
92
|
+
# @param [Object] other object to compare to
|
93
|
+
#
|
94
|
+
# @return [Integer] -1,0,1 or nil depending on value supplied, nil comparison to supplied object is not possible.
|
95
|
+
#
|
96
|
+
def <=>(other)
|
97
|
+
logger.trace("Comparing #{self} to #{other}")
|
98
|
+
case other
|
99
|
+
when DimmerItem, NumberItem
|
100
|
+
state <=> other.state
|
101
|
+
when DecimalType
|
102
|
+
state <=> other
|
103
|
+
else
|
104
|
+
to_i <=> other.to_i
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
#
|
109
|
+
# Compare DimmerItem to supplied object.
|
110
|
+
# The == operator needs to be overridden because the parent java object
|
111
|
+
# has .equals which overrides the <=> operator above
|
112
|
+
#
|
113
|
+
# @param [Object] other object to compare to
|
114
|
+
#
|
115
|
+
# @return [Integer] true if the two objects contain the same value, false otherwise
|
116
|
+
#
|
117
|
+
def ==(other)
|
118
|
+
(self <=> other).zero?
|
119
|
+
end
|
120
|
+
|
88
121
|
#
|
89
122
|
# Check if dimmer has a state and state is not zero
|
90
123
|
#
|
@@ -105,6 +138,15 @@ class Java::OrgOpenhabCoreLibraryItems::DimmerItem
|
|
105
138
|
|
106
139
|
alias to_int to_i
|
107
140
|
|
141
|
+
#
|
142
|
+
# Return the string representation of the dimmer item
|
143
|
+
#
|
144
|
+
# @return [String] String version of the dimmer value
|
145
|
+
#
|
146
|
+
def to_s
|
147
|
+
to_i.to_s
|
148
|
+
end
|
149
|
+
|
108
150
|
#
|
109
151
|
# Check if dimmer is on
|
110
152
|
#
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'core/duration'
|
4
|
-
|
5
3
|
module OpenHAB
|
6
4
|
module Core
|
7
5
|
module DSL
|
@@ -14,44 +12,18 @@ module OpenHAB
|
|
14
12
|
include OpenHAB::Core
|
15
13
|
|
16
14
|
#
|
17
|
-
# Create Duration with
|
18
|
-
#
|
19
|
-
# @return [OpenHAB::Core::Duration] Duration with number of seconds from self
|
20
|
-
#
|
21
|
-
def seconds
|
22
|
-
Duration.new(temporal_unit: :SECONDS, amount: self)
|
23
|
-
end
|
24
|
-
|
25
|
-
#
|
26
|
-
# Create Duration with unit of milliseconds
|
27
|
-
#
|
28
|
-
# @return [OpenHAB::Core::Duration] Duration with number of milliseconds from self
|
29
|
-
#
|
30
|
-
def milliseconds
|
31
|
-
Duration.new(temporal_unit: :MILLISECONDS, amount: self)
|
32
|
-
end
|
33
|
-
|
34
|
-
#
|
35
|
-
# Create Duration with unit of minutes
|
36
|
-
#
|
37
|
-
# @return [OpenHAB::Core::Duration] Duration with number of minutes from self
|
38
|
-
#
|
39
|
-
def minutes
|
40
|
-
Duration.new(temporal_unit: :MINUTES, amount: self)
|
41
|
-
end
|
42
|
-
|
43
|
-
#
|
44
|
-
# Create Duration with unit of hours
|
15
|
+
# Create Duration with the specified unit
|
45
16
|
#
|
46
|
-
# @return [
|
17
|
+
# @return [Java::JavaTime::Duration] Duration with number of units from self
|
47
18
|
#
|
48
|
-
|
49
|
-
Duration.
|
19
|
+
%w[millis seconds minutes hours].each do |unit|
|
20
|
+
define_method(unit) { Java::JavaTime::Duration.public_send("of_#{unit}", self) }
|
50
21
|
end
|
51
22
|
|
52
23
|
alias second seconds
|
53
|
-
alias millisecond
|
54
|
-
alias
|
24
|
+
alias millisecond millis
|
25
|
+
alias milliseconds millis
|
26
|
+
alias ms millis
|
55
27
|
alias minute minutes
|
56
28
|
alias hour hours
|
57
29
|
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'java'
|
4
|
-
require 'core/duration'
|
5
4
|
require 'core/dsl/time_of_day'
|
6
5
|
require 'core/cron'
|
7
6
|
|
@@ -64,8 +63,10 @@ module OpenHAB
|
|
64
63
|
expression_map = EXPRESSION_MAP[value]
|
65
64
|
expression_map = at_condition(expression_map, at) if at
|
66
65
|
cron(map_to_cron(expression_map))
|
67
|
-
when Duration
|
68
|
-
|
66
|
+
when Java::JavaTime::Duration
|
67
|
+
raise ArgumentError, '"at" cannot be used with duration' if at
|
68
|
+
|
69
|
+
cron(map_to_cron(duration_to_map(value)))
|
69
70
|
else
|
70
71
|
raise ArgumentExpression, 'Unknown interval' unless expression_map
|
71
72
|
end
|
@@ -93,6 +94,28 @@ module OpenHAB
|
|
93
94
|
%i[second minute hour dom month dow].map { |field| map.fetch(field) }.join(' ')
|
94
95
|
end
|
95
96
|
|
97
|
+
#
|
98
|
+
# Convert a Java Duration to a map for the map_to_cron method
|
99
|
+
#
|
100
|
+
# @param duration [Java::JavaTime::Duration] The duration object
|
101
|
+
#
|
102
|
+
# @return [Hash] a map suitable for map_to_cron
|
103
|
+
#
|
104
|
+
def duration_to_map(duration)
|
105
|
+
if duration.to_millis_part.zero? && duration.to_nanos_part.zero? && duration.to_days.zero?
|
106
|
+
%i[second minute hour].each do |unit|
|
107
|
+
to_unit_part = duration.public_send("to_#{unit}s_part")
|
108
|
+
next unless to_unit_part.positive?
|
109
|
+
|
110
|
+
to_unit = duration.public_send("to_#{unit}s")
|
111
|
+
break unless to_unit_part == to_unit
|
112
|
+
|
113
|
+
return EXPRESSION_MAP[unit].merge(unit => "*/#{to_unit}")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
raise ArgumentError, "Cron Expression not supported for duration: #{duration}"
|
117
|
+
end
|
118
|
+
|
96
119
|
#
|
97
120
|
# If an at time is provided, parse that and merge the new fields into the expression.
|
98
121
|
#
|
@@ -274,7 +274,7 @@ module OpenHAB
|
|
274
274
|
process_trigger_delay(mod, inputs)
|
275
275
|
else
|
276
276
|
logger.trace("Item changed to #{state} for #{trigger_delay}, rescheduling timer.")
|
277
|
-
trigger_delay.timer.reschedule(ZonedDateTime.now.plus(
|
277
|
+
trigger_delay.timer.reschedule(ZonedDateTime.now.plus(duration))
|
278
278
|
end
|
279
279
|
end
|
280
280
|
else
|
@@ -303,6 +303,23 @@ module OpenHAB
|
|
303
303
|
false
|
304
304
|
end
|
305
305
|
|
306
|
+
#
|
307
|
+
# Patch event to include event.item when it doesn't exist
|
308
|
+
# This is to patch a bug see https://github.com/boc-tothefuture/openhab-jruby/issues/75
|
309
|
+
# It may be fixed in the openhab core in the future, in which case, this patch will no longer be necessary
|
310
|
+
#
|
311
|
+
# @param [OpenHAB Event] event to check for item accessor
|
312
|
+
# @param [OpenHAB Event Inputs] inputs inputs to running rule
|
313
|
+
#
|
314
|
+
def add_event_item(event, inputs)
|
315
|
+
return if event.nil? || defined?(event.item)
|
316
|
+
|
317
|
+
class << event
|
318
|
+
attr_accessor :item
|
319
|
+
end
|
320
|
+
event.item = inputs&.dig('triggeringItem')
|
321
|
+
end
|
322
|
+
|
306
323
|
#
|
307
324
|
# Process the run queue
|
308
325
|
#
|
@@ -317,7 +334,7 @@ module OpenHAB
|
|
317
334
|
when RuleConfig::Run
|
318
335
|
|
319
336
|
event = inputs&.dig('event')
|
320
|
-
|
337
|
+
add_event_item(event, inputs)
|
321
338
|
logger.trace { "Executing rule '#{name}' run block with event(#{event})" }
|
322
339
|
task.block.call(event)
|
323
340
|
when RuleConfig::Trigger
|
@@ -4,8 +4,6 @@ require 'java'
|
|
4
4
|
require 'delegate'
|
5
5
|
require 'forwardable'
|
6
6
|
|
7
|
-
require 'core/duration'
|
8
|
-
|
9
7
|
module OpenHAB
|
10
8
|
module Core
|
11
9
|
module DSL
|
@@ -49,7 +47,7 @@ module OpenHAB
|
|
49
47
|
|
50
48
|
semaphore.synchronize do
|
51
49
|
@timer = ScriptExecution.createTimer(
|
52
|
-
ZonedDateTime.now.plus(
|
50
|
+
ZonedDateTime.now.plus(@duration), @block
|
53
51
|
)
|
54
52
|
super(@timer)
|
55
53
|
end
|
@@ -64,7 +62,7 @@ module OpenHAB
|
|
64
62
|
#
|
65
63
|
def reschedule(duration = nil)
|
66
64
|
duration ||= @duration
|
67
|
-
@timer.reschedule(ZonedDateTime.now.plus(
|
65
|
+
@timer.reschedule(ZonedDateTime.now.plus(duration))
|
68
66
|
end
|
69
67
|
end
|
70
68
|
|
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: 2.
|
4
|
+
version: 2.13.1
|
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: 2021-
|
11
|
+
date: 2021-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,7 +76,6 @@ files:
|
|
76
76
|
- lib/openhab/core/dsl/timers.rb
|
77
77
|
- lib/openhab/core/dsl/types/quantity.rb
|
78
78
|
- lib/openhab/core/dsl/units.rb
|
79
|
-
- lib/openhab/core/duration.rb
|
80
79
|
- lib/openhab/core/log.rb
|
81
80
|
- lib/openhab/core/patch_load_path.rb
|
82
81
|
- lib/openhab/core/startup_delay.rb
|
@@ -1,69 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'openhab/core/cron'
|
4
|
-
|
5
|
-
module OpenHAB
|
6
|
-
module Core
|
7
|
-
#
|
8
|
-
# This class represents a duration of time
|
9
|
-
#
|
10
|
-
class Duration
|
11
|
-
include OpenHAB::Core::Cron
|
12
|
-
|
13
|
-
# @return [Array] of supported temperal units (milliseconds, seconds, minutes and hours)
|
14
|
-
TEMPORAL_UNITS = %i[MILLISECONDS SECONDS MINUTES HOURS].freeze
|
15
|
-
|
16
|
-
#
|
17
|
-
# Create a new Duration object
|
18
|
-
#
|
19
|
-
# @param [Symbol] temporal_unit Unit for duration
|
20
|
-
# @param [Integer] amount of that unit
|
21
|
-
#
|
22
|
-
def initialize(temporal_unit:, amount:)
|
23
|
-
unless TEMPORAL_UNITS.include? temporal_unit
|
24
|
-
raise ArgumentError,
|
25
|
-
"Unexpected Temporal Unit: #{temporal_unit}"
|
26
|
-
end
|
27
|
-
|
28
|
-
@temporal_unit = temporal_unit
|
29
|
-
@amount = amount
|
30
|
-
end
|
31
|
-
|
32
|
-
#
|
33
|
-
# Return a map
|
34
|
-
#
|
35
|
-
# @return [Map] Map with fields representing this duration @see OpenHAB::Core::Cron
|
36
|
-
#
|
37
|
-
def cron_map
|
38
|
-
case @temporal_unit
|
39
|
-
when :SECONDS
|
40
|
-
cron_expression_map.merge(second: "*/#{@amount}")
|
41
|
-
when :MINUTES
|
42
|
-
cron_expression_map.merge(minute: "*/#{@amount}")
|
43
|
-
when :HOURS
|
44
|
-
cron_expression_map.merge(hour: "*/#{@amount}")
|
45
|
-
else
|
46
|
-
raise ArgumentError, "Cron Expression not supported for temporal unit: #{temporal_unit}"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
#
|
51
|
-
# Convert the duration to milliseconds
|
52
|
-
#
|
53
|
-
# @return [Integer] Duration in milliseconds
|
54
|
-
#
|
55
|
-
def to_ms
|
56
|
-
case @temporal_unit
|
57
|
-
when :MILLISECONDS
|
58
|
-
@amount
|
59
|
-
when :SECONDS
|
60
|
-
@amount * 1000
|
61
|
-
when :MINUTES
|
62
|
-
@amount * 1000 * 60
|
63
|
-
when :HOURS
|
64
|
-
@amount * 1000 * 60 * 60
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|