Module: OpenHAB::Core::Items::Semantics
- Included in:
- GenericItem
- Defined in:
- lib/openhab/core/items/semantics.rb
Overview
Module for implementing semantics helper methods on Item in order to easily navigate the Semantic Model in your scripts. This can be extremely useful to find related items in rules that are executed for any member of a group.
Wraps org.openhab.core.model.script.actions.Semantics as well as adding a few additional convenience methods. Also includes classes for each semantic tag.
Be warned that the Semantic model is stricter than can actually be described by tags and groups on an Item. It makes assumptions that any given item only belongs to one semantic type (Location, Equipment, Point).
#Enumerable helper methods
Enumerable helper methods are also provided to complement the semantic model. These methods can be chained together to find specific item(s) based on custom tags or group memberships that are outside the semantic model.
The Enumerable helper methods apply to:
- GroupItem#members and GroupItem#all_members. This includes semantic #location and #equipment because they are also group items. An exception is for Equipments that are an item (not a group)
- Array of items, such as the return value of Enumerable#equipments, Enumerable#locations, Enumerable#points, Enumerable#tagged, Enumerable#not_tagged, Enumerable#member_of, Enumerable#not_member_of, Enumerable#members methods, etc.
- items[] hash which contains all items in the system.
#Semantic Classes
Each Semantic Tag has a corresponding class within the org.openhab.core.semantics class hierarchy. These "semantic classes" are available as constants in the Semantics module with the corresponding name. The following table illustrates the semantic constants:
Semantic Constant | openHAB's Semantic Class |
---|---|
Semantics::LivingRoom |
org.openhab.core.semantics.model.location.LivingRoom |
Semantics::Lightbulb |
org.openhab.core.semantics.model.equipment.Lightbulb |
Semantics::Control |
org.openhab.core.semantics.model.point.Control |
Semantics::Switch |
org.openhab.core.semantics.model.point.Switch |
Semantics::Power |
org.openhab.core.semantics.model.property.Power |
... | ... |
These constants can be used as arguments to the #points, Enumerable#locations and Enumerable#equipments methods to filter their results. They can also be compared against the return value of #semantic_type, #location_type, #equipment_type, #point_type, and #property_type. They can even be used with DSL::Items::ItemBuilder#tag.
Constant Summary collapse
- Tag =
This is a marker interface for all semantic tag classes.
- org.openhab.core.semantics.Tag
- Location =
This is the super interface for all types that represent a Location.
- org.openhab.core.semantics.Location
- Equipment =
This is the super interface for all types that represent an Equipment.
- org.openhab.core.semantics.Equipment
- Point =
This is the super interface for all types that represent a Point.
- org.openhab.core.semantics.Point
- Property =
This is the super interface for all property tags.
- org.openhab.core.semantics.Property
Instance Attribute Summary collapse
-
#equipment ⇒ Item?
readonly
Gets the related Equipment Item of this Item.
-
#equipment_type ⇒ Class?
readonly
Returns the sub-class of Equipment related to this Item.
-
#location ⇒ Item?
readonly
Gets the related Location Item of this Item.
-
#location_type ⇒ Class?
readonly
Returns the sub-class of Location related to this Item.
-
#point_type ⇒ Class?
readonly
Returns the sub-class of Point this Item is tagged with.
-
#property_type ⇒ Class?
readonly
Returns the sub-class of Property this Item is tagged with.
-
#semantic_type ⇒ Class?
readonly
Returns the sub-class of Tag this Item is tagged with.
Instance Method Summary collapse
-
#equipment? ⇒ true, false
Checks if this Item is an Equipment.
-
#location? ⇒ true, false
Checks if this Item is a Location.
-
#point? ⇒ true, false
Checks if this Item is a Point.
-
#points(*point_or_property_types) ⇒ Array<Item>
Return the related Point Items.
-
#semantic? ⇒ true, false
Checks if this Item has any semantic tags.
Instance Attribute Details
#equipment ⇒ Item? (readonly)
305 306 307 |
# File 'lib/openhab/core/items/semantics.rb', line 305 def equipment Actions::Semantics.get_equipment(self)&.then(&Proxy.method(:new)) end |
#equipment_type ⇒ Class? (readonly)
Returns the sub-class of Equipment related to this Item.
In other words, the #semantic_type of this Item's Equipment.
318 319 320 |
# File 'lib/openhab/core/items/semantics.rb', line 318 def equipment_type Actions::Semantics.get_equipment_type(self)&.ruby_class end |
#location ⇒ Item? (readonly)
279 280 281 |
# File 'lib/openhab/core/items/semantics.rb', line 279 def location Actions::Semantics.get_location(self)&.then(&Proxy.method(:new)) end |
#location_type ⇒ Class? (readonly)
Returns the sub-class of Location related to this Item.
In other words, the #semantic_type of this Item's Location.
292 293 294 |
# File 'lib/openhab/core/items/semantics.rb', line 292 def location_type Actions::Semantics.get_location_type(self)&.ruby_class end |
#point_type ⇒ Class? (readonly)
Returns the sub-class of Point this Item is tagged with.
329 330 331 |
# File 'lib/openhab/core/items/semantics.rb', line 329 def point_type Actions::Semantics.get_point_type(self)&.ruby_class end |
#property_type ⇒ Class? (readonly)
Returns the sub-class of Property this Item is tagged with.
340 341 342 |
# File 'lib/openhab/core/items/semantics.rb', line 340 def property_type Actions::Semantics.get_property_type(self)&.ruby_class end |
#semantic_type ⇒ Class? (readonly)
354 355 356 |
# File 'lib/openhab/core/items/semantics.rb', line 354 def semantic_type Actions::Semantics.get_semantic_type(self)&.ruby_class end |
Instance Method Details
#equipment? ⇒ true, false
Checks if this Item is an Equipment
This is implemented as checking if the item's #semantic_type is an Equipment. I.e. an Item has a single #semantic_type.
245 246 247 |
# File 'lib/openhab/core/items/semantics.rb', line 245 def equipment? Actions::Semantics.equipment?(self) end |
#location? ⇒ true, false
Checks if this Item is a Location
This is implemented as checking if the item's #semantic_type is a Location. I.e. an Item has a single #semantic_type.
233 234 235 |
# File 'lib/openhab/core/items/semantics.rb', line 233 def location? Actions::Semantics.location?(self) end |
#point? ⇒ true, false
Checks if this Item is a Point
This is implemented as checking if the item's #semantic_type is a Point. I.e. an Item has a single #semantic_type.
256 257 258 |
# File 'lib/openhab/core/items/semantics.rb', line 256 def point? Actions::Semantics.point?(self) end |
#points(*point_or_property_types) ⇒ Array<Item>
Return the related Point Items.
Searches this Equipment Item for Points that are tagged appropriately.
If called on a Point Item, it will automatically search for sibling Points (and remove itself if found).
385 386 387 388 389 390 391 392 |
# File 'lib/openhab/core/items/semantics.rb', line 385 def points(*point_or_property_types) return members.points(*point_or_property_types) if equipment? || location? # automatically search the parent equipment (or location?!) for sibling points result = (equipment || location)&.points(*point_or_property_types) || [] result.delete(self) result end |
#semantic? ⇒ true, false
Checks if this Item has any semantic tags
265 266 267 |
# File 'lib/openhab/core/items/semantics.rb', line 265 def semantic? !!semantic_type end |