Class: OpenHAB::DSL::Items::ItemBuilder
- Inherits:
-
Object
- Object
- OpenHAB::DSL::Items::ItemBuilder
- Includes:
- Core::EntityLookup
- Defined in:
- lib/openhab/dsl/items/builder.rb
Overview
The ItemBuilder DSL allows you to customize an Item
Direct Known Subclasses
Instance Attribute Summary collapse
-
#autoupdate ⇒ true, ...
Autoupdate setting.
-
#channels ⇒ Array<String, Symbol, Core::Things::ChannelUID, Core::Things::Channel, Array>
Channel to link the item to.
-
#command_options ⇒ Hash, ...
A list of valid commands If a hash, keys are commands, and values are labels.
-
#dimension ⇒ String?
Unit dimension (for number items only).
-
#format ⇒ String?
The formatting pattern for the item's state.
-
#groups ⇒ Array<String, GroupItem>
Groups to which this item should be added.
-
#icon ⇒ Symbol, ...
The icon to be associated with the item.
-
#label ⇒ String?
Item label.
- #metadata ⇒ Core::Items::Metadata::NamespaceHash readonly
-
#name ⇒ String
Item name.
-
#range ⇒ Range?
The valid range for a number item.
-
#read_only ⇒ true, ...
(also: #read_only?)
If the item is read-only, and does not accept commands.
-
#state ⇒ Core::Types::State
Initial state.
-
#state_options ⇒ Hash, ...
A list of valid states If a hash, keys are commands, and values are labels.
-
#step ⇒ Number?
The step size for a number item.
-
#tags ⇒ Array<String>
Tags to apply to this item.
-
#thing ⇒ String, ...
Thing from which to resolve relative channel ids.
-
#type ⇒ Symbol
readonly
The type of this item.
-
#unit ⇒ String?
Unit (for number items only).
Instance Method Summary collapse
-
#alexa(value, config = nil) ⇒ void
Shortcut for adding Alexa metadata.
-
#channel(*channels) ⇒ Array<String, Symbol, Core::Things::ChannelUID, Core::Things::Channel, Array>
(also: #channels)
Add a channel link to this item.
-
#expire(duration, command: nil, state: nil, ignore_state_updates: nil, ignore_commands: nil) ⇒ void
Configure item expiration.
-
#ga(value, config = nil) ⇒ void
Shortcut for adding Google Assistant metadata.
-
#group(*groups) ⇒ Array<String, GroupItemBulder, GroupItem>
(also: #groups)
Add this item to a group.
-
#homekit(value, config = nil) ⇒ void
Shortcut for adding Homekit metadata.
-
#initialize(type, name = nil, label = nil, provider:, dimension: nil, unit: nil, format: nil, range: nil, step: nil, read_only: nil, command_options: nil, state_options: nil, icon: nil, group: nil, groups: nil, tag: nil, tags: nil, autoupdate: nil, thing: nil, channel: nil, channels: nil, expire: nil, alexa: nil, ga: nil, homekit: nil, matter: nil, metadata: nil, state: nil, persist: nil) ⇒ ItemBuilder
constructor
A new instance of ItemBuilder.
- #inspect ⇒ String
-
#matter(value, config = nil) ⇒ void
Shortcut for adding Matter metadata.
-
#persist(strategies = [], service: nil, every: nil, cron: nil, filter: nil) ⇒ void
Configure automatic state persistence.
-
#tag(*tags) ⇒ Array<String>
(also: #tags)
Tag item.
-
#to_s ⇒ String
The item's label if one is defined, otherwise its name.
Methods included from Core::EntityLookup
#items, #method_missing, #things
Constructor Details
#initialize(type, name = nil, label = nil, provider:, dimension: nil, unit: nil, format: nil, range: nil, step: nil, read_only: nil, command_options: nil, state_options: nil, icon: nil, group: nil, groups: nil, tag: nil, tags: nil, autoupdate: nil, thing: nil, channel: nil, channels: nil, expire: nil, alexa: nil, ga: nil, homekit: nil, matter: nil, metadata: nil, state: nil, persist: nil) ⇒ ItemBuilder
Returns a new instance of ItemBuilder.
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/openhab/dsl/items/builder.rb', line 330 def initialize(type, name = nil, label = nil, provider:, dimension: nil, unit: nil, format: nil, range: nil, step: nil, read_only: nil, command_options: nil, state_options: nil, icon: nil, group: nil, groups: nil, tag: nil, tags: nil, autoupdate: nil, thing: nil, channel: nil, channels: nil, expire: nil, alexa: nil, ga: nil, # rubocop:disable Naming/MethodParameterName homekit: nil, matter: nil, metadata: nil, state: nil, persist: nil) raise ArgumentError, "`name` cannot be nil" if name.nil? if dimension raise ArgumentError, "`dimension` can only be specified with NumberItem" unless type == :number begin org.openhab.core.types.util.UnitUtils.parse_dimension(dimension.to_s) rescue java.lang.IllegalArgumentException raise ArgumentError, "Invalid dimension '#{dimension}'" end end name = name.name if name.respond_to?(:name) if provider.is_a?(GroupItemBuilder) name = "#{provider.name_base}#{name}" label = "#{provider.label_base}#{label}".strip if label end @provider = provider @type = type @name = name.to_s @label = label @dimension = dimension @format = format @range = range @step = step @read_only = read_only @command_options = command_options @state_options = state_options self.unit = unit @icon = icon @groups = [] @tags = [] @metadata = Core::Items::Metadata::NamespaceHash.new @metadata.merge!(metadata) if metadata @autoupdate = autoupdate @channels = [] @thing = thing @expire = nil if expire expire = Array(expire) expire_config = expire.pop if expire.last.is_a?(Hash) expire_config ||= {} self.expire(*expire, **expire_config) end self.alexa(alexa) if alexa self.ga(ga) if ga self.homekit(homekit) if homekit self.matter(matter) if matter self.state = state @persistence = [] if persist.is_a?(Hash) self.persist(**persist) elsif persist self.persist(persist) end self.group(*group) self.groups(*groups) self.tag(*tag) self.tags(*tags) self.channel(*channel) self.channels(*channels) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class OpenHAB::Core::EntityLookup
Instance Attribute Details
#autoupdate ⇒ true, ...
Autoupdate setting
258 259 260 |
# File 'lib/openhab/dsl/items/builder.rb', line 258 def autoupdate @autoupdate end |
#channels=(value) ⇒ Array<String, Symbol, Core::Things::ChannelUID, Core::Things::Channel, Array>
Returns Channel to link the item to.
|
|
# File 'lib/openhab/dsl/items/builder.rb', line 273
|
#command_options ⇒ Hash, ...
A list of valid commands If a hash, keys are commands, and values are labels
248 249 250 |
# File 'lib/openhab/dsl/items/builder.rb', line 248 def command_options @command_options end |
#dimension ⇒ String?
Unit dimension (for number items only)
If #unit is provided, and #dimension is not, it will be inferred.
224 225 226 |
# File 'lib/openhab/dsl/items/builder.rb', line 224 def dimension @dimension end |
#format ⇒ String?
234 235 236 |
# File 'lib/openhab/dsl/items/builder.rb', line 234 def format @format end |
#groups=(value) ⇒ Array<String, GroupItem>
Returns Groups to which this item should be added.
|
|
# File 'lib/openhab/dsl/items/builder.rb', line 277
|
#icon ⇒ Symbol, ...
The icon to be associated with the item
255 256 257 |
# File 'lib/openhab/dsl/items/builder.rb', line 255 def icon @icon end |
#label ⇒ String?
Item label
218 219 220 |
# File 'lib/openhab/dsl/items/builder.rb', line 218 def label @label end |
#metadata ⇒ Core::Items::Metadata::NamespaceHash (readonly)
263 264 265 |
# File 'lib/openhab/dsl/items/builder.rb', line 263 def metadata @metadata end |
#name ⇒ String
Item name
215 216 217 |
# File 'lib/openhab/dsl/items/builder.rb', line 215 def name @name end |
#range ⇒ Range?
The valid range for a number item
237 238 239 |
# File 'lib/openhab/dsl/items/builder.rb', line 237 def range @range end |
#read_only ⇒ true, ... Also known as: read_only?
If the item is read-only, and does not accept commands
243 244 245 |
# File 'lib/openhab/dsl/items/builder.rb', line 243 def read_only @read_only end |
#state ⇒ Core::Types::State
Initial state
If #state is set to a QuantityType, and #unit is not set, it will be inferred.
269 270 271 |
# File 'lib/openhab/dsl/items/builder.rb', line 269 def state @state end |
#state_options ⇒ Hash, ...
A list of valid states If a hash, keys are commands, and values are labels
252 253 254 |
# File 'lib/openhab/dsl/items/builder.rb', line 252 def state_options @state_options end |
#step ⇒ Number?
The step size for a number item
240 241 242 |
# File 'lib/openhab/dsl/items/builder.rb', line 240 def step @step end |
#tags=(value) ⇒ Array<String>
Returns Tags to apply to this item.
|
|
# File 'lib/openhab/dsl/items/builder.rb', line 280
|
#thing ⇒ String, ...
Returns Thing from which to resolve relative channel ids.
261 262 263 |
# File 'lib/openhab/dsl/items/builder.rb', line 261 def thing @thing end |
#type ⇒ Symbol (readonly)
The type of this item
212 213 214 |
# File 'lib/openhab/dsl/items/builder.rb', line 212 def type @type end |
#unit ⇒ String?
Unit (for number items only)
228 229 230 |
# File 'lib/openhab/dsl/items/builder.rb', line 228 def unit @unit end |
Instance Method Details
#alexa(value, config = nil) ⇒ void
This method returns an undefined value.
Shortcut for adding Alexa metadata
|
|
# File 'lib/openhab/dsl/items/builder.rb', line 524
|
#channel(channel, config = {}) ⇒ Array<String, Symbol, Core::Things::ChannelUID, Core::Things::Channel, Array> #channels ⇒ Array<String, Symbol, Core::Things::ChannelUID, Core::Things::Channel, Array> #channels(*channels) ⇒ Array<String, Symbol, Core::Things::ChannelUID, Core::Things::Channel, Array> Also known as: channels
Add a channel link to this item.
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
# File 'lib/openhab/dsl/items/builder.rb', line 627 def channel(*channels) return @channels if channels.empty? channels = [channels] if channels.length == 2 && channels[1].is_a?(Hash) channels.each do |channel| orig_channel = channel channel = channel.first if channel.is_a?(Array) next if channel.is_a?(String) || channel.is_a?(Symbol) || channel.is_a?(Core::Things::ChannelUID) || channel.is_a?(Core::Things::Channel) raise ArgumentError, "channel #{orig_channel.inspect} must be a `String`, `Symbol`, `ChannelUID`, or " \ "`Channel`, or a two element array with the first element those types, and the " \ "second element a Hash" end @channels.concat(channels) end |
#expire(duration, command: nil, state: nil, ignore_state_updates: nil, ignore_commands: nil) ⇒ void
This method returns an undefined value.
Configure item expiration
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
# File 'lib/openhab/dsl/items/builder.rb', line 679 def expire(*args, command: nil, state: nil, ignore_state_updates: nil, ignore_commands: nil) unless (0..2).cover?(args.length) raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..2)" end return @expire if args.empty? state = args.last if args.length == 2 raise ArgumentError, "cannot provide both command and state" if command && state duration = args.first return @expire = nil if duration.nil? duration = duration.to_s[2..].downcase if duration.is_a?(Duration) state = "'#{state}'" if state.respond_to?(:to_str) && type == :string value = duration value += ",state=#{state}" if state value += ",command=#{command}" if command config = { ignoreStateUpdates: ignore_state_updates, ignoreCommands: ignore_commands } config.compact! @expire = [value, config] end |
#ga(value, config = nil) ⇒ void
This method returns an undefined value.
Shortcut for adding Google Assistant metadata
|
|
# File 'lib/openhab/dsl/items/builder.rb', line 535
|
#group(group) ⇒ Array<String, GroupItemBulder, GroupItem> #groups ⇒ Array<String, GroupItemBulder, GroupItem> #groups(*groups) ⇒ Array<String, GroupItemBulder, GroupItem> Also known as: groups
Add this item to a group
511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/openhab/dsl/items/builder.rb', line 511 def group(*groups) return @groups if groups.empty? unless groups.all? do |group| group.is_a?(String) || group.is_a?(Core::Items::GroupItem) || group.is_a?(GroupItemBuilder) end raise ArgumentError, "`group` must be a `GroupItem`, `GroupItemBuilder`, or a `String`" end @groups.concat(groups) end |
#homekit(value, config = nil) ⇒ void
This method returns an undefined value.
Shortcut for adding Homekit metadata
|
|
# File 'lib/openhab/dsl/items/builder.rb', line 546
|
#inspect ⇒ String
786 787 788 789 790 791 792 793 |
# File 'lib/openhab/dsl/items/builder.rb', line 786 def inspect s = "#<OpenHAB::Core::Items::#{inspect_type}ItemBuilder#{type_details} #{name} #{label.inspect}" s += " category=#{icon.inspect}" if icon s += " tags=#{tags.inspect}" unless tags.empty? s += " groups=#{groups.map { |g| g.respond_to?(:name) ? g.name : g }.inspect}" unless groups.empty? s += " metadata=#{metadata.to_h.inspect}" unless metadata.empty? "#{s}>" end |
#matter(value, config = nil) ⇒ void
This method returns an undefined value.
Shortcut for adding Matter metadata
568 569 570 571 572 573 |
# File 'lib/openhab/dsl/items/builder.rb', line 568 %i[alexa ga homekit matter].each do |shortcut| define_method(shortcut) do |value = nil, config = nil| value, config = value if value.is_a?(Array) metadata[shortcut] = [value, config] end end |
#persist(strategies = [], service: nil, every: nil, cron: nil, filter: nil) ⇒ void
This method returns an undefined value.
Configure automatic state persistence.
Persistence must be configured through the UI to use this feature. The selected
service must already have an editable configuration. Configurations supplied by
.persist files are read-only through the framework and will raise an error.
If the persistence service already has configuration for this item, it will be replaced. If no persistence configuration is defined in the builder, any existing configuration is retained. Transient items created in Ruby also remove their persistence configuration when the item is unloaded.
454 455 456 |
# File 'lib/openhab/dsl/items/builder.rb', line 454 def persist(strategies = [], service: nil, every: nil, cron: nil, filter: nil) @persistence << Builder::Persistence.declaration(strategies, service:, every:, cron:, filter:) end |
#tag(tag) ⇒ Array<String> #tags ⇒ Array<String> #tags(*tags) ⇒ Array<String> Also known as:
Tag item
488 489 490 491 492 |
# File 'lib/openhab/dsl/items/builder.rb', line 488 def tag(*tags) return @tags if tags.empty? @tags.concat(Tags.normalize(*tags)) end |
#to_s ⇒ String
The item's label if one is defined, otherwise its name.
468 469 470 |
# File 'lib/openhab/dsl/items/builder.rb', line 468 def to_s label || name end |