openhab-scripting 3.9.2 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 56b3593e694d3bc367977cdd3514712c452977d4644cb59610e3e25e4c7e651b
4
- data.tar.gz: 0c45d6f6c9c4e493f7baca477e88cb8ec0ab3a3325fad60926d987875711ef62
3
+ metadata.gz: 4028b9fda2ff3ffa7005f730d7b876e136cf4c3dacc66f0f8950077c4a8f5ab9
4
+ data.tar.gz: dc819dea7d3f573299934da843ddadc597f98f59165426e6c88e321a1fd93200
5
5
  SHA512:
6
- metadata.gz: 806e64e7bb07ff62fb8d99b3ad10505733340482b28c3c9e867b08fe0e40c2a43de586b56ca019cbc5b2408b57b4175c947e54d4020b20c6a8494791ab62cfc5
7
- data.tar.gz: 7285604ad3b6898441b7290143893d7488414ef91809b676639fb0ad100049305d15e48c95e1d6ad0afe4f98541d7c5e0f55070157b8675c5839cc2fe8b441fd
6
+ metadata.gz: 2839736ff1c0d15c6622e679e7ada29d6f34afe5bae7d9fa375ebe34e809b14eb0f54b447d95ff7548b3f97786f0c7f6f979cd8ce796fa84bdbb1b60312a7e73
7
+ data.tar.gz: 45b0ca416f63b8971fdd4f7ee7d86c449ac44b0d0acdf6a1ce6955d604c04bf12788900a180fde7ec49d9fafe8aa5a5b6a8d4271cc5a72ab4440ddbe9e7a2b8a
@@ -129,7 +129,7 @@ module OpenHAB
129
129
  # Thing UIDs have at least 3 segements
130
130
  return if name.count('_') < 3
131
131
 
132
- name = name.gsub('_', ':')
132
+ name = name.tr('_', ':')
133
133
  # rubocop: disable Style/GlobalVars
134
134
  $things.get(Java::OrgOpenhabCoreThing::ThingUID.new(name))
135
135
  # rubocop: enable Style/GlobalVars
@@ -13,7 +13,9 @@ module OpenHAB
13
13
  #
14
14
  # Class for indicating to triggers that a group trigger should be used
15
15
  #
16
- class GroupMembers < SimpleDelegator
16
+ class GroupMembers
17
+ include Enumerable
18
+
17
19
  attr_reader :group
18
20
 
19
21
  #
@@ -23,8 +25,24 @@ module OpenHAB
23
25
  #
24
26
  def initialize(group_item)
25
27
  @group = group_item
26
- super(OpenHAB::Core::EntityLookup.decorate_items(@group.members.to_a))
27
28
  end
29
+
30
+ # Calls the given block once for each group member, passing that
31
+ # item as a parameter. Returns self.
32
+ #
33
+ # If no block is given, an Enumerator is returned.
34
+ def each(&block)
35
+ to_a.each(&block)
36
+ self
37
+ end
38
+
39
+ # explicit conversion to array
40
+ # more efficient than letting Enumerable do it
41
+ def to_a
42
+ OpenHAB::Core::EntityLookup.decorate_items(group.members.to_a)
43
+ end
44
+ # implicitly convertible to array
45
+ alias to_ary to_a
28
46
  end
29
47
 
30
48
  #
@@ -68,12 +86,13 @@ module OpenHAB
68
86
  def members
69
87
  GroupMembers.new(@group_item)
70
88
  end
89
+ alias items members
71
90
 
72
91
  #
73
92
  # Iterates through the direct members of the Group
74
93
  #
75
94
  def each(&block)
76
- OpenHAB::Core::EntityLookup.decorate_items(@group_item.members.to_a).each(&block)
95
+ members.each(&block)
77
96
  end
78
97
 
79
98
  #
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'java'
4
4
  require 'openhab/log/logger'
5
+ require 'openhab/dsl/monkey_patch/events/item_command'
5
6
 
6
7
  module OpenHAB
7
8
  module DSL
@@ -28,7 +29,7 @@ module OpenHAB
28
29
  command_enum.values.each do |command|
29
30
  command_method = command.to_s.downcase
30
31
  command_method = methods.transform_keys(&:to_sym).fetch(command_method.to_sym, command_method)
31
- logger.trace("Creating command method (#{command_method}) for #{self.class}")
32
+ logger.trace("Creating command method (#{command_method}) for #{self}")
32
33
  define_method(command_method) do
33
34
  self.command(command)
34
35
  end
@@ -54,7 +55,7 @@ module OpenHAB
54
55
  command_enum.values.each do |command|
55
56
  status_method = "#{command.to_s.downcase}?"
56
57
  status_method = methods.transform_keys(&:to_sym).fetch(status_method.to_sym, status_method)
57
- logger.trace("Creating status method (#{status_method}) for #{self.class}")
58
+ logger.trace("Creating status method (#{status_method}) for #{self}")
58
59
  define_method(status_method) do
59
60
  state? && state.as(command_enum) == command
60
61
  end
@@ -70,7 +71,7 @@ module OpenHAB
70
71
  # @param [Hash] optional hash in which if a generated method name mactches a key, the value of that key
71
72
  # will be used as the method name instead, for example `:play? => :playing?`
72
73
  #
73
- def item_type(item_class, methods = {})
74
+ def item_type(item_class, methods = {}) # rubocop:disable Metrics/MethodLength
74
75
  item_class.field_reader(:ACCEPTED_DATA_TYPES)
75
76
  item_class.field_reader(:ACCEPTED_COMMAND_TYPES)
76
77
  item_class.ACCEPTED_DATA_TYPES.select(&:is_enum)
@@ -78,7 +79,10 @@ module OpenHAB
78
79
  .each { |type| item_state(type.ruby_class, methods) }
79
80
  item_class.ACCEPTED_COMMAND_TYPES.select(&:is_enum)
80
81
  .grep_v(UnDefType)
81
- .each { |type| item_command(type.ruby_class, methods) }
82
+ .each do |type|
83
+ item_command(type.ruby_class, methods)
84
+ MonkeyPatch::Events::ItemCommandEvent.def_enum_predicates(type.ruby_class)
85
+ end
82
86
  end
83
87
  end
84
88
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'java'
4
4
  require 'openhab/core/entity_lookup'
5
+ require 'singleton'
5
6
 
6
7
  module OpenHAB
7
8
  module DSL
@@ -12,11 +13,14 @@ module OpenHAB
12
13
  #
13
14
  # Delegates to underlying set of all OpenHAB Items, provides convenience methods
14
15
  #
15
- class Items < SimpleDelegator
16
+ class Items
17
+ include Enumerable
18
+ include Singleton
19
+
16
20
  # Fetches the named item from the the ItemRegistry
17
21
  # @param [String] name
18
22
  # @return Item from registry, nil if item missing or requested item is a Group Type
19
- def[](name)
23
+ def [](name)
20
24
  OpenHAB::Core::EntityLookup.lookup_item(name)
21
25
  rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
22
26
  nil
@@ -26,19 +30,35 @@ module OpenHAB
26
30
  # @param name [String] Item name to check
27
31
  # @return [Boolean] true if the item exists, false otherwise
28
32
  def include?(name)
29
- # rubocop: disable Style/GlobalVars
30
- !$ir.getItems(name).empty?
31
- # rubocop: enable Style/GlobalVars
33
+ !$ir.getItems(name).empty? # rubocop: disable Style/GlobalVars
32
34
  end
33
35
  alias key? include?
36
+
37
+ # Calls the given block once for each Item, passing that Item as a
38
+ # parameter. Returns self.
39
+ #
40
+ # If no block is given, an Enumerator is returned.
41
+ def each(&block)
42
+ # ideally we would do this lazily, but until ruby 2.7
43
+ # there's no #eager method to convert back to a non-lazy
44
+ # enumerator
45
+ to_a.each(&block)
46
+ end
47
+
48
+ # explicit conversion to array
49
+ # more efficient than letting Enumerable do it
50
+ def to_a
51
+ items = $ir.items.grep_v(Java::OrgOpenhabCoreItems::GroupItem) # rubocop:disable Style/GlobalVars
52
+ OpenHAB::Core::EntityLookup.decorate_items(items)
53
+ end
54
+ # implicitly convertible to array
55
+ alias to_ary to_a
34
56
  end
35
57
 
36
58
  # Fetches all non-group items from the item registry
37
59
  # @return [OpenHAB::DSL::Items::Items]
38
60
  def items
39
- # rubocop: disable Style/GlobalVars
40
- Items.new(OpenHAB::Core::EntityLookup.decorate_items($ir.items.grep_v(Java::OrgOpenhabCoreItems::GroupItem)))
41
- # rubocop: enable Style/GlobalVars
61
+ Items.instance
42
62
  end
43
63
  end
44
64
  end
@@ -15,7 +15,26 @@ module OpenHAB
15
15
  # Monkey patch with ruby style accesors
16
16
  #
17
17
  class ItemCommandEvent
18
+ include Log
19
+
18
20
  alias command item_command
21
+
22
+ #
23
+ # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
24
+ # string representation of the enum value For example, an enum with values of STOP and START
25
+ # would create methods stop? and start? that check if the command is corresponding STOP and START type
26
+ #
27
+ # @param [Java::JavaLang::Enum] command_enum Enumeration to create commands for
28
+ #
29
+ def self.def_enum_predicates(command_enum)
30
+ command_enum.values.each do |command| # rubocop:disable Style/HashEachMethods : Java enum does not have each_value
31
+ command_method = "#{command.to_s.downcase}?"
32
+ logger.trace("Creating predicate method (#{command_method}) for #{self}")
33
+ define_method(command_method) do
34
+ self.command == command
35
+ end
36
+ end
37
+ end
19
38
  end
20
39
  end
21
40
  end
@@ -10,12 +10,51 @@ module OpenHAB
10
10
  #
11
11
  module Events
12
12
  java_import Java::OrgOpenhabCoreItemsEvents::ItemStateEvent
13
+ java_import Java::OrgOpenhabCoreTypes::UnDefType
14
+
15
+ # Helpers common to ItemStateEvent and ItemStateChangedEvent
16
+ module ItemStateUnDefTypeHelpers
17
+ #
18
+ # Check if the state == UNDEF
19
+ #
20
+ # @return [Boolean] True if the state is UNDEF, false otherwise
21
+ #
22
+ def undef?
23
+ item_state == UnDefType::UNDEF
24
+ end
25
+
26
+ #
27
+ # Check if the state == NULL
28
+ #
29
+ # @return [Boolean] True if the state is NULL, false otherwise
30
+ def null?
31
+ item_state == UnDefType::NULL
32
+ end
33
+
34
+ #
35
+ # Check if the state is defined (not UNDEF or NULL)
36
+ #
37
+ # @return [Boolean] True if state is not UNDEF or NULL
38
+ #
39
+ def state?
40
+ undef? == false && null? == false
41
+ end
42
+
43
+ #
44
+ # Get the item state
45
+ #
46
+ # @return [State] OpenHAB state if state is not UNDEF or NULL, nil otherwise
47
+ #
48
+ def state
49
+ item_state if state?
50
+ end
51
+ end
13
52
 
14
53
  #
15
54
  # MonkeyPatch with ruby style accessors
16
55
  #
17
56
  class ItemStateEvent
18
- alias state item_state
57
+ include ItemStateUnDefTypeHelpers
19
58
  end
20
59
  end
21
60
  end
@@ -10,13 +10,50 @@ module OpenHAB
10
10
  #
11
11
  module Events
12
12
  java_import Java::OrgOpenhabCoreItemsEvents::ItemStateChangedEvent
13
+ java_import Java::OrgOpenhabCoreTypes::UnDefType
13
14
 
14
15
  #
15
16
  # MonkeyPatch with ruby style accessors
16
17
  #
17
18
  class ItemStateChangedEvent
18
- alias state item_state
19
- alias last old_item_state
19
+ include ItemStateUnDefTypeHelpers
20
+
21
+ #
22
+ # Check if state was == UNDEF
23
+ #
24
+ # @return [Boolean] True if the state is UNDEF, false otherwise
25
+ #
26
+ def was_undef?
27
+ old_item_state == UnDefType::UNDEF
28
+ end
29
+
30
+ #
31
+ # Check if state was == NULL
32
+ #
33
+ # @return [Boolean] True if the state is NULL, false otherwise
34
+ def was_null?
35
+ old_item_state == UnDefType::NULL
36
+ end
37
+
38
+ #
39
+ # Check if state was defined (not UNDEF or NULL)
40
+ #
41
+ # @return [Boolean] True if state is not UNDEF or NULL
42
+ #
43
+ def was?
44
+ was_undef? == false && was_null? == false
45
+ end
46
+
47
+ #
48
+ # Get the previous item state
49
+ #
50
+ # @return [State] OpenHAB state if state was not UNDEF or NULL, nil otherwise
51
+ #
52
+ def was
53
+ old_item_state if was?
54
+ end
55
+
56
+ alias last was
20
57
  end
21
58
  end
22
59
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module MonkeyPatch
8
+ #
9
+ # Patches OpenHAB types
10
+ #
11
+ module Types
12
+ java_import Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
13
+
14
+ #
15
+ # Monkey patch for DSL use
16
+ #
17
+ class IncreaseDecreaseType
18
+ alias inspect to_s
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module MonkeyPatch
8
+ #
9
+ # Patches OpenHAB types
10
+ #
11
+ module Types
12
+ java_import Java::OrgOpenhabCoreLibraryTypes::NextPreviousType
13
+
14
+ #
15
+ # Monkey patch for DSL use
16
+ #
17
+ class NextPreviousType
18
+ alias inspect to_s
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -33,16 +33,9 @@ module OpenHAB
33
33
  # nil if object cannot be compared
34
34
  #
35
35
  def ===(other)
36
- # A case statement here causes and infinite loop
37
- # rubocop:disable Style/CaseLikeIf
38
- if self == ON
39
- other.on? if other.respond_to? :on?
40
- elsif self == OFF
41
- other.off? if other.respond_to?('off?')
42
- else
43
- false
44
- end
45
- # rubocop:enable Style/CaseLikeIf
36
+ (on? && other.respond_to?(:on?) && other.on?) ||
37
+ (off? && other.respond_to?(:off?) && other.off?) ||
38
+ super
46
39
  end
47
40
 
48
41
  #
@@ -59,6 +52,26 @@ module OpenHAB
59
52
  super
60
53
  end
61
54
  end
55
+
56
+ #
57
+ # Check if the state is ON
58
+ #
59
+ # @return [Boolean] true if ON, false otherwise
60
+ #
61
+ def on?
62
+ self == ON
63
+ end
64
+
65
+ #
66
+ # Check if the state is OFF
67
+ #
68
+ # @return [Boolean] true if OFF, false otherwise
69
+ #
70
+ def off?
71
+ self == OFF
72
+ end
73
+
74
+ alias inspect to_s
62
75
  end
63
76
  end
64
77
  end
@@ -25,13 +25,9 @@ module OpenHAB
25
25
  # @return [Boolean] True if the other object is a ContactItem and has the same state
26
26
  #
27
27
  def ===(other)
28
- if other.respond_to?(:state)
29
- self == other.state
30
- elsif other.is_a? OpenClosedType
31
- self == other
32
- else
28
+ (open? && other.respond_to?(:open?) && other.open?) ||
29
+ (closed? && other.respond_to?(:closed?) && other.closed?) ||
33
30
  super
34
- end
35
31
  end
36
32
 
37
33
  #
@@ -48,6 +44,26 @@ module OpenHAB
48
44
  super
49
45
  end
50
46
  end
47
+
48
+ #
49
+ # Check if the state is OPEN
50
+ #
51
+ # @return [Boolean] true if OPEN, false otherwise
52
+ #
53
+ def open?
54
+ self == OPEN
55
+ end
56
+
57
+ #
58
+ # Check if the state is CLOSED
59
+ #
60
+ # @return [Boolean] true if CLOSED, false otherwise
61
+ #
62
+ def closed?
63
+ self == CLOSED
64
+ end
65
+
66
+ alias inspect to_s
51
67
  end
52
68
  end
53
69
  end
@@ -25,6 +25,51 @@ module OpenHAB
25
25
  super
26
26
  end
27
27
  # rubocop:enable Lint/UselessMethodDefinition
28
+
29
+ #
30
+ # Provide details about PercentType object
31
+ #
32
+ # @return [String] Representing details about the PercentType object
33
+ #
34
+ def inspect
35
+ "#{to_string}%"
36
+ end
37
+
38
+ #
39
+ # Check if the state is ON
40
+ #
41
+ # @return [Boolean] true if ON, false otherwise
42
+ #
43
+ def on?
44
+ as(OnOffType).on?
45
+ end
46
+
47
+ #
48
+ # Check if the state is OFF
49
+ #
50
+ # @return [Boolean] true if OFF, false otherwise
51
+ #
52
+ def off?
53
+ as(OnOffType).off?
54
+ end
55
+
56
+ #
57
+ # Check if the state is UP
58
+ #
59
+ # @return [Boolean] true if UP, false otherwise
60
+ #
61
+ def up?
62
+ !!as(UpDownType)&.up?
63
+ end
64
+
65
+ #
66
+ # Check if the state is DOWN
67
+ #
68
+ # @return [Boolean] true if DOWN, false otherwise
69
+ #
70
+ def down?
71
+ !!as(UpDownType)&.down?
72
+ end
28
73
  end
29
74
  end
30
75
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module MonkeyPatch
8
+ #
9
+ # Patches OpenHAB types
10
+ #
11
+ module Types
12
+ java_import Java::OrgOpenhabCoreLibraryTypes::PlayPauseType
13
+
14
+ #
15
+ # Monkey patch for DSL use
16
+ #
17
+ class PlayPauseType
18
+ alias inspect to_s
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module MonkeyPatch
8
+ #
9
+ # Patches OpenHAB types
10
+ #
11
+ module Types
12
+ java_import Java::OrgOpenhabCoreTypes::RefreshType
13
+
14
+ #
15
+ # Monkey patch for DSL use
16
+ #
17
+ class RefreshType
18
+ alias inspect to_s
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module MonkeyPatch
8
+ #
9
+ # Patches OpenHAB types
10
+ #
11
+ module Types
12
+ java_import Java::OrgOpenhabCoreLibraryTypes::RewindFastforwardType
13
+
14
+ #
15
+ # Monkey patch for DSL use
16
+ #
17
+ class RewindFastforwardType
18
+ alias inspect to_s
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module MonkeyPatch
8
+ #
9
+ # Patches OpenHAB types
10
+ #
11
+ module Types
12
+ java_import Java::OrgOpenhabCoreLibraryTypes::StopMoveType
13
+
14
+ #
15
+ # Monkey patch for DSL use
16
+ #
17
+ class StopMoveType
18
+ alias inspect to_s
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -4,6 +4,12 @@
4
4
  require 'openhab/dsl/monkey_patch/types/open_closed_type'
5
5
  require 'openhab/dsl/monkey_patch/types/on_off_type'
6
6
  require 'openhab/dsl/monkey_patch/types/decimal_type'
7
+ require 'openhab/dsl/monkey_patch/types/increase_decrease_type'
8
+ require 'openhab/dsl/monkey_patch/types/next_previous_type'
7
9
  require 'openhab/dsl/monkey_patch/types/percent_type'
10
+ require 'openhab/dsl/monkey_patch/types/play_pause_type'
8
11
  require 'openhab/dsl/monkey_patch/types/quantity_type'
12
+ require 'openhab/dsl/monkey_patch/types/refresh_type'
13
+ require 'openhab/dsl/monkey_patch/types/rewind_fastforward_type'
14
+ require 'openhab/dsl/monkey_patch/types/stop_move_type'
9
15
  require 'openhab/dsl/monkey_patch/types/up_down_type'
@@ -45,6 +45,26 @@ module OpenHAB
45
45
  super
46
46
  end
47
47
  end
48
+
49
+ #
50
+ # Check if the state is UP
51
+ #
52
+ # @return [Boolean] true if UP, false otherwise
53
+ #
54
+ def up?
55
+ self == UP
56
+ end
57
+
58
+ #
59
+ # Check if the state is DOWN
60
+ #
61
+ # @return [Boolean] true if DOWN, false otherwise
62
+ #
63
+ def down?
64
+ self == DOWN
65
+ end
66
+
67
+ alias inspect to_s
48
68
  end
49
69
  end
50
70
  end
@@ -22,7 +22,7 @@ module OpenHAB
22
22
  #
23
23
  #
24
24
  def received_command(*items, command: nil, commands: nil)
25
- separate_groups(items).flatten.each do |item|
25
+ separate_groups(items).each do |item|
26
26
  logger.trace("Creating received command trigger for item(#{item})"\
27
27
  "command(#{command}) commands(#{commands})")
28
28
 
@@ -61,9 +61,13 @@ module OpenHAB
61
61
  # @return [Array] A new flat array with any GroupMembers object left intact
62
62
  #
63
63
  def separate_groups(item_array)
64
- return item_array if item_array.grep(Array).length.zero?
64
+ # we want to support anything that can be flattened... i.e. responds to to_ary
65
+ # we want to be more lenient than only things that are currently Array,
66
+ # but Enumerable is too lenient because Array#flatten won't traverse interior
67
+ # Enumerables
68
+ return item_array unless item_array.find { |item| item.respond_to?(:to_ary) }
65
69
 
66
- groups, items = item_array.partition { |item| item.is_a? OpenHAB::DSL::Items::GroupItem::GroupMembers }
70
+ groups, items = item_array.partition { |item| item.is_a?(OpenHAB::DSL::Items::GroupItem::GroupMembers) }
67
71
  groups + separate_groups(items.flatten(1))
68
72
  end
69
73
 
@@ -20,7 +20,7 @@ module OpenHAB
20
20
  # @return [Trigger] Trigger for updated entity
21
21
  #
22
22
  def updated(*items, to: nil)
23
- separate_groups(items).flatten.each do |item|
23
+ separate_groups(items).each do |item|
24
24
  logger.trace("Creating updated trigger for item(#{item}) to(#{to})")
25
25
  [to].flatten.each do |to_state|
26
26
  trigger, config = create_update_trigger(item, to_state)
@@ -4,6 +4,7 @@ require 'java'
4
4
  require 'openhab/log/logger'
5
5
  require 'openhab/dsl/actions'
6
6
  require 'delegate'
7
+ require 'singleton'
7
8
 
8
9
  module OpenHAB
9
10
  module DSL
@@ -76,32 +77,54 @@ module OpenHAB
76
77
  #
77
78
  # Wraps all Things in a delegator to underlying set and provides lookup method
78
79
  #
79
- class Things < SimpleDelegator
80
+ class Things
80
81
  java_import org.openhab.core.thing.ThingUID
81
82
 
83
+ include Enumerable
84
+ include Singleton
85
+
82
86
  # Gets a specific thing by name in the format binding_id:type_id:thing_id
83
87
  # @return Thing specified by name or nil if name does not exist in thing registry
84
- def[](uid)
88
+ def [](uid)
85
89
  thing_uid = ThingUID.new(*uid.split(':'))
86
- # rubocop: disable Style/GlobalVars
87
- thing = $things.get(thing_uid)
88
- # rubocop: enable Style/GlobalVars
90
+ thing = $things.get(thing_uid) # rubocop: disable Style/GlobalVars
89
91
  return unless thing
90
92
 
91
93
  logger.trace("Retrieved Thing(#{thing}) from registry for uid: #{uid}")
92
94
  Thing.new(thing)
93
95
  end
96
+
97
+ alias include? []
98
+ alias key? []
99
+
100
+ # Calls the given block once for each Thing, passing that Thing as a
101
+ # parameter. Returns self.
102
+ #
103
+ # If no block is given, an Enumerator is returned.
104
+ def each(&block)
105
+ # ideally we would do this lazily, but until ruby 2.7
106
+ # there's no #eager method to convert back to a non-lazy
107
+ # enumerator
108
+ to_a.each(&block)
109
+ self
110
+ end
111
+
112
+ # explicit conversion to array
113
+ # more efficient than letting Enumerable do it
114
+ def to_a
115
+ $things.getAll.map { |thing| Thing.new(thing) } # rubocop: disable Style/GlobalVars
116
+ end
117
+ # implicitly convertible to array
118
+ alias to_ary to_a
94
119
  end
95
120
 
96
121
  #
97
122
  # Get all things known to OpenHAB
98
123
  #
99
- # @return [Set] of all Thing objects known to openhab
124
+ # @return [Things] all Thing objects known to OpenHAB
100
125
  #
101
126
  def things
102
- # rubocop: disable Style/GlobalVars
103
- Things.new($things.getAll.map { |thing| Thing.new(thing) }.to_set)
104
- # rubocop: enable Style/GlobalVars
127
+ Things.instance
105
128
  end
106
129
  end
107
130
  end
@@ -31,14 +31,14 @@ module OpenHAB
31
31
  # @param [Duration] duration Duration until timer should fire
32
32
  # @param [Block] block Block to execute when timer fires
33
33
  #
34
- def initialize(duration:, &block)
34
+ def initialize(duration:)
35
35
  @duration = duration
36
36
 
37
37
  # A semaphore is used to prevent a race condition in which calling the block from the timer thread
38
38
  # occurs before the @timer variable can be set resulting in @timer being nil
39
39
  semaphore = Mutex.new
40
40
 
41
- timer_block = proc { semaphore.synchronize { block.call(self) } }
41
+ timer_block = proc { semaphore.synchronize { yield(self) } }
42
42
 
43
43
  semaphore.synchronize do
44
44
  @timer = ScriptExecution.createTimer(
@@ -81,7 +81,7 @@ module OpenHAB
81
81
 
82
82
  case other
83
83
  when TimeOfDay::TimeOfDay, TimeOfDay::TimeOfDayRangeElement then to_tod <=> other
84
- when String then self <=> DateTime.parse(DATE_ONLY_REGEX =~ other ? "#{other}'T'00:00:00#{zone}" : other)
84
+ when String then self <=> DateTime.parse(DATE_ONLY_REGEX.match?(other) ? "#{other}'T'00:00:00#{zone}" : other)
85
85
  else
86
86
  self <=> DateTime.from(other)
87
87
  end
@@ -120,7 +120,7 @@ module OpenHAB
120
120
  when Numeric then DateTime.from(to_time - other)
121
121
  when String
122
122
  dt = DateTime.parse(other)
123
- TIME_ONLY_REGEX =~ other ? self - dt.to_f : time_diff(dt)
123
+ TIME_ONLY_REGEX.match?(other) ? self - dt.to_f : time_diff(dt)
124
124
  when Duration then DateTime.new(zoned_date_time.minus(other))
125
125
  when Time, DateTime, DateTimeType, DateTimeItem then time_diff(other)
126
126
  end
@@ -298,7 +298,7 @@ module OpenHAB
298
298
  # @return [Java::org::openhab::core::library::types::DateTimeType] Object representing the same time
299
299
  #
300
300
  def self.parse(time_string)
301
- time_string += 'Z' if TIME_ONLY_REGEX =~ time_string
301
+ time_string += 'Z' if TIME_ONLY_REGEX.match?(time_string)
302
302
  DateTime.new(DateTimeType.new(time_string))
303
303
  rescue Java::JavaLang::StringIndexOutOfBoundsException, Java::JavaLang::IllegalArgumentException
304
304
  # Try ruby's Time.parse if OpenHAB's DateTimeType parser fails
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '3.9.2'
8
+ VERSION = '4.0.1'
9
9
  end
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: 3.9.2
4
+ version: 4.0.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-09-25 00:00:00.000000000 Z
11
+ date: 2021-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,10 +84,16 @@ files:
84
84
  - lib/openhab/dsl/monkey_patch/ruby/string.rb
85
85
  - lib/openhab/dsl/monkey_patch/ruby/time.rb
86
86
  - lib/openhab/dsl/monkey_patch/types/decimal_type.rb
87
+ - lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb
88
+ - lib/openhab/dsl/monkey_patch/types/next_previous_type.rb
87
89
  - lib/openhab/dsl/monkey_patch/types/on_off_type.rb
88
90
  - lib/openhab/dsl/monkey_patch/types/open_closed_type.rb
89
91
  - lib/openhab/dsl/monkey_patch/types/percent_type.rb
92
+ - lib/openhab/dsl/monkey_patch/types/play_pause_type.rb
90
93
  - lib/openhab/dsl/monkey_patch/types/quantity_type.rb
94
+ - lib/openhab/dsl/monkey_patch/types/refresh_type.rb
95
+ - lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb
96
+ - lib/openhab/dsl/monkey_patch/types/stop_move_type.rb
91
97
  - lib/openhab/dsl/monkey_patch/types/types.rb
92
98
  - lib/openhab/dsl/monkey_patch/types/up_down_type.rb
93
99
  - lib/openhab/dsl/persistence.rb