openhab-scripting 5.12.0 → 5.13.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.
@@ -36,8 +36,14 @@ module OpenHAB
36
36
  # Base class for all widgets
37
37
  # @see org.openhab.core.model.sitemap.sitemap.Widget
38
38
  class WidgetBuilder
39
- # This is copied directly out of UIComponentSitemapProvider.java
40
- CONDITION_PATTERN = /(?<item>[A-Za-z]\w*)?\s*(?<condition>==|!=|<=|>=|<|>)?\s*(?<sign>\+|-)?(?<state>.+)/.freeze
39
+ include Core::EntityLookup
40
+
41
+ # This is copied out of UIComponentSitemapProvider.java
42
+ # The original pattern will match plain state e.g. "ON" as item="O" and state="N"
43
+ # this pattern is modified so it matches as item=nil and state="ON" by using atomic grouping `(?>subexpression)`
44
+ # rubocop:disable Layout/LineLength
45
+ CONDITION_PATTERN = /(?>(?<item>[A-Za-z]\w*)?\s*(?<condition>==|!=|<=|>=|<|>))?\s*(?<sign>\+|-)?(?<state>.+)/.freeze
46
+ # rubocop:enable Layout/LineLength
41
47
  private_constant :CONDITION_PATTERN
42
48
 
43
49
  # @return [String, nil]
@@ -45,19 +51,39 @@ module OpenHAB
45
51
  # The item whose state to show
46
52
  # @return [String, Core::Items::Item, nil]
47
53
  attr_accessor :item
48
- # @return [String, nil]
54
+ # The icon to show
55
+ # It can be a string, or a hash of conditions and icons.
56
+ # @example A simple icon
57
+ # sitemaps.build { text icon: "f7:house" }
58
+ #
59
+ # @example A dynamic icon with conditions
60
+ # sitemaps.build do
61
+ # text item: Wifi_Status, icon: {
62
+ # "ON" => "f7:wifi",
63
+ # "OFF" => "f7:wifi_slash",
64
+ # default: "f7:wifi_exclamationmark"
65
+ # }
66
+ # end
67
+ #
68
+ # @return [String, Hash<String, String>, Hash<Array<String>, String>, nil]
49
69
  # @see https://www.openhab.org/docs/ui/sitemaps.html#icons
50
70
  attr_accessor :icon
71
+ # The static icon to show
72
+ # This is mutually exclusive with {#icon}
73
+ # @return [String, nil]
74
+ # @since openHAB 4.1
75
+ # @see https://www.openhab.org/docs/ui/sitemaps.html#element-types
76
+ attr_accessor :static_icon
51
77
  # Label color rules
52
- # @return [Hash<String, String>]
78
+ # @return [Hash<String, String>, Hash<Array<String>, String>]
53
79
  # @see https://www.openhab.org/docs/ui/sitemaps.html#label-value-and-icon-colors
54
80
  attr_reader :label_colors
55
81
  # Value color rules
56
- # @return [Hash<String, String>]
82
+ # @return [Hash<String, String>, Hash<Array<String>, String>]
57
83
  # @see https://www.openhab.org/docs/ui/sitemaps.html#label-value-and-icon-colors
58
84
  attr_reader :value_colors
59
85
  # Icon color rules
60
- # @return [Hash<String, String>]
86
+ # @return [Hash<String, String>, Hash<Array<String>, String>]
61
87
  # @see https://www.openhab.org/docs/ui/sitemaps.html#label-value-and-icon-colors
62
88
  attr_reader :icon_colors
63
89
  # Visibility rules
@@ -67,16 +93,22 @@ module OpenHAB
67
93
 
68
94
  # @param item [String, Core::Items::Item, nil] The item whose state to show (see {#item})
69
95
  # @param label [String, nil] (see {#label})
70
- # @param icon [String, nil] (see {#icon})
71
- # @param label_color [String, Array<String>, nil] One or more label color rules (see {#label_color})
72
- # @param value_color [String, Array<String>, nil] One or more value color rules (see {#value_color})
73
- # @param icon_color [String, Array<String>, nil] One or more icon color rules (see {#icon_color})
74
- # @param visibility [String, Array<String>, nil] One or more visibility rules (see {#visibility})
96
+ # @param icon [String, Hash<String, String>, Hash<Array<String>, String>, nil] (see {#icon})
97
+ # @param static_icon [String, nil] (see {#static_icon})
98
+ # @param label_color [String, Hash<String, String>, Hash<Array<String>, String>, nil]
99
+ # One or more label color rules (see {#label_color})
100
+ # @param value_color [String, Hash<String, String>, Hash<Array<String>, String>, nil]
101
+ # One or more value color rules (see {#value_color})
102
+ # @param icon_color [String, Hash<String, String>, Hash<Array<String>, String>, nil]
103
+ # One or more icon color rules (see {#icon_color})
104
+ # @param visibility [String, Array<String>, Array<Array<String>>, nil]
105
+ # One or more visibility rules (see {#visibility})
75
106
  # @!visibility private
76
107
  def initialize(type,
77
108
  item: nil,
78
109
  label: nil,
79
110
  icon: nil,
111
+ static_icon: nil,
80
112
  label_color: nil,
81
113
  value_color: nil,
82
114
  icon_color: nil,
@@ -90,6 +122,7 @@ module OpenHAB
90
122
  @item = item
91
123
  @label = label
92
124
  @icon = icon
125
+ @static_icon = static_icon
93
126
  @visibilities = []
94
127
  @label_colors = {}
95
128
  @value_colors = {}
@@ -104,18 +137,21 @@ module OpenHAB
104
137
  # Adds one or more new rules for setting the label color
105
138
  # @return [Hash<String, String>] the current rules
106
139
  def label_color(rules)
140
+ rules = { default: rules } if rules.is_a?(String)
107
141
  @label_colors.merge!(rules)
108
142
  end
109
143
 
110
144
  # Adds one or more new rules for setting the value color
111
145
  # @return [Hash<String, String>] the current rules
112
146
  def value_color(rules)
147
+ rules = { default: rules } if rules.is_a?(String)
113
148
  @value_colors.merge!(rules)
114
149
  end
115
150
 
116
151
  # Adds one or more new rules for setting the icon color
117
152
  # @return [Hash<String, String>] the current rules
118
153
  def icon_color(rules)
154
+ rules = { default: rules } if rules.is_a?(String)
119
155
  @icon_colors.merge!(rules)
120
156
  end
121
157
 
@@ -132,31 +168,24 @@ module OpenHAB
132
168
  item = item.name if item.respond_to?(:name)
133
169
  widget.item = item if item
134
170
  widget.label = @label
135
- widget.icon = @icon
171
+
172
+ raise ArgumentError, "icon and static_icon are mutually exclusive" if icon && static_icon
173
+
174
+ if static_icon
175
+ widget.static_icon = static_icon
176
+ elsif icon.is_a?(String)
177
+ widget.icon = @icon
178
+ elsif icon.is_a?(Hash)
179
+ add_icons(widget)
180
+ elsif !icon.nil?
181
+ raise ArgumentError, "icon must be a String or a Hash"
182
+ end
136
183
 
137
184
  add_colors(widget, :label_color, label_colors)
138
185
  add_colors(widget, :value_color, value_colors)
139
186
  add_colors(widget, :icon_color, icon_colors)
140
187
 
141
- # @deprecated OH 4.1
142
- if SitemapBuilder.factory.respond_to?(:create_condition)
143
- add_conditions(widget, :visibility, visibilities, :create_visibility_rule)
144
- else
145
- visibilities.each do |v|
146
- raise ArgumentError, "AND conditions not supported prior to openHAB 4.1" if v.is_a?(Array)
147
-
148
- unless (match = CONDITION_PATTERN.match(v))
149
- raise ArgumentError, "Syntax error in visibility rule #{v.inspect}"
150
- end
151
-
152
- rule = SitemapBuilder.factory.create_visibility_rule
153
- rule.item = match["item"]
154
- rule.condition = match["condition"]
155
- rule.sign = match["sign"]
156
- rule.state = match["state"]
157
- widget.visibility.add(rule)
158
- end
159
- end
188
+ add_conditions(widget, :visibility, visibilities, :create_visibility_rule)
160
189
 
161
190
  widget
162
191
  end
@@ -173,12 +202,19 @@ module OpenHAB
173
202
 
174
203
  private
175
204
 
176
- def add_colors(widget, method, conditions)
177
- conditions.each do |condition, color|
178
- condition = [condition] unless condition.is_a?(Array)
179
- add_conditions(widget, method, condition, :create_color_array) do |color_array|
180
- color_array.arg = color
181
- end
205
+ def add_colors(widget, method, colors)
206
+ # ensure that the default color is at the end, and make the conditions nil (no conditions)
207
+ colors.delete(:default)&.tap { |default_color| colors.merge!(nil => default_color) }
208
+
209
+ add_conditions(widget, method, colors.keys, :create_color_array) do |color_array, key|
210
+ color_array.arg = colors[key]
211
+ end
212
+ end
213
+
214
+ def add_icons(widget)
215
+ icon.delete(:default)&.tap { |default_icon| icon.merge!(nil => default_icon) }
216
+ add_conditions(widget, :icon_rules, icon.keys, :create_icon_rule) do |icon_array, key|
217
+ icon_array.arg = icon[key]
182
218
  end
183
219
  end
184
220
 
@@ -186,25 +222,22 @@ module OpenHAB
186
222
  return if conditions.empty?
187
223
 
188
224
  object = widget.send(method)
189
- has_and_conditions = conditions.any?(Array)
190
- # @deprecated OH 4.1
191
- if !SitemapBuilder.factory.respond_to?(:create_condition) && has_and_conditions
225
+ # @deprecated OH 4.0
226
+ if conditions.any?(Array) && !SitemapBuilder.factory.respond_to?(:create_condition)
192
227
  raise ArgumentError, "AND conditions not supported prior to openHAB 4.1"
193
228
  end
194
229
 
195
- conditions = [conditions] unless has_and_conditions
196
-
197
230
  conditions.each do |sub_conditions|
198
231
  container = SitemapBuilder.factory.send(container_method)
199
232
 
200
233
  add_conditions_to_container(container, sub_conditions)
201
- yield container if block_given?
234
+ yield container, sub_conditions if block_given?
202
235
  object.add(container)
203
236
  end
204
237
  end
205
238
 
206
239
  def add_conditions_to_container(container, conditions)
207
- # @deprecated OH 4.1
240
+ # @deprecated OH 4.0
208
241
  supports_and_conditions = SitemapBuilder.factory.respond_to?(:create_condition)
209
242
 
210
243
  Array.wrap(conditions).each do |c|
@@ -235,7 +268,7 @@ module OpenHAB
235
268
  attr_accessor :mappings
236
269
 
237
270
  # (see WidgetBuilder#initialize)
238
- # @!method initialize(item: nil, label: nil, icon: nil, mappings: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
271
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, mappings: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
239
272
  # @param mappings [Hash, Array, nil] Mappings from command to label (see {SwitchBuilder#mappings})
240
273
  # @!visibility private
241
274
  def initialize(type, mappings: nil, **kwargs)
@@ -275,7 +308,7 @@ module OpenHAB
275
308
  attr_accessor :step
276
309
 
277
310
  # (see WidgetBuilder#initialize)
278
- # @!method initialize(item: nil, label: nil, icon: nil, range: nil, step: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
311
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, range: nil, step: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
279
312
  # @param range [Range, nil] Allowed range of the value (see {SetpointBuilder#range})
280
313
  # @param step [Numeric,nil] How far the value will change with each button press (see {SetpointBuilder#step})
281
314
  # @!visibility private
@@ -309,7 +342,7 @@ module OpenHAB
309
342
  attr_writer :switch
310
343
 
311
344
  # (see SetpointBuilder#initialize)
312
- # @!method initialize(item: nil, label: nil, icon: nil, range: nil, step: nil, switch: nil, frequency: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
345
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, range: nil, step: nil, switch: nil, frequency: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
313
346
  # @param switch [true, false, nil]
314
347
  # A short press on the item toggles the item on or off (see {SliderBuilder#switch=})
315
348
  # @param frequency [Numeric, nil]
@@ -350,7 +383,7 @@ module OpenHAB
350
383
  attr_reader :encoding
351
384
 
352
385
  # (see WidgetBuilder#initialize)
353
- # @!method initialize(item: nil, label: nil, icon: nil, url: nil, encoding: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
386
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, url: nil, encoding: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
354
387
  # @param [String, nil] url (see {VideoBuilder#url})
355
388
  # @param [:mjpeg, :hls, nil] encoding (see {VideoBuilder#encoding})
356
389
  # @!visibility private
@@ -408,7 +441,7 @@ module OpenHAB
408
441
  attr_accessor :y_axis_pattern
409
442
 
410
443
  # (see WidgetBuilder#initialize)
411
- # @!method initialize(item: nil, label: nil, icon: nil, service: nil, refresh: nil, period: nil, legend: nil, group: nil, y_axis_pattern: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
444
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, service: nil, refresh: nil, period: nil, legend: nil, group: nil, y_axis_pattern: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
412
445
  # @param service [String, nil]
413
446
  # The persistence service to use (see {ChartBuilder#service})
414
447
  # @param refresh [Numeric, nil)]
@@ -477,7 +510,7 @@ module OpenHAB
477
510
  attr_accessor :height
478
511
 
479
512
  # (see WidgetBuilder#initialize)
480
- # @!method initialize(item: nil, label: nil, icon: nil, height: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
513
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, height: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
481
514
  # @param height [Integer] The number of element rows to fill (see {DefaultBuilder#height})
482
515
  # @!visibility private
483
516
  def initialize(type, height: nil, **kwargs)
@@ -502,7 +535,7 @@ module OpenHAB
502
535
  attr_accessor :url
503
536
 
504
537
  # (see DefaultBuilder#initialize)
505
- # @!method initialize(item: nil, label: nil, icon: nil, url: nil, height: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
538
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, url: nil, height: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
506
539
  # @param url [String, nil] (see {WebviewBuilder#url})
507
540
  # @!visibility private
508
541
  def initialize(type, url: nil, **kwargs)
@@ -528,7 +561,7 @@ module OpenHAB
528
561
  attr_accessor :frequency
529
562
 
530
563
  # (see WidgetBuilder#initialize)
531
- # @!method initialize(item: nil, label: nil, icon: nil, frequency: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
564
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, frequency: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
532
565
  # @param frequency [Numeric, nil] How often to send requests (see {ColorpickerBuilder#frequency})
533
566
  # @!visibility private
534
567
  def initialize(type, frequency: nil, **kwargs)
@@ -563,7 +596,7 @@ module OpenHAB
563
596
  attr_reader :hint
564
597
 
565
598
  # (see WidgetBuilder#initialize)
566
- # @!method initialize(item: nil, label: nil, icon: nil, hint: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
599
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, hint: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
567
600
  # @param [:text, :number, :date, :time, :datetime, nil] hint
568
601
  # Gives a hint to the user interface to use a widget adapted to a specific use (see {InputBuilder#hint})
569
602
  # @!visibility private
@@ -588,11 +621,87 @@ module OpenHAB
588
621
  end
589
622
  end
590
623
 
624
+ # Builds a `Buttongrid` element
625
+ # @since openHAB 4.1
626
+ # @see https://www.openhab.org/docs/ui/sitemaps.html#element-type-buttongrid
627
+ # @see org.openhab.core.model.sitemap.sitemap.Buttongrid
628
+ class ButtongridBuilder < WidgetBuilder
629
+ # @return [Array<Array<int, int, Command, String, String>>]
630
+ # An array of buttons to display
631
+ attr_reader :buttons
632
+
633
+ # (see WidgetBuilder#initialize)
634
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, buttons: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
635
+ # @param [Array<Array<int, int, Command, String, String>>] buttons An array of buttons to display.
636
+ # Each element is an array with the following elements:
637
+ # - row: 1-12
638
+ # - column: 1-12
639
+ # - command: The command to send when the button is pressed
640
+ # - label: The label to display on the button
641
+ # - icon: The icon to display on the button (optional)
642
+ #
643
+ # @example
644
+ # # This creates a buttongrid to emulate a TV remote control
645
+ # sitemaps.build do
646
+ # buttongrid item: LivingRoom_TV_RCButton, buttons: [
647
+ # [1, 1, "BACK", "Back", "f7:return"],
648
+ # [1, 2, "HOME", "Menu", "material:apps"],
649
+ # [1, 3, "YELLOW", "Search", "f7:search"],
650
+ # [2, 2, "UP", "Up", "f7:arrowtriangle_up"],
651
+ # [4, 2, "DOWN", "Down", "f7:arrowtriangle_down"],
652
+ # [3, 1, "LEFT", "Left", "f7:arrowtriangle_left"],
653
+ # [3, 3, "RIGHT", "Right", "f7:arrowtriangle_right"],
654
+ # [3, 2, "ENTER", "Enter", "material:adjust"]
655
+ # ]
656
+ # end
657
+ #
658
+ # @see https://www.openhab.org/docs/ui/sitemaps.html#element-type-buttongrid
659
+ # @!visibility private
660
+ def initialize(type, buttons: [], **kwargs)
661
+ super(type, **kwargs)
662
+ buttons.each { |button| validate_button(button) }
663
+ @buttons = buttons
664
+ end
665
+
666
+ #
667
+ # Adds a button to the buttongrid
668
+ #
669
+ # @param [Array<int, int, Command, String, String>] button the button to add
670
+ # @return [Array<Array<int, int, Command, String, String>>] the current buttons
671
+ #
672
+ def button(button)
673
+ validate_button(button)
674
+ @buttons << button
675
+ end
676
+
677
+ # @!visibility private
678
+ def build
679
+ widget = super
680
+ buttons.each do |button|
681
+ button_object = SitemapBuilder.factory.create_button
682
+ button_object.row = button[0]
683
+ button_object.column = button[1]
684
+ button_object.cmd = button[2]
685
+ button_object.label = button[3]
686
+ button_object.icon = button[4] if button[4]
687
+ widget.buttons.add(button_object)
688
+ end
689
+
690
+ widget
691
+ end
692
+
693
+ private
694
+
695
+ def validate_button(button)
696
+ return if (4..5).cover?(button.size)
697
+
698
+ raise ArgumentError, "Invalid button: '#{button.inspect}'. It must be an array with (4..5) elements"
699
+ end
700
+ end
701
+
591
702
  # Parent class for builders of widgets that can contain other widgets.
592
703
  # @see org.openhab.core.model.sitemap.sitemap.LinkableWidget
593
704
  class LinkableWidgetBuilder < WidgetBuilder
594
- include Core::EntityLookup
595
-
596
705
  # allow referring to items that don't exist yet
597
706
  self.create_dummy_items = true
598
707
 
@@ -609,6 +718,7 @@ module OpenHAB
609
718
  # def frame(item: nil,
610
719
  # label: nil,
611
720
  # icon: nil,
721
+ # static_icon: nil,
612
722
  # label_color: nil,
613
723
  # value_color: nil,
614
724
  # icon_color: nil,
@@ -623,6 +733,7 @@ module OpenHAB
623
733
  # def text(item: nil,
624
734
  # label: nil,
625
735
  # icon: nil,
736
+ # static_icon: nil,
626
737
  # label_color: nil,
627
738
  # value_color: nil,
628
739
  # icon_color: nil,
@@ -637,6 +748,7 @@ module OpenHAB
637
748
  # def group(item: nil,
638
749
  # label: nil,
639
750
  # icon: nil,
751
+ # static_icon: nil,
640
752
  # label_color: nil,
641
753
  # value_color: nil,
642
754
  # icon_color: nil,
@@ -651,6 +763,7 @@ module OpenHAB
651
763
  # def image(item: nil,
652
764
  # label: nil,
653
765
  # icon: nil,
766
+ # static_icon: nil,
654
767
  # url: nil,
655
768
  # refresh: nil,
656
769
  # label_color: nil,
@@ -667,6 +780,7 @@ module OpenHAB
667
780
  # def video(item: nil,
668
781
  # label: nil,
669
782
  # icon: nil,
783
+ # static_icon: nil,
670
784
  # url: nil,
671
785
  # encoding: nil,
672
786
  # label_color: nil,
@@ -683,6 +797,7 @@ module OpenHAB
683
797
  # def chart(item: nil,
684
798
  # label: nil,
685
799
  # icon: nil,
800
+ # static_icon: nil,
686
801
  # service: nil,
687
802
  # refresh: nil,
688
803
  # period: nil,
@@ -703,6 +818,7 @@ module OpenHAB
703
818
  # def webview(item: nil,
704
819
  # label: nil,
705
820
  # icon: nil,
821
+ # static_icon: nil,
706
822
  # url: nil,
707
823
  # height: nil,
708
824
  # label_color: nil,
@@ -719,6 +835,7 @@ module OpenHAB
719
835
  # def switch(item: nil,
720
836
  # label: nil,
721
837
  # icon: nil,
838
+ # static_icon: nil,
722
839
  # mappings: nil,
723
840
  # label_color: nil,
724
841
  # value_color: nil,
@@ -734,6 +851,7 @@ module OpenHAB
734
851
  # def mapview(item: nil,
735
852
  # label: nil,
736
853
  # icon: nil,
854
+ # static_icon: nil,
737
855
  # height: nil,
738
856
  # label_color: nil,
739
857
  # value_color: nil,
@@ -749,6 +867,7 @@ module OpenHAB
749
867
  # def slider(item: nil,
750
868
  # label: nil,
751
869
  # icon: nil,
870
+ # static_icon: nil,
752
871
  # range: nil,
753
872
  # step: nil,
754
873
  # switch: nil,
@@ -767,6 +886,7 @@ module OpenHAB
767
886
  # def selection(item: nil,
768
887
  # label: nil,
769
888
  # icon: nil,
889
+ # static_icon: nil,
770
890
  # mappings: nil,
771
891
  # label_color: nil,
772
892
  # value_color: nil,
@@ -783,6 +903,7 @@ module OpenHAB
783
903
  # def input(item: nil,
784
904
  # label: nil,
785
905
  # icon: nil,
906
+ # static_icon: nil,
786
907
  # hint: nil,
787
908
  # label_color: nil,
788
909
  # value_color: nil,
@@ -790,6 +911,23 @@ module OpenHAB
790
911
  # visibility: nil)
791
912
  # end
792
913
  #
914
+ # # (see ButtongridBuilder#initialize)
915
+ # # Create a new `Buttongrid` element.
916
+ # # @yield Block executed in the context of an {ButtongridBuilder}
917
+ # # @return [ButtongridBuilder]
918
+ # # @since openHAB 4.1
919
+ # # @!visibility public
920
+ # def buttongrid(item: nil,
921
+ # label: nil,
922
+ # icon: nil,
923
+ # static_icon: nil,
924
+ # buttons: nil,
925
+ # label_color: nil,
926
+ # value_color: nil,
927
+ # icon_color: nil,
928
+ # visibility: nil)
929
+ # end
930
+ #
793
931
  # # (see SetpointBuilder#initialize)
794
932
  # # Create a new `Setpoint` element.
795
933
  # # @yield Block executed in the context of a {SetpointBuilder}
@@ -798,6 +936,7 @@ module OpenHAB
798
936
  # def setpoint(item: nil,
799
937
  # label: nil,
800
938
  # icon: nil,
939
+ # static_icon: nil,
801
940
  # range: nil,
802
941
  # step: nil,
803
942
  # label_color: nil,
@@ -814,6 +953,7 @@ module OpenHAB
814
953
  # def colorpicker(item: nil,
815
954
  # label: nil,
816
955
  # icon: nil,
956
+ # static_icon: nil,
817
957
  # frequency: nil,
818
958
  # label_color: nil,
819
959
  # value_color: nil,
@@ -829,6 +969,7 @@ module OpenHAB
829
969
  # def default(item: nil,
830
970
  # label: nil,
831
971
  # icon: nil,
972
+ # static_icon: nil,
832
973
  # height: nil,
833
974
  # label_color: nil,
834
975
  # value_color: nil,
@@ -849,6 +990,7 @@ module OpenHAB
849
990
  slider
850
991
  selection
851
992
  input
993
+ buttongrid
852
994
  setpoint
853
995
  colorpicker
854
996
  default].each do |method|
@@ -865,7 +1007,7 @@ module OpenHAB
865
1007
  end
866
1008
 
867
1009
  # (see WidgetBuilder#initialize)
868
- # @!method initialize(item: nil, label: nil, icon: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
1010
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
869
1011
  # @!visibility private
870
1012
  def initialize(*, **)
871
1013
  super
@@ -915,7 +1057,7 @@ module OpenHAB
915
1057
  attr_accessor :refresh
916
1058
 
917
1059
  # (see LinkableWidgetBuilder#initialize)
918
- # @!method initialize(item: nil, label: nil, icon: nil, url: nil, refresh: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
1060
+ # @!method initialize(item: nil, label: nil, icon: nil, static_icon: nil, url: nil, refresh: nil, label_color: nil, value_color: nil, icon_color: nil, visibility: nil)
919
1061
  # @param url [String, nil] The URL for the image (see {ImageBuilder#url})
920
1062
  # @param refresh [Numeric, nil] How often to refresh the image (see {ImageBuilder#refresh})
921
1063
  # @!visibility private
@@ -32,6 +32,36 @@ module OpenHAB
32
32
  # end
33
33
  # end
34
34
  #
35
+ # @example Create a Thing within a Bridge
36
+ # things.build do
37
+ # bridge "mqtt:broker:mosquitto", config: { host: "127.0.0.1", enableDiscovery: false } do
38
+ # thing "mqtt:topic:window1", "My Window Sensor" do
39
+ # channel "contact1", "contact", config: {
40
+ # stateTopic: "zigbee2mqtt/window1/contact",
41
+ # on: "false",
42
+ # off: "true"
43
+ # }
44
+ # end
45
+ # end
46
+ # end
47
+ #
48
+ # items.build do
49
+ # contact_item Window1_Contact, channel: "mqtt:topic:window1:contact1"
50
+ # end
51
+ #
52
+ # @example Create a Thing separately from the Bridge
53
+ # things.build do
54
+ # bridge = bridge "mqtt:broker:mosquitto", config: { host: "127.0.0.1", enableDiscovery: false }
55
+ #
56
+ # thing "mqtt:topic:window1", "My Window Sensor", bridge: bridge do
57
+ # channel "contact1", "contact", config: {
58
+ # stateTopic: "zigbee2mqtt/window1/contact",
59
+ # on: "false",
60
+ # off: "true"
61
+ # }
62
+ # end
63
+ # end
64
+ #
35
65
  # @see ThingBuilder#initialize ThingBuilder#initialize for #thing's parameters
36
66
  # @see ChannelBuilder#initialize ChannelBuilder#initialize for #channel's parameters
37
67
  # @see Items::Builder
@@ -61,6 +91,7 @@ module OpenHAB
61
91
 
62
92
  def build(klass, *args, **kwargs, &block)
63
93
  builder = klass.new(*args, **kwargs)
94
+ builder.parent_builder = self if builder.respond_to?(:parent_builder=)
64
95
  builder.instance_eval(&block) if block
65
96
  thing = builder.build
66
97
 
@@ -190,6 +221,7 @@ module OpenHAB
190
221
  @location = location.label if location.is_a?(Item)
191
222
  @config = config.transform_keys(&:to_s)
192
223
  @enabled = enabled
224
+ @builder = org.openhab.core.thing.binding.builder.ThingBuilder unless instance_variable_defined?(:@builder)
193
225
  end
194
226
 
195
227
  # Add an explicitly configured channel to this item
@@ -222,13 +254,12 @@ module OpenHAB
222
254
  @channels = merged_channels.values
223
255
  end
224
256
 
225
- builder = org.openhab.core.thing.binding.builder.ThingBuilder
226
- .create(thing_type_uid, uid)
227
- .with_label(label)
228
- .with_location(location)
229
- .with_configuration(configuration)
230
- .with_bridge(bridge_uid)
231
- .with_channels(channels)
257
+ builder = @builder.create(thing_type_uid, uid)
258
+ .with_label(label)
259
+ .with_location(location)
260
+ .with_configuration(configuration)
261
+ .with_bridge(bridge_uid)
262
+ .with_channels(channels)
232
263
 
233
264
  builder.with_properties(thing_type.properties) if thing_type
234
265
 
@@ -244,16 +275,26 @@ module OpenHAB
244
275
 
245
276
  # The BridgeBuilder DSL allows you to customize a thing
246
277
  class BridgeBuilder < ThingBuilder
278
+ # @!visibility private
279
+ attr_accessor :parent_builder
280
+
281
+ # Constructor for BridgeBuilder
282
+ # @see ThingBuilder#initialize
283
+ def initialize(uid, label = nil, binding: nil, type: nil, bridge: nil, location: nil, config: {}, enabled: nil)
284
+ @builder = org.openhab.core.thing.binding.builder.BridgeBuilder
285
+ super
286
+ end
287
+
247
288
  # Create a new Bridge with this Bridge as its Bridge
248
289
  # @see BridgeBuilder#initialize
249
290
  def bridge(*args, **kwargs, &block)
250
- super(*args, bridge: self, **kwargs, &block)
291
+ parent_builder.bridge(*args, bridge: self, **kwargs, &block)
251
292
  end
252
293
 
253
294
  # Create a new Thing with this Bridge as its Bridge
254
295
  # @see ThingBuilder#initialize
255
296
  def thing(*args, **kwargs, &block)
256
- super(*args, bridge: self, **kwargs, &block)
297
+ parent_builder.thing(*args, bridge: self, **kwargs, &block)
257
298
  end
258
299
  end
259
300
 
@@ -268,13 +309,6 @@ module OpenHAB
268
309
  :description,
269
310
  :auto_update_policy
270
311
 
271
- class << self
272
- # @!visibility private
273
- def channel_type_registry
274
- @channel_type_registry ||= OSGi.service("org.openhab.core.thing.type.ChannelTypeRegistry")
275
- end
276
- end
277
-
278
312
  #
279
313
  # Constructor for ChannelBuilder
280
314
  #
@@ -283,7 +317,7 @@ module OpenHAB
283
317
  # @param [String] uid The channel's ID.
284
318
  # @param [String, ChannelTypeUID, :trigger] type The concrete type of the channel.
285
319
  # @param [String] label The channel label.
286
- # @param [thing] thing The thing associated with this channel.
320
+ # @param [Thing] thing The thing associated with this channel.
287
321
  # This parameter is not needed for the {ThingBuilder#channel} method.
288
322
  # @param [String] description The channel description.
289
323
  # @param [String] group The group name.
@@ -355,7 +389,7 @@ module OpenHAB
355
389
  # @!attribute [r] accepted_item_type
356
390
  # @return [String] The accepted item type.
357
391
  def accepted_item_type
358
- @accepted_item_type ||= self.class.channel_type_registry.get_channel_type(type)&.item_type
392
+ @accepted_item_type ||= type.channel_type&.item_type
359
393
  end
360
394
 
361
395
  private