Module: OpenHAB::Core::EntityLookup

Overview

Note:

Thing UIDs are separated by a colon ‘:`. Since it is not a valid symbol for an identifier, it must be replaced with an underscore `_`. So to access `astro:sun:home`, use `astro_sun_home` as an alternative to `things`

Manages access to openHAB entities

You can access openHAB items and things directly using their name, anywhere ‘EntityLookup` is available.

Examples:

Accessing Items and Groups

gAll_Lights       # Access the gAll_Lights group. It is the same as items["gAll_Lights"]
Kitchen_Light.on  # The openHAB object for the Kitchen_Light item and send an ON command

Accessing Things

smtp_mail_local.send_mail('[email protected]', 'Subject', 'Dear Person, ...')
# Is equivalent to:
things['smtp:mail:local'].send_mail('[email protected]', 'Subject', 'Dear Person, ...')

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingItem, ...

Automatically looks up openHAB items and things in appropriate registries



126
127
128
129
130
131
132
133
# File 'lib/openhab/core/entity_lookup.rb', line 126

ruby2_keywords def method_missing(method, *args)
  return super unless args.empty? && !block_given?

  logger.trace { "method missing, performing openHAB Lookup for: #{method}" }
  EntityLookup.lookup_entity(method,
                             create_dummy_items: self.class.respond_to?(:create_dummy_items?) &&
                               self.class.create_dummy_items?) || super
end

Instance Method Details

#itemsCore::Items::Registry

Fetches all items from the item registry

The examples all assume the following items exist.

“‘xtend Dimmer DimmerTest “Test Dimmer” Switch SwitchTest “Test Switch” “`

Examples:

logger.info("Item Count: #{items.count}")  # Item Count: 2
logger.info("Items: #{items.map(&:label).sort.join(', ')}")  # Items: Test Dimmer, Test Switch'
logger.info("DimmerTest exists? #{items.key?('DimmerTest')}") # DimmerTest exists? true
logger.info("StringTest exists? #{items.key?('StringTest')}") # StringTest exists? false
rule 'Use dynamic item lookup to increase related dimmer brightness when switch is turned on' do
  changed SwitchTest, to: ON
  triggered { |item| items[item.name.gsub('Switch','Dimmer')].brighten(10) }
end
rule 'search for a suitable item' do
  on_load
  triggered do
    # Send ON to DimmerTest if it exists, otherwise send it to SwitchTest
    (items['DimmerTest'] || items['SwitchTest'])&.on
  end
end


99
100
101
# File 'lib/openhab/core/entity_lookup.rb', line 99

def items
  Core::Items::Registry.instance
end

#thingsCore::Things::Registry

Get all things known to openHAB

Examples:

things.each { |thing| logger.info("Thing: #{thing.uid}")}
logger.info("Thing: #{things['astro:sun:home'].uid}")
homie_things = things.select { |t| t.thing_type_uid == "mqtt:homie300" }
zwave_things = things.select { |t| t.binding_id == "zwave" }
homeseer_dimmers = zwave_things.select { |t| t.thing_type_uid.id == "homeseer_hswd200_00_000" }
things['zwave:device:512:node90'].uid.bridge_ids # => ["512"]
things['mqtt:topic:4'].uid.bridge_ids # => []


117
118
119
# File 'lib/openhab/core/entity_lookup.rb', line 117

def things
  Core::Things::Registry.instance
end