Class: OpenHAB::Core::Things::Registry

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

Overview

Provides access to all openHAB things, 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

#[](uid) ⇒ Thing? Also known as: include?, key?, has_key?

Gets a specific Thing

Parameters:

  • uid (String, ThingUID)

    Thing UID in the format binding_id:type_id:thing_id or via the ThingUID

Returns:



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

def [](uid)
  EntityLookup.lookup_thing(uid)
end

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

Enter the Thing Builder DSL.

Parameters:

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

    When true, existing things with the same name will be redefined if they're different. When false, an error will be raised if a thing with the same uid 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 result of the block.

Raises:

  • (ArgumentError)

    if a thing with the same uid already exists and update is false.

  • (FrozenError)

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



51
52
53
# File 'lib/openhab/core/things/registry.rb', line 51

def build(preferred_provider = nil, update: true, &block)
  DSL::Things::Builder.new(preferred_provider, update: update).instance_eval(&block)
end

#remove(thing_uid) ⇒ Thing?

Remove a Thing.

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

Parameters:

Returns:

  • (Thing, nil)

    The removed item, if found.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/openhab/core/things/registry.rb', line 62

def remove(thing_uid)
  thing_uid = thing.uid if thing_uid.is_a?(Thing)
  thing_uid = ThingUID.new(thing_uid) if thing_uid.is_a?(String)
  provider = Provider.registry.provider_for(thing_uid)
  unless provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
    raise "Cannot remove thing #{thing_uid} from non-managed provider #{provider.inspect}"
  end

  Links::Provider.registry.providers.grep(ManagedProvider).each do |managed_provider|
    managed_provider.remove_links_for_thing(thing_uid)
  end

  provider.remove(thing_uid)
end

#to_aArray<Thing>

Explicit conversion to array

Returns:



35
36
37
# File 'lib/openhab/core/things/registry.rb', line 35

def to_a
  $things.all.map { |thing| Proxy.new(thing) }
end