openhab-scripting 4.3.0 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34dcff6b869bdb1bbcf1e61d84ffe7ff07d039204ee868d16f0f249c3074e898
4
- data.tar.gz: 39645a3f3486d9bba7db56a43d4c60883c346eea007242603a2dd2f7c8aa3b6e
3
+ metadata.gz: a39d84f4797ad4d9807e581919e5a681e0b6ba52df5a64f395bc2409cb2d7f38
4
+ data.tar.gz: d56eb7cb6e9a3a50dd07407bed0a9be23f3b719b676c1e2d4bbf82685f049f73
5
5
  SHA512:
6
- metadata.gz: 303f0751aac26ab7f28a4350ff03151a3a03bb1559f51ae0130c830ea5e20d6bb4165929a173ed4e749cf62ee0e4ab66f1f79845e8e851d44a288407e11be5a5
7
- data.tar.gz: dfee68ab34f41a303886bc8028ae7a30ff4baaee2fd87cc75602bfa7d82f57c2c3a7bf6190f6efecdb2a364c21a4514fe59984e9da8574cf19c5f3fd782abc7e
6
+ metadata.gz: 4bfda0871cefc7b2a0a95ea0d8528ab50111192727f4380f441c922eb5214573a59c9e0c7f39b9c13b26f9ee733607548445477f5b9a79ee7c1028cbbf064db6
7
+ data.tar.gz: 73002d6de64e98d48dd4f5b0a7ad0439c2bed9b15cdabfe36cffbf026930b0cdeac98da0b748e477362f28a19bdfdb2c0223749dbe6ae353ddc8a4e7b52341b6
@@ -9,7 +9,6 @@ module OpenHAB
9
9
  # Module to import and streamline access to OpenHAB actions
10
10
  #
11
11
  module Actions
12
- java_import org.openhab.core.library.types.PercentType
13
12
  include OpenHAB::Log
14
13
 
15
14
  OpenHAB::Core::OSGI.services('org.openhab.core.model.script.engine.action.ActionService')&.each do |service|
@@ -83,7 +82,7 @@ module OpenHAB
83
82
  # @return [void]
84
83
  #
85
84
  def say(text, voice: nil, sink: nil, volume: nil)
86
- volume = PercentType.new(volume&.to_i) unless volume.is_a?(PercentType) || volume.nil?
85
+ volume = Types::PercentType.new(volume) unless volume.is_a?(Types::PercentType) || volume.nil?
87
86
  Voice.say text, voice, sink, volume
88
87
  end
89
88
 
@@ -97,7 +96,7 @@ module OpenHAB
97
96
  # @return [void]
98
97
  #
99
98
  def play_sound(filename, sink: nil, volume: nil)
100
- volume = PercentType.new(volume&.to_i) unless volume.is_a?(PercentType) || volume.nil?
99
+ volume = Types::PercentType.new(volume) unless volume.is_a?(Types::PercentType) || volume.nil?
101
100
  Audio.playSound sink, filename, volume
102
101
  end
103
102
  end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'generic_item'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module Items
8
+ # Functionality to implement +ensure+/+ensure_states+
9
+ module Ensure
10
+ # Contains the global +ensure_states+ method
11
+ module EnsureStates
12
+ # Global method that takes a block and for the duration of the block
13
+ # all commands sent will check if the item is in the command's state
14
+ # before sending the command.
15
+ #
16
+ # @example Turn on several switches only if they're not already on
17
+ # ensure_states do
18
+ # Switch1.on
19
+ # Switch2.on
20
+ # end
21
+ def ensure_states
22
+ old = Thread.current[:ensure_states]
23
+ Thread.current[:ensure_states] = true
24
+ yield
25
+ ensure
26
+ Thread.current[:ensure_states] = old
27
+ end
28
+ module_function :ensure_states
29
+ end
30
+
31
+ # Contains the +ensure+ method mixed into {GenericItem} and {GroupItem::GroupMembers}
32
+ module Ensurable
33
+ # Fluent method call that you can chain commands on to, that will
34
+ # then automatically ensure that the item is not in the command's
35
+ # state before sending the command.
36
+ #
37
+ # @example Turn switch on only if it's not on
38
+ # MySwitch.ensure.on
39
+ # @example Turn on all switches in a group that aren't already on
40
+ # MySwitchGroup.members.ensure.on
41
+ def ensure
42
+ GenericItemDelegate.new(self)
43
+ end
44
+ end
45
+
46
+ # Extensions for {Items::GenericItem} to implement {Ensure}'s
47
+ # functionality
48
+ module GenericItem
49
+ include Ensurable
50
+
51
+ # If +ensure_states+ is active (by block or chained method), then
52
+ # check if this item is in the command's state before actually
53
+ # sending the command
54
+ def command(command)
55
+ return super unless Thread.current[:ensure_states]
56
+ return if command == state
57
+
58
+ super
59
+ end
60
+ alias << command
61
+ end
62
+
63
+ # "anonymous" class that wraps any method call in +ensure_states+
64
+ # before forwarding to the wrapped object
65
+ # @!visibility private
66
+ class GenericItemDelegate
67
+ def initialize(item)
68
+ @item = item
69
+ end
70
+
71
+ # activate +ensure_states+ before forwarding to the wrapped object
72
+ def method_missing(method, *args, &block)
73
+ return super unless @item.respond_to?(method)
74
+
75
+ ensure_states do
76
+ @item.__send__(method, *args, &block)
77
+ end
78
+ end
79
+
80
+ # .
81
+ def respond_to_missing?(method, include_private = false)
82
+ @item.respond_to?(method, include_private) || super
83
+ end
84
+ end
85
+ end
86
+
87
+ GenericItem.prepend(Ensure::GenericItem)
88
+ GroupItem::GroupMembers.include(Ensure::Ensurable)
89
+ end
90
+ end
91
+ end
92
+
93
+ Object.include OpenHAB::DSL::Items::Ensure::EnsureStates
@@ -31,6 +31,74 @@ module OpenHAB
31
31
  def to_a
32
32
  group.get_members.to_a
33
33
  end
34
+
35
+ # Send a command to each member of the group
36
+ #
37
+ # @return [GroupMembers] +self+
38
+ def command(command)
39
+ each { |item| item << command }
40
+ end
41
+ alias << command
42
+
43
+ # @!method refresh
44
+ # Send the +REFRESH+ command to each member of the group
45
+ # @return [GroupMembers] +self+
46
+
47
+ # @!method on
48
+ # Send the +ON+ command to each member of the group
49
+ # @return [GroupMembers] +self+
50
+
51
+ # @!method off
52
+ # Send the +OFF+ command to each member of the group
53
+ # @return [GroupMembers] +self+
54
+
55
+ # @!method up
56
+ # Send the +UP+ command to each member of the group
57
+ # @return [GroupMembers] +self+
58
+
59
+ # @!method down
60
+ # Send the +DOWN+ command to each member of the group
61
+ # @return [GroupMembers] +self+
62
+
63
+ # @!method stop
64
+ # Send the +STOP+ command to each member of the group
65
+ # @return [GroupMembers] +self+
66
+
67
+ # @!method move
68
+ # Send the +MOVE+ command to each member of the group
69
+ # @return [GroupMembers] +self+
70
+
71
+ # @!method increase
72
+ # Send the +INCREASE+ command to each member of the group
73
+ # @return [GroupMembers] +self+
74
+
75
+ # @!method desrease
76
+ # Send the +DECREASE+ command to each member of the group
77
+ # @return [GroupMembers] +self+
78
+
79
+ # @!method play
80
+ # Send the +PLAY+ command to each member of the group
81
+ # @return [GroupMembers] +self+
82
+
83
+ # @!method pause
84
+ # Send the +PAUSE+ command to each member of the group
85
+ # @return [GroupMembers] +self+
86
+
87
+ # @!method rewind
88
+ # Send the +REWIND+ command to each member of the group
89
+ # @return [GroupMembers] +self+
90
+
91
+ # @!method fast_forward
92
+ # Send the +FASTFORWARD+ command to each member of the group
93
+ # @return [GroupMembers] +self+
94
+
95
+ # @!method next
96
+ # Send the +NEXT+ command to each member of the group
97
+ # @return [GroupMembers] +self+
98
+
99
+ # @!method previous
100
+ # Send the +PREVIOUS+ command to each member of the group
101
+ # @return [GroupMembers] +self+
34
102
  end
35
103
 
36
104
  include Enumerable
@@ -18,6 +18,8 @@ require_relative 'player_item'
18
18
  require_relative 'rollershutter_item'
19
19
  require_relative 'string_item'
20
20
 
21
+ require_relative 'ensure'
22
+
21
23
  module OpenHAB
22
24
  module DSL
23
25
  # Contains all OpenHAB *Item classes, as well as associated support
@@ -51,7 +53,7 @@ module OpenHAB
51
53
 
52
54
  # defined methods for commanding an item to one of the Enum states
53
55
  # as well as predicates for if an ItemCommandEvent is one of those commands
54
- def def_command_methods(klass) # rubocop:disable Metrics/MethodLength method has single purpose
56
+ def def_command_methods(klass) # rubocop:disable Metrics method has single purpose
55
57
  values_for_enums(klass.ACCEPTED_COMMAND_TYPES).each do |value|
56
58
  command = Types::COMMAND_ALIASES[value.to_s]
57
59
  next if klass.instance_methods.include?(command)
@@ -63,6 +65,13 @@ module OpenHAB
63
65
  end # end
64
66
  RUBY
65
67
 
68
+ OpenHAB::Core.logger.trace("Defining GroupItem::GroupMembers##{command} for #{value}")
69
+ GroupItem::GroupMembers.class_eval <<~RUBY, __FILE__, __LINE__ + 1
70
+ def #{command} # def on
71
+ each(&:#{command}) # each(&:on)
72
+ end # end
73
+ RUBY
74
+
66
75
  OpenHAB::Core.logger.trace("Defining ItemCommandEvent##{command}? for #{value}")
67
76
  MonkeyPatch::Events::ItemCommandEvent.class_eval <<~RUBY, __FILE__, __LINE__ + 1
68
77
  def #{command}? # def refresh?
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.3.0'
8
+ VERSION = '4.4.0'
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.0
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
@@ -58,6 +58,7 @@ files:
58
58
  - lib/openhab/dsl/items/contact_item.rb
59
59
  - lib/openhab/dsl/items/date_time_item.rb
60
60
  - lib/openhab/dsl/items/dimmer_item.rb
61
+ - lib/openhab/dsl/items/ensure.rb
61
62
  - lib/openhab/dsl/items/generic_item.rb
62
63
  - lib/openhab/dsl/items/group_item.rb
63
64
  - lib/openhab/dsl/items/image_item.rb