openhab-scripting 2.19.3 → 2.21.0

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: 92d53929fa49d74df4409422669c809e146723ef88862019ee77dbf8f671b2e0
4
- data.tar.gz: 3d421f3dd17058b3319bf1973da3c39f4dcc74b1a04ac365b1361be0afb59ab8
3
+ metadata.gz: b163016271e25f2e7e4607db6bb97d3e1fadc1e19b6d8a0396560278aa70c4ea
4
+ data.tar.gz: e133649130e7c960f0e4b1174fe20644111d933f339aabe8256dbfcf39862f10
5
5
  SHA512:
6
- metadata.gz: ef748b3b0b3f7e4e85a7b2523df44101ceb64c9170358e5521fb2601880aefd5fd789c71d88c2115b339323081013e53032daf7e1b7e25ffda9010493da9cbec
7
- data.tar.gz: 7468514e99d37e180e694b07ef751c5f9badf56dbc94f43e9ed982c9a7799760c37940dfaaa5c0adaecbfb3d82505a184401bad565ab88c56c0401207c3f2176
6
+ metadata.gz: c93bb71565595e355f1e8a3669bff9a389bbc92bc1d7f1b27e534c055c6bbfb45bc39d2220672d06e061c099c48cc73fdfad72642959e2c0abc8329f13bd5811
7
+ data.tar.gz: 5cf280a5718af694e89d2f5c8262bd6930ef8dea1bf61f697e72391bb0be58089e6c7677e1c2a22b6dfe31f7fb16c137706f1ecadcf76fcacd8f9280225b542b
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module Items
8
+ #
9
+ # Holds methods to automatically generate commands and
10
+ # accessors for items
11
+ module ItemCommand
12
+ #
13
+ # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
14
+ # string representation of the enum value For example, an enum with values of STOP and START
15
+ # would create methods stop() and start() that send the corresponding STOP and START commands to the item
16
+ #
17
+ # @param [Java::JavaLang::Enum] command_enum Enumeration to create commands for
18
+ #
19
+ #
20
+ def item_command(command_enum)
21
+ # rubocop:disable Style/HashEachMethods
22
+ # Disable rule because Java enum does not support each_value
23
+ command_enum.values.each do |command|
24
+ command_method = command.to_s.downcase
25
+ define_method(command_method) do
26
+ self.command(command)
27
+ end
28
+ end
29
+ # rubocop:enable Style/HashEachMethods
30
+ end
31
+
32
+ #
33
+ # For every value in the supplied enumeration create a corresponding method mapped to the lowercase
34
+ # string representation appended with a question mark '?' of the enum value For example,
35
+ # an enum with values of UP and DOWN would create methods up? and down? that check
36
+ # if the current state matches the value of the enum
37
+ #
38
+ # @param [Java::JavaLang::Enum] command_enum Enumeration to create methods for each value
39
+ # to check if current state matches that enum
40
+ # @yield [state] Optional block that can be used to transform state prior to comparison
41
+ #
42
+ #
43
+ def item_state(command_enum)
44
+ # rubocop:disable Style/HashEachMethods
45
+ # Disable rule because Java enum does not support each_value
46
+ command_enum.values.each do |command|
47
+ status_method = "#{command.to_s.downcase}?"
48
+ define_method(status_method) do
49
+ state? && state.as(command_enum) == command
50
+ end
51
+ end
52
+ # rubocop:enable Style/HashEachMethods
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'forwardable'
4
4
  require 'java'
5
+ require 'openhab/dsl/items/item_command'
5
6
 
6
7
  module OpenHAB
7
8
  module DSL
@@ -11,6 +12,7 @@ module OpenHAB
11
12
  #
12
13
  class RollershutterItem < Numeric
13
14
  extend Forwardable
15
+ extend OpenHAB::DSL::Items::ItemCommand
14
16
  include Comparable
15
17
 
16
18
  def_delegator :@rollershutter_item, :to_s
@@ -19,6 +21,10 @@ module OpenHAB
19
21
  java_import Java::OrgOpenhabCoreLibraryTypes::UpDownType
20
22
  java_import Java::OrgOpenhabCoreLibraryTypes::StopMoveType
21
23
 
24
+ item_command Java::OrgOpenhabCoreLibraryTypes::StopMoveType
25
+ item_command Java::OrgOpenhabCoreLibraryTypes::UpDownType
26
+ item_state Java::OrgOpenhabCoreLibraryTypes::UpDownType
27
+
22
28
  #
23
29
  # Creates a new RollershutterItem
24
30
  #
@@ -32,24 +38,6 @@ module OpenHAB
32
38
  super()
33
39
  end
34
40
 
35
- #
36
- # Check if the rollershutter is up
37
- #
38
- # @return [Boolean] true if the rollershutter is up, false otherwise
39
- #
40
- def up?
41
- state.as(UpDownType) == UpDownType::UP
42
- end
43
-
44
- #
45
- # Check if the rollershutter is down
46
- #
47
- # @return [Boolean] true if the rollershutter is down, false otherwise
48
- #
49
- def down?
50
- state.as(UpDownType) == UpDownType::DOWN
51
- end
52
-
53
41
  #
54
42
  # Returns the rollershutter's position
55
43
  #
@@ -104,34 +92,6 @@ module OpenHAB
104
92
  state.as(UpDownType).equals(other)
105
93
  end
106
94
 
107
- #
108
- # Sends an UP command to the Item
109
- #
110
- def up
111
- command UpDownType::UP
112
- end
113
-
114
- #
115
- # Sends a DOWN command to the Item
116
- #
117
- def down
118
- command UpDownType::DOWN
119
- end
120
-
121
- #
122
- # Sends a STOP command to the Item
123
- #
124
- def stop
125
- command StopMoveType::STOP
126
- end
127
-
128
- #
129
- # Sends a MOVE command to the Item
130
- #
131
- def move
132
- command StopMoveType::MOVE
133
- end
134
-
135
95
  #
136
96
  # Define math operations
137
97
  #
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'java'
4
+ require 'openhab/dsl/items/item_command'
4
5
 
5
6
  module OpenHAB
6
7
  module DSL
@@ -20,24 +21,11 @@ module OpenHAB
20
21
  # Monkey patch Contact Item with Ruby methods
21
22
  #
22
23
  class ContactItem
23
- java_import org.openhab.core.library.types.OpenClosedType
24
- #
25
- # Check if the contact is open
26
- #
27
- # @return [Boolean] True if contact has state and is open, false otherwise
28
- #
29
- def open?
30
- state? && state == OpenClosedType::OPEN
31
- end
24
+ extend OpenHAB::DSL::Items::ItemCommand
32
25
 
33
- #
34
- # Check if the contact is closed
35
- #
36
- # @return [Boolean] True if contact has state and is closed, false otherwise
37
- #
38
- def closed?
39
- state? && state == OpenClosedType::CLOSED
40
- end
26
+ java_import Java::OrgOpenhabCoreLibraryTypes::OpenClosedType
27
+
28
+ item_state Java::OrgOpenhabCoreLibraryTypes::OpenClosedType
41
29
 
42
30
  #
43
31
  # Compares contacts to OpenClosedTypes
@@ -101,10 +101,10 @@ module OpenHAB
101
101
  #
102
102
  # Get the string representation of the state of the item
103
103
  #
104
- # @return [String] State of the item as a string if not UNDEF or NULL, nil otherwise
104
+ # @return [String] State of the item as a string
105
105
  #
106
106
  def to_s
107
- state&.to_s
107
+ method(:state).super_method.call.to_s # call the super state to include UNDEF/NULL
108
108
  end
109
109
 
110
110
  #
@@ -134,6 +134,18 @@ module OpenHAB
134
134
  end
135
135
  end
136
136
 
137
+ #
138
+ # Implements Hash#dig-like functionaity to metadata
139
+ #
140
+ # @param [String] key The first key
141
+ # @param [Array<String, Symbol>] keys More keys to dig deeper
142
+ #
143
+ # @return [OpenHAB::DSL::MonkeyPatch::Items::MetadataItem], or nil if the namespace doesn't exist
144
+ #
145
+ def dig(key, *keys)
146
+ keys.empty? ? self[key]&.value : self[key]&.dig(*keys)
147
+ end
148
+
137
149
  #
138
150
  # Enumerates through all the namespaces
139
151
  #
@@ -49,7 +49,12 @@ module OpenHAB
49
49
  if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
50
50
  timestamp = Java::JavaTime::ZonedDateTime.now.minus(timestamp)
51
51
  end
52
- PersistenceExtensions.public_send(method, self, timestamp, service&.to_s)
52
+ result = PersistenceExtensions.public_send(method, self, timestamp, service&.to_s)
53
+ if result.is_a?(Java::OrgOpenhabCoreLibraryTypes::DecimalType) && respond_to?(:unit) && unit
54
+ Quantity.new(Java::OrgOpenhabCoreLibraryTypes::QuantityType.new(result.to_big_decimal, unit))
55
+ else
56
+ result
57
+ end
53
58
  end
54
59
  end
55
60
 
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'java'
4
+ require 'openhab/dsl/items/item_command'
5
+
3
6
  module OpenHAB
4
7
  module DSL
5
8
  module MonkeyPatch
@@ -16,43 +19,15 @@ module OpenHAB
16
19
  # Monkeypatching SwitchItem to add Ruby Support methods
17
20
  #
18
21
  class SwitchItem
19
- java_import Java::OrgOpenhabCoreLibraryTypes::OnOffType
20
- #
21
- # Send the OFF command to the switch
22
- #
23
- #
24
- def off
25
- command(OnOffType::OFF)
26
- end
22
+ extend OpenHAB::DSL::Items::ItemCommand
27
23
 
28
- #
29
- # Send the OFF command to the switch
30
- #
31
- #
32
- def on
33
- command(OnOffType::ON)
34
- end
24
+ java_import Java::OrgOpenhabCoreLibraryTypes::OnOffType
35
25
 
36
- #
37
- # Check if a switch is on
38
- #
39
- # @return [Boolean] True if the switch is on, false otherwise
40
- #
41
- def on?
42
- state? && state == OnOffType::ON
43
- end
26
+ item_command Java::OrgOpenhabCoreLibraryTypes::OnOffType
27
+ item_state Java::OrgOpenhabCoreLibraryTypes::OnOffType
44
28
 
45
29
  alias truthy? on?
46
30
 
47
- #
48
- # Check if a switch is off
49
- #
50
- # @return [Boolean] True if the switch is off, false otherwise
51
- #
52
- def off?
53
- state? && state == OnOffType::OFF
54
- end
55
-
56
31
  #
57
32
  # Send a command to invert the state of the switch
58
33
  #
@@ -21,7 +21,7 @@ module OpenHAB
21
21
  #
22
22
  def ==(other)
23
23
  case other
24
- when OpenHAB::DSL::Types::Quantity, QuantityType
24
+ when OpenHAB::DSL::Types::Quantity, QuantityType, Java::OrgOpenhabCoreLibraryTypes::StringType
25
25
  other == self
26
26
  else
27
27
  super
@@ -122,7 +122,7 @@ module OpenHAB
122
122
  # @return [Boolean] true if no from state is defined or defined state equals supplied state
123
123
  #
124
124
  def check_from(trigger_delay, state)
125
- trigger_delay.from.nil? || trigger_delay.from == state
125
+ trigger_delay.from.nil? || state == trigger_delay.from
126
126
  end
127
127
 
128
128
  #
@@ -134,7 +134,7 @@ module OpenHAB
134
134
  # @return [Boolean] true if no to state is defined or defined state equals supplied state
135
135
  #
136
136
  def check_to(trigger_delay, state)
137
- trigger_delay.to.nil? || trigger_delay.to == state
137
+ trigger_delay.to.nil? || state == trigger_delay.to
138
138
  end
139
139
 
140
140
  #
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '2.19.3'
8
+ VERSION = '2.21.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: 2.19.3
4
+ version: 2.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
@@ -40,6 +40,7 @@ files:
40
40
  - lib/openhab/dsl/gems.rb
41
41
  - lib/openhab/dsl/group.rb
42
42
  - lib/openhab/dsl/items/datetime_item.rb
43
+ - lib/openhab/dsl/items/item_command.rb
43
44
  - lib/openhab/dsl/items/items.rb
44
45
  - lib/openhab/dsl/items/number_item.rb
45
46
  - lib/openhab/dsl/items/rollershutter_item.rb