openhab-scripting 4.4.0 → 4.5.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: a39d84f4797ad4d9807e581919e5a681e0b6ba52df5a64f395bc2409cb2d7f38
4
- data.tar.gz: d56eb7cb6e9a3a50dd07407bed0a9be23f3b719b676c1e2d4bbf82685f049f73
3
+ metadata.gz: 13732b675b80ad3a92e2346e56da3e7d63b6a4845f8409c5a545d1b5006ab999
4
+ data.tar.gz: a5b9d54c25a47857256805d849344cfde4602ad23408228cc595bfc574a04587
5
5
  SHA512:
6
- metadata.gz: 4bfda0871cefc7b2a0a95ea0d8528ab50111192727f4380f441c922eb5214573a59c9e0c7f39b9c13b26f9ee733607548445477f5b9a79ee7c1028cbbf064db6
7
- data.tar.gz: 73002d6de64e98d48dd4f5b0a7ad0439c2bed9b15cdabfe36cffbf026930b0cdeac98da0b748e477362f28a19bdfdb2c0223749dbe6ae353ddc8a4e7b52341b6
6
+ metadata.gz: 9d8715ab0e0e84315b02a67aeb28f47ebcddb8b58ea5ea1043b53ef329f5f5017329fdae23961530cd1f0ac184e38acf8ad11fd9b1a7a41f77f486cf3d53f01c
7
+ data.tar.gz: 15a9babdc9a9d7179dda57fcad46356175c25933f534cc4ed1dad9566a56bdde08677b6ef0224c2761fdeb1dd9253f4d6a50cec5a5804bd703b6c843e438a66b
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ require_relative 'comparable_item'
6
+ require 'openhab/dsl/types/hsb_type'
7
+
8
+ module OpenHAB
9
+ module DSL
10
+ module Items
11
+ java_import org.openhab.core.library.items.ColorItem
12
+
13
+ # Adds methods to core OpenHAB ColorItem type to make it more natural in
14
+ # Ruby
15
+ class ColorItem < DimmerItem
16
+ extend Forwardable
17
+ include ComparableItem
18
+
19
+ # !@visibility private
20
+ def ==(other)
21
+ # need to check if we're referring to the same item before
22
+ # forwarding to <=> (and thus checking equality with state)
23
+ return true if equal?(other) || eql?(other)
24
+
25
+ super
26
+ end
27
+
28
+ #
29
+ # Type Coercion
30
+ #
31
+ # Coerce object to a HSBType
32
+ #
33
+ # @param [Types::HSBType, String] other object to coerce to a
34
+ # HSBType
35
+ #
36
+ # @return [[Types::HSBType, Types::HSBType]]
37
+ #
38
+ def coerce(other)
39
+ logger.trace("Coercing #{self} as a request from #{other.class}")
40
+ return [other, nil] unless state?
41
+ return [other, state] if other.is_a?(Types::HSBType) || other.respond_to?(:to_str)
42
+
43
+ raise TypeError, "can't convert #{other.class} into #{self.class}"
44
+ end
45
+
46
+ # any method that exists on {Types::HSBType} gets forwarded to +state+
47
+ delegate (Types::HSBType.instance_methods - instance_methods) => :state
48
+
49
+ # string commands aren't allowed on ColorItems, so try to implicitly
50
+ # convert it to an HSBType
51
+ # @!visibility private
52
+ def format_type(command)
53
+ return Types::HSBType.new(command) if command.respond_to?(:to_str)
54
+
55
+ super
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -10,6 +10,7 @@ require_relative 'switch_item'
10
10
  require_relative 'date_time_item'
11
11
  require_relative 'dimmer_item'
12
12
 
13
+ require_relative 'color_item'
13
14
  require_relative 'contact_item'
14
15
  require_relative 'group_item'
15
16
  require_relative 'image_item'
@@ -0,0 +1,201 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'percent_type'
4
+
5
+ module OpenHAB
6
+ module DSL
7
+ module Types
8
+ java_import org.openhab.core.library.types.HSBType
9
+
10
+ # global alias
11
+ ::HSBType = HSBType
12
+
13
+ # Adds methods to core OpenHAB HSBType to make it more natural in Ruby
14
+ class HSBType < PercentType
15
+ # @!parse BLACK = BLACK # @return [HSBType]
16
+ # @!parse WHITE = WHITE # @return [HSBType]
17
+ # @!parse RED = RED # @return [HSBType]
18
+ # @!parse GREEN = GREEN # @return [HSBType]
19
+ # @!parse BLUE = BLUE # @return [HSBType]
20
+
21
+ # conversion to QuantityType doesn't make sense on HSBType
22
+ undef_method :|
23
+
24
+ remove_method :==
25
+
26
+ # r, g, b as an array of symbols
27
+ RGB_KEYS = %i[r g b].freeze
28
+ private_constant :RGB_KEYS
29
+
30
+ class << self
31
+ # @!method from_rgb(r, g, b)
32
+ # Create HSBType from RGB values
33
+ # @param r [Integer] Red component (0-255)
34
+ # @param g [Integer] Green component (0-255)
35
+ # @param b [Integer] Blue component (0-255)
36
+ # @return [HSBType]
37
+
38
+ # @!method from_xy(x, y)
39
+ # Create HSBType representing the provided xy color values in CIE XY color model
40
+ # @param x [Float]
41
+ # @param y [Float]
42
+ # @return [HSBType]
43
+
44
+ # Create HSBType from hue, saturation, and brightness values
45
+ # @param hue [DecimalType, QuantityType, Numeric] Hue component (0-360º)
46
+ # @param saturation [PercentType, Numeric] Saturation component (0-100%)
47
+ # @param brightness [PercentType, Numeric] Brightness component (0-100%)
48
+ # @return [HSBType]
49
+ def from_hsv(hue, saturation, brightness)
50
+ new(hue, saturation, brightness)
51
+ end
52
+
53
+ # add additional "overloads" to the constructor
54
+ # @!visibility private
55
+ def new(*args) # rubocop:disable Metrics
56
+ if args.length == 1 && args.first.respond_to?(:to_str)
57
+ value = args.first.to_str
58
+
59
+ # parse some formats OpenHAB doesn't understand
60
+ # in this case, HTML hex format for rgb
61
+ if (match = value.match(/^#(\h{2})(\h{2})(\h{2})$/))
62
+ rgb = match.to_a[1..3].map { |v| v.to_i(16) }
63
+ logger.trace("creating from rgb #{rgb.inspect}")
64
+ return from_rgb(*rgb)
65
+ end
66
+ end
67
+
68
+ return super unless args.length == 3
69
+
70
+ # convert from several numeric-like types to the exact types
71
+ # OpenHAB needs
72
+ hue = args[0]
73
+ args[0] = if hue.is_a?(DecimalType)
74
+ hue
75
+ elsif hue.is_a?(QuantityType)
76
+ DecimalType.new(hue.to_unit(::Units::DEGREE_ANGLE).to_big_decimal)
77
+ elsif hue.respond_to?(:to_d)
78
+ DecimalType.new(hue)
79
+ end
80
+ args[1..2] = args[1..2].map do |v|
81
+ if v.is_a?(PercentType)
82
+ v
83
+ elsif v.respond_to?(:to_d)
84
+ PercentType.new(v)
85
+ end
86
+ end
87
+
88
+ super(*args)
89
+ end
90
+ end
91
+
92
+ #
93
+ # Comparison
94
+ #
95
+ # @param [NumericType, Items::NumericItem, Items::ColorItem, Numeric, String]
96
+ # other object to compare to
97
+ #
98
+ # @return [Integer, nil] -1, 0, +1 depending on whether +other+ is
99
+ # less than, equal to, or greater than self
100
+ #
101
+ # nil is returned if the two values are incomparable
102
+ #
103
+ def <=>(other)
104
+ logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
105
+ if other.is_a?(Items::ColorItem) ||
106
+ (other.is_a?(Items::GroupItem) && other.base_item.is_a?(ColorItem))
107
+ return false unless other.state?
108
+
109
+ self <=> other.state
110
+ elsif other.respond_to?(:to_str)
111
+ self <=> HSBType.new(other)
112
+ else
113
+ super
114
+ end
115
+ end
116
+
117
+ #
118
+ # Type Coercion
119
+ #
120
+ # Coerce object to a HSBType
121
+ #
122
+ # @param [NumericType, Items::NumericItem, Items::ColorItem, Numeric, String]
123
+ # other object to coerce to a HSBType
124
+ #
125
+ # @return [[HSBType, HSBType]]
126
+ #
127
+ def coerce(other)
128
+ logger.trace("Coercing #{self} as a request from #{other.class}")
129
+ if other.is_a?(Items::NumericItem) ||
130
+ (other.is_a?(Items::GroupItem) && other.base_item.is_a?(Items::NumericItem))
131
+ raise TypeError, "can't convert #{UnDefType} into #{self.class}" unless other.state?
132
+
133
+ [other.state, self]
134
+ elsif other.respond_to?(:to_str)
135
+ [HSBType.new(other.to_str), self]
136
+ else
137
+ super
138
+ end
139
+ end
140
+
141
+ # rename raw methods so we can overwrite them
142
+ # @!visibility private
143
+ alias raw_hue hue
144
+
145
+ # @!attribute [r] hue
146
+ # @return [QuantityType]
147
+ def hue
148
+ QuantityType.new(raw_hue.to_big_decimal, ::Units::DEGREE_ANGLE)
149
+ end
150
+
151
+ # Convert to a packed 32-bit RGB value representing the color in the default sRGB color model.
152
+ #
153
+ # The alpha component is always 100%.
154
+ #
155
+ # @return [Integer]
156
+ alias argb rgb
157
+
158
+ # Convert to a packed 24-bit RGB value representing the color in the default sRGB color model.
159
+ # @return [Integer]
160
+ def rgb
161
+ argb & 0xffffff
162
+ end
163
+
164
+ # Convert to an HTML-style string of 6 hex characters in the default sRGB color model.
165
+ # @return [String] +'#xxxxxx'+
166
+ def to_hex
167
+ Kernel.format('#%06x', rgb)
168
+ end
169
+
170
+ # include units
171
+ # @!visibility private
172
+ def to_s
173
+ "#{hue},#{saturation},#{brightness}"
174
+ end
175
+
176
+ # @!attribute [r] saturation
177
+ # @return [PercentType]
178
+
179
+ # @!attribute [r] brightness
180
+ # @return [PercentType]
181
+
182
+ # @!attribute [r] red
183
+ # @return [PercentType]
184
+
185
+ # @!attribute [r] green
186
+ # @return [PercentType]
187
+
188
+ # @!attribute [r] blue
189
+ # @return [PercentType]
190
+
191
+ # @!method to_rgb
192
+ # Convert to RGB values representing the color in the default sRGB color model
193
+ # @return [[PercentType, PercentType, PercentType]]
194
+
195
+ # @!method to_xy
196
+ # Convert to the xyY values representing this object's color in CIE XY color model
197
+ # @return [[PercentType, PercentType]]
198
+ end
199
+ end
200
+ end
201
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'decimal_type'
4
+
3
5
  module OpenHAB
4
6
  module DSL
5
7
  module Types
@@ -4,6 +4,7 @@ require_relative 'type'
4
4
 
5
5
  require_relative 'date_time_type'
6
6
  require_relative 'decimal_type'
7
+ require_relative 'hsb_type'
7
8
  require_relative 'increase_decrease_type'
8
9
  require_relative 'next_previous_type'
9
10
  require_relative 'open_closed_type'
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.4.0'
8
+ VERSION = '4.5.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.4.0
4
+ version: 4.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
@@ -54,6 +54,7 @@ files:
54
54
  - lib/openhab/dsl/dsl.rb
55
55
  - lib/openhab/dsl/gems.rb
56
56
  - lib/openhab/dsl/group.rb
57
+ - lib/openhab/dsl/items/color_item.rb
57
58
  - lib/openhab/dsl/items/comparable_item.rb
58
59
  - lib/openhab/dsl/items/contact_item.rb
59
60
  - lib/openhab/dsl/items/date_time_item.rb
@@ -105,6 +106,7 @@ files:
105
106
  - lib/openhab/dsl/types/comparable_type.rb
106
107
  - lib/openhab/dsl/types/date_time_type.rb
107
108
  - lib/openhab/dsl/types/decimal_type.rb
109
+ - lib/openhab/dsl/types/hsb_type.rb
108
110
  - lib/openhab/dsl/types/increase_decrease_type.rb
109
111
  - lib/openhab/dsl/types/next_previous_type.rb
110
112
  - lib/openhab/dsl/types/numeric_type.rb