openhab-scripting 2.18.0 → 2.19.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: 44145354fe523ea40a69c6cf2ba81f2684e9982580fe5bc3996f07ab56202e71
4
- data.tar.gz: ac579ab41fe3f005d7e5a87df7d18c7f32efa9f49c40f251e07620f238b9e65f
3
+ metadata.gz: 64cbcfa58ba3a4d63a88514b61fdbe811a317e3902c61e16119e0d97a89a3523
4
+ data.tar.gz: 075ca2dbc459da17c0c2f13b6a05bf6c8ffb829b5d0f96965b449820db59b216
5
5
  SHA512:
6
- metadata.gz: 00026266f68f7d1c9b1ba442ffe0933ac90b5dbe80b61aa2db25470bf3c166c3f5f73067fbba4c292ad31eaeb6913e56299fc9c3625b1e7ce3efc8aaeac69665
7
- data.tar.gz: 497fee2e97d90f9e64bd2ba324b9612ed44becb0aec7eb51d4706f1414e3313afeeb3242b5ece884be2fb15525c190a753116548fc2a022c0d4159cff2a8f559
6
+ metadata.gz: 4ae006b45842d227600eaaeb094285800bddf16935388d6c686112c31bd596ddc8d7fa53eebc6d1b83c0e4053c379662fbfcebab441794bdc183143c27004f92
7
+ data.tar.gz: 0506ac926852a72f400d8c47d701205e27a22b3514878a366eba39812b5226358c1a5e11a95bce3376b03fef2adceac9ccc1f76e5d3dca0a079b2883d565e8ea
@@ -8,6 +8,7 @@ require 'openhab/log/logger'
8
8
  require 'openhab/dsl/items/number_item'
9
9
  require 'openhab/dsl/items/string_item'
10
10
  require 'openhab/dsl/items/datetime_item'
11
+ require 'openhab/dsl/items/rollershutter_item'
11
12
 
12
13
  # Automation lookup and injection of OpenHab entities
13
14
  java_import org.openhab.core.items.GroupItem
@@ -86,7 +87,8 @@ module OpenHAB
86
87
  # @return [Object] the ruby wrapper for the item
87
88
  #
88
89
  # rubocop: disable Metrics/MethodLength
89
- # Method length check disabled - case dispatch pattern
90
+ # rubocop: disable Metrics/AbcSize
91
+ # Disabled line length and branch size - case dispatch pattern
90
92
  private_class_method def self.decorate_item(item)
91
93
  case item
92
94
  when GroupItem
@@ -97,10 +99,13 @@ module OpenHAB
97
99
  OpenHAB::DSL::Items::StringItem.new(item)
98
100
  when Java::Org.openhab.core.library.items::DateTimeItem
99
101
  OpenHAB::DSL::Items::DateTimeItem.new(item)
102
+ when Java::Org.openhab.core.library.items::RollershutterItem
103
+ OpenHAB::DSL::Items::RollershutterItem.new(item)
100
104
  else
101
105
  item
102
106
  end
103
107
  end
108
+ # rubocop: enable Metrics/AbcSize
104
109
  # rubocop: enable Metrics/MethodLength
105
110
 
106
111
  #
@@ -0,0 +1,179 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'java'
5
+
6
+ module OpenHAB
7
+ module DSL
8
+ module Items
9
+ #
10
+ # Delegator to OpenHAB Rollershutter Item
11
+ #
12
+ class RollershutterItem < Numeric
13
+ extend Forwardable
14
+ include Comparable
15
+
16
+ def_delegator :@rollershutter_item, :to_s
17
+
18
+ java_import Java::OrgOpenhabCoreLibraryTypes::PercentType
19
+ java_import Java::OrgOpenhabCoreLibraryTypes::UpDownType
20
+ java_import Java::OrgOpenhabCoreLibraryTypes::StopMoveType
21
+
22
+ #
23
+ # Creates a new RollershutterItem
24
+ #
25
+ # @param [Java::OrgOpenhabCoreLibraryItems::RollershutterItem] rollershutter_item
26
+ # The OpenHAB RollershutterItem to delegate to
27
+ #
28
+ def initialize(rollershutter_item)
29
+ logger.trace("Wrapping #{rollershutter_item}")
30
+ @rollershutter_item = rollershutter_item
31
+
32
+ super()
33
+ end
34
+
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
+ #
54
+ # Returns the rollershutter's position
55
+ #
56
+ # @return [Java::OrgOpenhabCoreLibraryTypes::PercentType] the position of the rollershutter
57
+ #
58
+ def position
59
+ state.as(PercentType)
60
+ end
61
+
62
+ #
63
+ # Compare the rollershutter's position against another object
64
+ #
65
+ # @param [Object] other object to compare against
66
+ #
67
+ # @return [Integer] -1, 0 or 1 depending on the result of the comparison
68
+ #
69
+ def <=>(other)
70
+ case other
71
+ when PercentType, Java::OrgOpenhabCoreLibraryTypes::DecimalType then position.compare_to(other)
72
+ when Numeric then position.int_value <=> other
73
+ when RollershutterItem then position.compare_to(other.position)
74
+ when UpDownType then state.as(UpDownType) == other
75
+ end
76
+ end
77
+
78
+ #
79
+ # Coerce self into other to enable calculations
80
+ #
81
+ # @param [Numeric] other Other numeric to coerce into
82
+ #
83
+ # @return [Array<Numeric>] an array of other and self coerced into other's type
84
+ #
85
+ def coerce(other)
86
+ raise ArgumentError, "Cannot coerce to #{other.class}" unless other.is_a? Numeric
87
+
88
+ case other
89
+ when Integer then [other, position.int_value]
90
+ when Float then [other, position.float_value]
91
+ end
92
+ end
93
+
94
+ #
95
+ # Case equality
96
+ #
97
+ # @param [Java::OrgOpenhabCoreLibraryTypes::UpDownType, Numeric] other Other object to compare against
98
+ #
99
+ # @return [Boolean] true if self can be defined as other, false otherwise
100
+ #
101
+ def ===(other)
102
+ super unless other.is_a? UpDownType
103
+
104
+ state.as(UpDownType).equals(other)
105
+ end
106
+
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
+ #
136
+ # Define math operations
137
+ #
138
+ %i[+ - * / %].each do |operator|
139
+ define_method(operator) do |other|
140
+ right, left = coerce(other)
141
+ left.send(operator, right)
142
+ end
143
+ end
144
+
145
+ #
146
+ # Checks if this method responds to the missing method
147
+ #
148
+ # @param [String] meth Name of the method to check
149
+ # @param [Boolean] _include_private boolean if private methods should be checked
150
+ #
151
+ # @return [Boolean] true if this object will respond to the supplied method, false otherwise
152
+ #
153
+ def respond_to_missing?(meth, _include_private = false)
154
+ @rollershutter_item.respond_to?(meth) || position.respond_to?(meth)
155
+ end
156
+
157
+ #
158
+ # Forward missing methods to Openhab RollershutterItem or the PercentType representing it's state
159
+ # if they are defined
160
+ #
161
+ # @param [String] meth method name
162
+ # @param [Array] args arguments for method
163
+ # @param [Proc] block <description>
164
+ #
165
+ # @return [Object] Value from delegated method in OpenHAB NumberItem
166
+ #
167
+ def method_missing(meth, *args, &block)
168
+ if @rollershutter_item.respond_to?(meth)
169
+ @rollershutter_item.__send__(meth, *args, &block)
170
+ elsif position.respond_to?(meth)
171
+ position.__send__(meth, *args, &block)
172
+ else
173
+ raise NoMethodError, "No method `#{meth}' defined for #{self.class}"
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -6,3 +6,4 @@ require 'openhab/dsl/monkey_patch/types/on_off_type'
6
6
  require 'openhab/dsl/monkey_patch/types/decimal_type'
7
7
  require 'openhab/dsl/monkey_patch/types/percent_type'
8
8
  require 'openhab/dsl/monkey_patch/types/quantity_type'
9
+ require 'openhab/dsl/monkey_patch/types/up_down_type'
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+ module OpenHAB
5
+ module DSL
6
+ module MonkeyPatch
7
+ #
8
+ # Patches OpenHAB types
9
+ #
10
+ module Types
11
+ java_import Java::OrgOpenhabCoreLibraryTypes::UpDownType
12
+
13
+ #
14
+ # MonkeyPatching UpDownType
15
+ #
16
+ class UpDownType
17
+ #
18
+ # Check if the supplied object is case equals to self
19
+ #
20
+ # @param [Object] other object to compare
21
+ #
22
+ # @return [Boolean] True if the other object is a RollershutterItem and has the same state
23
+ #
24
+ def ===(other)
25
+ super unless other.is_a? OpenHAB::DSL::Items::RollershutterItem
26
+
27
+ equals(other.state.as(UpDownType))
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '2.18.0'
8
+ VERSION = '2.19.0'
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: 2.18.0
4
+ version: 2.19.0
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-02-14 00:00:00.000000000 Z
11
+ date: 2021-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,6 +42,7 @@ files:
42
42
  - lib/openhab/dsl/items/datetime_item.rb
43
43
  - lib/openhab/dsl/items/items.rb
44
44
  - lib/openhab/dsl/items/number_item.rb
45
+ - lib/openhab/dsl/items/rollershutter_item.rb
45
46
  - lib/openhab/dsl/items/string_item.rb
46
47
  - lib/openhab/dsl/monkey_patch/actions/actions.rb
47
48
  - lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb
@@ -67,6 +68,7 @@ files:
67
68
  - lib/openhab/dsl/monkey_patch/types/percent_type.rb
68
69
  - lib/openhab/dsl/monkey_patch/types/quantity_type.rb
69
70
  - lib/openhab/dsl/monkey_patch/types/types.rb
71
+ - lib/openhab/dsl/monkey_patch/types/up_down_type.rb
70
72
  - lib/openhab/dsl/persistence.rb
71
73
  - lib/openhab/dsl/rules/automation_rule.rb
72
74
  - lib/openhab/dsl/rules/guard.rb