Class: OpenHAB::Core::Items::Registry

Inherits:
Object
  • Object
show all
Includes:
LazyArray
Defined in:
lib/openhab/core/items/registry.rb

Overview

Provides access to all openHAB items, and acts like an array.

Instance Method Summary collapse

Methods included from LazyArray

#each, #method_missing, #to_ary

Methods included from Enumerable

#all_groups, #all_members, #command, #command!, #decrease, #down, #equipments, #fast_forward, #groups, #increase, #locations, #member_of, #members, #move, #next, #not_member_of, #not_tagged, #off, #on, #pause, #play, #points, #previous, #refresh, #rewind, #stop, #tagged, #toggle, #up, #update, #update!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OpenHAB::Core::LazyArray

Instance Method Details

#[](name) ⇒ Item

Fetches the named item from the the ItemRegistry

Parameters:

  • name (String)

Returns:

  • (Item)

    Item from registry, nil if item missing or requested item is a Group Type



22
23
24
# File 'lib/openhab/core/items/registry.rb', line 22

def [](name)
  EntityLookup.lookup_item(name)
end

#build(preferred_provider = nil, update: true) { ... } ⇒ Object

Enter the Item Builder DSL.

Parameters:

  • update (true, false) (defaults to: true)

    Update existing items with the same name. When false, an error will be raised if an item with the same name already exists.

  • preferred_provider (org.openhab.core.common.registry.Provider, Proc, :persistent, :transient, nil) (defaults to: nil)

    An optional preferred provider to use. Can be one of several types:

    • An explicit instance of ManagedProvider
    • A Proc, which can calculate the preferred provider based on whatever conditions it wants, and then is further processed as this parameter.
    • :persistent, meaning the default ManagedProvider for this registry. Managed providers persist their objects to JSON, and will survive after the Ruby script is unloaded. This is where objects you configure with MainUI are stored. You should use this provider when you're creating something in response to a one-time event.
    • :transient, meaning a ManagedProvider that will remove all of its contents when the Ruby script is unloaded. You should use this if you're generating objects dynamically, either based on some sort of other configuration, or simply hard coded and you're using Ruby as a more expressive way to define things than a .items or .things file. If you don't use this provider for something such as metadata, then you may have issues such as metadata still showing up even though you're no longer creating items with it anymore.
    • nil, meaning to fall back to the current thread setting. See DSL.provider. If there is no thread setting (or the thread setting was Proc that returned nil), it defaults to :transient.

Yields:

Returns:

  • (Object)

    The return value of the block.

Raises:

  • (ArgumentError)

    if an item with the same name already exists and update is false.

  • (FrozenError)

    if update is true but the existing item with the same name wasn't created by the current provider.

See Also:



56
57
58
59
# File 'lib/openhab/core/items/registry.rb', line 56

def build(preferred_provider = nil, update: true, &block)
  DSL::Items::BaseBuilderDSL.new(preferred_provider, update: update)
                            .instance_eval_with_dummy_items(&block)
end

#key?(name) ⇒ true, false Also known as: include?, has_key?

Returns true if the given item name exists

Parameters:

  • name (String)

    Item name to check

Returns:

  • (true, false)

    true if the item exists, false otherwise



29
30
31
# File 'lib/openhab/core/items/registry.rb', line 29

def key?(name)
  !$ir.get(name).nil?
end

#remove(item_name, recursive: false) ⇒ Item?

Remove an item.

The item must be a managed item (typically created by Ruby or in the UI).

Any associated metadata or channel links are also removed.

Parameters:

  • item_name (String, Item)
  • recursive (true, false) (defaults to: false)

    Remove the item's members if it's a group

Returns:

  • (Item, nil)

    The removed item, if found.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/openhab/core/items/registry.rb', line 71

def remove(item_name, recursive: false)
  item_name = item_name.name if item_name.is_a?(Item)
  provider = Provider.registry.provider_for(item_name)
  unless provider.is_a?(ManagedProvider)
    raise "Cannot remove item #{item_name} from non-managed provider #{provider.inspect}"
  end

  Metadata::Provider.registry.providers.grep(ManagedProvider).each do |managed_provider|
    managed_provider.remove_item_metadata(item_name)
  end

  Things::Links::Provider.registry.providers.grep(ManagedProvider).each do |managed_provider|
    managed_provider.remove_links_for_item(item_name)
  end
  provider.remove(item_name, recursive)
end

#to_aArray

Explicit conversion to array

Returns:



38
39
40
# File 'lib/openhab/core/items/registry.rb', line 38

def to_a
  $ir.items.map { |item| Proxy.new(item) }
end