openhab-scripting 4.0.0 → 4.1.2

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: bf75e647f468a653d6848739cf2552ba57b9d1b1d81f33eed2f3719315d28d11
4
- data.tar.gz: 8f6403a4642e1edd1eb3047d438939b5d127c0fd0eca57c8989e9a72bf0bdb39
3
+ metadata.gz: 4d752e22ac324370b7c63e7e38455cd919f3f64abece0fc7ab62fd29c72953b7
4
+ data.tar.gz: 34c7cd408a075c831e7ea75e55f907bd57203268da81f34e318657d18ad6a90f
5
5
  SHA512:
6
- metadata.gz: 9e0ea6a8962334965162c95ff2db82a3fcf0485c6b80fefd2ea0814d0883678052bd52306093ea70e2cb0aec9e3fb8df42fd9290f9b9b21ddfb71db11649f104
7
- data.tar.gz: 4d492998dd50c6b25fd845df988d7ef418e5d2a8a66c339bf5fabf990cd6380d6a588ee6fc899d47d697564b78c88397a77d362a39da0a6d7f9d7ba3c99d4c71
6
+ metadata.gz: bc29460c4a427f81fa6a8ce0760d746d0bf1513574e72f89a17c4d5647c22dc860abe12c60d4a0904f5c32927725bed03ff8ff62212bd45217e05d9b9e3fe01a
7
+ data.tar.gz: b29f6d50649f32134815cb71b9c2d42b466f9d0147a613bbc2225f99469f5c5d4194a7a5c47f81c20757b149c2e5bcc7133fc6b099025f43132ecef25bb2cfc2
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'delegate'
4
- require 'forwardable'
3
+ require 'java'
4
+ require 'singleton'
5
+
5
6
  require 'openhab/core/entity_lookup'
6
7
  require 'openhab/dsl/items/group_item'
8
+ require 'openhab/dsl/lazy_array'
7
9
 
8
10
  module OpenHAB
9
11
  module DSL
@@ -14,16 +16,27 @@ module OpenHAB
14
16
  #
15
17
  # Provide access to groups as a set
16
18
  #
17
- class Groups < SimpleDelegator
19
+ class Groups
20
+ include LazyArray
21
+ include Singleton
22
+
18
23
  #
19
24
  # Get a OpenHAB Group by name
20
25
  # @param [String] name of the group to retrieve
21
26
  #
22
27
  # @return [Set] of OpenHAB Groups
23
28
  #
24
- def[](name)
25
- group = OpenHAB::Core::EntityLookup.lookup_item(name)
26
- group.is_a?(OpenHAB::DSL::Items::GroupItem) ? group : nil
29
+ def [](name)
30
+ group = Core::EntityLookup.lookup_item(name)
31
+ group.is_a?(Items::GroupItem) ? group : nil
32
+ end
33
+ alias include? []
34
+ alias key? include?
35
+
36
+ # explicit conversion to array
37
+ def to_a
38
+ items = $ir.items.grep(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
39
+ Core::EntityLookup.decorate_items(items)
27
40
  end
28
41
  end
29
42
 
@@ -33,9 +46,7 @@ module OpenHAB
33
46
  # @return [Set] of OpenHAB Groups
34
47
  #
35
48
  def groups
36
- # rubocop: disable Style/GlobalVars
37
- Groups.new(OpenHAB::Core::EntityLookup.decorate_items($ir.items.grep(Java::OrgOpenhabCoreItems::GroupItem)))
38
- # rubocop: enable Style/GlobalVars
49
+ Groups.instance
39
50
  end
40
51
  end
41
52
  end
@@ -3,9 +3,11 @@
3
3
  require 'delegate'
4
4
  require 'forwardable'
5
5
  require 'java'
6
+
7
+ require 'openhab/core/entity_lookup'
6
8
  require 'openhab/dsl/items/item_command'
7
9
  require 'openhab/dsl/items/item_delegate'
8
- require 'openhab/core/entity_lookup'
10
+ require 'openhab/dsl/lazy_array'
9
11
 
10
12
  module OpenHAB
11
13
  module DSL
@@ -13,7 +15,9 @@ module OpenHAB
13
15
  #
14
16
  # Class for indicating to triggers that a group trigger should be used
15
17
  #
16
- class GroupMembers < SimpleDelegator
18
+ class GroupMembers
19
+ include LazyArray
20
+
17
21
  attr_reader :group
18
22
 
19
23
  #
@@ -23,7 +27,11 @@ module OpenHAB
23
27
  #
24
28
  def initialize(group_item)
25
29
  @group = group_item
26
- super(OpenHAB::Core::EntityLookup.decorate_items(@group.members.to_a))
30
+ end
31
+
32
+ # explicit conversion to array
33
+ def to_a
34
+ OpenHAB::Core::EntityLookup.decorate_items(group.members.to_a)
27
35
  end
28
36
  end
29
37
 
@@ -74,7 +82,7 @@ module OpenHAB
74
82
  # Iterates through the direct members of the Group
75
83
  #
76
84
  def each(&block)
77
- OpenHAB::Core::EntityLookup.decorate_items(@group_item.members.to_a).each(&block)
85
+ members.each(&block)
78
86
  end
79
87
 
80
88
  #
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'java'
4
+ require 'singleton'
5
+
4
6
  require 'openhab/core/entity_lookup'
7
+ require 'openhab/dsl/lazy_array'
5
8
 
6
9
  module OpenHAB
7
10
  module DSL
@@ -12,11 +15,14 @@ module OpenHAB
12
15
  #
13
16
  # Delegates to underlying set of all OpenHAB Items, provides convenience methods
14
17
  #
15
- class Items < SimpleDelegator
18
+ class Items
19
+ include LazyArray
20
+ include Singleton
21
+
16
22
  # Fetches the named item from the the ItemRegistry
17
23
  # @param [String] name
18
24
  # @return Item from registry, nil if item missing or requested item is a Group Type
19
- def[](name)
25
+ def [](name)
20
26
  OpenHAB::Core::EntityLookup.lookup_item(name)
21
27
  rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
22
28
  nil
@@ -26,19 +32,21 @@ module OpenHAB
26
32
  # @param name [String] Item name to check
27
33
  # @return [Boolean] true if the item exists, false otherwise
28
34
  def include?(name)
29
- # rubocop: disable Style/GlobalVars
30
- !$ir.getItems(name).empty?
31
- # rubocop: enable Style/GlobalVars
35
+ !$ir.getItems(name).empty? # rubocop: disable Style/GlobalVars
36
+ end
37
+ alias key? []
38
+
39
+ # explicit conversion to array
40
+ def to_a
41
+ items = $ir.items.grep_v(org.openhab.core.items.GroupItem) # rubocop:disable Style/GlobalVars
42
+ OpenHAB::Core::EntityLookup.decorate_items(items)
32
43
  end
33
- alias key? include?
34
44
  end
35
45
 
36
46
  # Fetches all non-group items from the item registry
37
47
  # @return [OpenHAB::DSL::Items::Items]
38
48
  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
49
+ Items.instance
42
50
  end
43
51
  end
44
52
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ # common base class for array-like collections that have lookup
6
+ # methods to avoid instantiating the elements if you only use
7
+ # the lookup method
8
+ #
9
+ # your class should implement to_a
10
+ #
11
+ module LazyArray
12
+ include Enumerable
13
+
14
+ # Calls the given block once for each Thing, passing that Thing as a
15
+ # parameter. Returns self.
16
+ #
17
+ # If no block is given, an Enumerator is returned.
18
+ def each(&block)
19
+ to_a.each(&block)
20
+ self
21
+ end
22
+
23
+ # implicitly convertible to array
24
+ def to_ary
25
+ to_a
26
+ end
27
+
28
+ # delegate any other methods to the actual array
29
+ # exclude mutating methods though
30
+ def method_missing(method, *args, &block)
31
+ return to_a.send(method, *args, &block) if method[-1] != '!' && Array.instance_methods.include?(method)
32
+
33
+ super
34
+ end
35
+
36
+ # advertise that methods exist that would be delegated to Array
37
+ def respond_to_missing?(method, include_private = false)
38
+ return true if method[-1] != '!' && Array.instance_methods.include?(method.to_sym)
39
+
40
+ super
41
+ end
42
+ end
43
+ end
44
+ end
@@ -16,6 +16,11 @@ module OpenHAB
16
16
  # MonkeyPatching Decimal Type
17
17
  #
18
18
  class DecimalType
19
+ extend Forwardable
20
+
21
+ delegate %i[to_d zero?] => :to_big_decimal
22
+ delegate %i[positive? negative?] => :to_d
23
+
19
24
  #
20
25
  # @param [Object] other object to compare to
21
26
  #
@@ -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)
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'delegate'
3
4
  require 'java'
5
+ require 'singleton'
6
+
4
7
  require 'openhab/log/logger'
5
8
  require 'openhab/dsl/actions'
6
- require 'delegate'
9
+ require 'openhab/dsl/lazy_array'
7
10
 
8
11
  module OpenHAB
9
12
  module DSL
@@ -76,32 +79,36 @@ module OpenHAB
76
79
  #
77
80
  # Wraps all Things in a delegator to underlying set and provides lookup method
78
81
  #
79
- class Things < SimpleDelegator
80
- java_import org.openhab.core.thing.ThingUID
82
+ class Things
83
+ include LazyArray
84
+ include Singleton
81
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)
85
- thing_uid = ThingUID.new(*uid.split(':'))
86
- # rubocop: disable Style/GlobalVars
87
- thing = $things.get(thing_uid)
88
- # rubocop: enable Style/GlobalVars
88
+ def [](uid)
89
+ thing_uid = org.openhab.core.thing.ThingUID.new(*uid.split(':'))
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
+ alias include? []
97
+ alias key? []
98
+
99
+ # explicit conversion to array
100
+ def to_a
101
+ $things.getAll.map { |thing| Thing.new(thing) } # rubocop: disable Style/GlobalVars
102
+ end
94
103
  end
95
104
 
96
105
  #
97
106
  # Get all things known to OpenHAB
98
107
  #
99
- # @return [Set] of all Thing objects known to openhab
108
+ # @return [Things] all Thing objects known to OpenHAB
100
109
  #
101
110
  def things
102
- # rubocop: disable Style/GlobalVars
103
- Things.new($things.getAll.map { |thing| Thing.new(thing) }.to_set)
104
- # rubocop: enable Style/GlobalVars
111
+ Things.instance
105
112
  end
106
113
  end
107
114
  end
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.0.0'
8
+ VERSION = '4.1.2'
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: 4.0.0
4
+ version: 4.1.2
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-10-05 00:00:00.000000000 Z
11
+ date: 2021-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -64,6 +64,7 @@ files:
64
64
  - lib/openhab/dsl/items/player_item.rb
65
65
  - lib/openhab/dsl/items/rollershutter_item.rb
66
66
  - lib/openhab/dsl/items/string_item.rb
67
+ - lib/openhab/dsl/lazy_array.rb
67
68
  - lib/openhab/dsl/monkey_patch/actions/actions.rb
68
69
  - lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb
69
70
  - lib/openhab/dsl/monkey_patch/events/events.rb