Module: OpenHAB::Core::Items::Item
- Included in:
- GenericItem
- Defined in:
- lib/openhab/core/items/item.rb
Overview
The core features of an openHAB item.
Constant Summary
Constants included from Semantics
Semantics::Equipment, Semantics::Location, Semantics::Point, Semantics::Property, Semantics::Tag
Constants included from Persistence
Instance Attribute Summary collapse
-
#accepted_command_types ⇒ Array<Class>
readonly
An array of Commands that can be sent as commands to this item.
-
#accepted_data_types ⇒ Array<Class>
readonly
An array of States that can be sent as commands to this item.
-
#all_groups ⇒ Array<GroupItem>
readonly
Returns all groups that this item is a part of, as well as those groups' groups, recursively.
-
#channel ⇒ Things::Channel?
readonly
Return the channel this item is linked to.
-
#channel_uid ⇒ Things::ChannelUID?
readonly
Return the UID of the channel this item is linked to.
-
#channel_uids ⇒ Array<Things::ChannelUID>
readonly
Return the UIDs of all of the channels this item is linked to.
-
#channels ⇒ Array<Things::Channel>
readonly
Return all of the channels this item is linked to.
- #command_description ⇒ Types::CommandDescription? readonly
-
#display_state ⇒ String
(also: #transformed_state, #formatted_state)
readonly
Format the item's state according to its state description.
-
#groups ⇒ Array<GroupItem>
readonly
Returns all groups that this item is part of.
-
#last_state ⇒ State
readonly
The previous state of the item.
-
#last_state_change ⇒ ZonedDateTime
readonly
The time of the last state change.
-
#last_state_update ⇒ ZonedDateTime
readonly
The time of the last state update.
-
#links ⇒ ItemChannelLinks
readonly
Returns all of the item's links (channels and link configurations).
-
#metadata ⇒ Metadata::NamespaceHash
readonly
Access to the item's metadata.
-
#name ⇒ String
readonly
The item's name.
-
#provider ⇒ org.openhab.core.common.registry.Provider?
readonly
Returns the provider for this item.
- #state_description ⇒ Types::StateDescription? readonly
-
#thing ⇒ Things::Thing?
(also: #linked_thing)
readonly
Return the item's thing if this item is linked with a thing.
-
#things ⇒ Array<Things::Thing>
(also: #all_linked_things)
readonly
Returns all of the item's linked things.
Attributes included from Semantics
#equipment, #equipment_type, #location, #location_type, #point_type, #property_type, #semantic_type
Instance Method Summary collapse
-
#call_item? ⇒ true, false
Check if the item is a call item.
-
#color_item? ⇒ true, false
Check if the item is a color item.
-
#command(command, source: nil) ⇒ self?
(also: #command!, #<<)
Send a command to this item.
-
#contact_item? ⇒ true, false
Check if the item is a contact item.
-
#date_time_item? ⇒ true, false
Check if the item is a date_time item.
-
#dimmer_item? ⇒ true, false
Check if the item is a dimmer item.
-
#group_item? ⇒ true, false
Check if the item is a group item.
-
#image_item? ⇒ true, false
Check if the item is a image item.
- #inspect ⇒ String
- #link(channel = nil, config = nil) ⇒ Things::ItemChannelLink?
-
#location_item? ⇒ true, false
Check if the item is a location item.
-
#member_of?(*groups) ⇒ true, false
Checks if this item is a member of at least one of the given groups.
-
#number_item? ⇒ true, false
Check if the item is a number item.
-
#player_item? ⇒ true, false
Check if the item is a player item.
-
#refresh ⇒ Item
Send the REFRESH command to the item.
-
#rollershutter_item? ⇒ true, false
Check if the item is a rollershutter item.
-
#string_item? ⇒ true, false
Check if the item is a string item.
-
#switch_item? ⇒ true, false
Check if the item is a switch item.
-
#tagged?(*tags) ⇒ true, false
Checks if this item has at least one of the given tags.
- #to_s ⇒ String
-
#unlink(channel) ⇒ Things::ItemChannelLink?
Removes a link to a channel from managed link providers.
-
#update(state) ⇒ self?
(also: #update!)
Send an update to this item.
Methods included from Semantics
add, #equipment?, #location?, lookup, #point?, #points, remove, #semantic?, tags
Methods included from DSL::Items::Ensure::Ensurable
Methods included from Persistence
#all_states_between, #all_states_since, #all_states_until, #average_between, #average_since, #average_until, #changed_between?, #changed_since?, #changed_until?, #count_between, #count_since, #count_state_changes_between, #count_state_changes_since, #count_state_changes_until, #count_until, #delta_between, #delta_since, #delta_until, #deviation_between, #deviation_since, #deviation_until, #evolution_rate, #evolution_rate_between, #evolution_rate_since, #evolution_rate_until, #historic_state, #last_change, #last_update, #maximum_between, #maximum_since, #maximum_until, #median_between, #median_since, #median_until, #minimum_between, #minimum_since, #minimum_until, #next_change, #next_state, #next_update, #persist, #persisted_state, #previous_state, #remove_all_states_between, #remove_all_states_since, #remove_all_states_until, #riemann_sum_between, #riemann_sum_since, #riemann_sum_until, #sum_between, #sum_since, #sum_until, #updated_between?, #updated_since?, #updated_until?, #variance_between, #variance_since, #variance_until
Instance Attribute Details
#accepted_command_types ⇒ Array<Class> (readonly)
Returns An array of Commands that can be sent as commands to this item.
|
# File 'lib/openhab/core/items/item.rb', line 61
|
#accepted_data_types ⇒ Array<Class> (readonly)
Returns An array of States that can be sent as commands to this item.
|
# File 'lib/openhab/core/items/item.rb', line 64
|
#all_groups ⇒ Array<GroupItem> (readonly)
Returns all groups that this item is a part of, as well as those groups' groups, recursively
271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/openhab/core/items/item.rb', line 271 def all_groups result = [] new_groups = Set.new(groups) until new_groups.empty? result.concat(new_groups.to_a) new_groups.replace(new_groups.flat_map(&:groups)) # remove any groups we already have in the result to avoid loops new_groups.subtract(result) end result end |
#channel ⇒ Things::Channel? (readonly)
Return the channel this item is linked to. If an item is linked to more than one channel, this method only returns the first channel.
471 472 473 |
# File 'lib/openhab/core/items/item.rb', line 471 def channel channel_uids.first&.channel end |
#channel_uid ⇒ Things::ChannelUID? (readonly)
Return the UID of the channel this item is linked to. If an item is linked to more than one channel, this method only returns the first channel.
454 455 456 |
# File 'lib/openhab/core/items/item.rb', line 454 def channel_uid channel_uids.first end |
#channel_uids ⇒ Array<Things::ChannelUID> (readonly)
Return the UIDs of all of the channels this item is linked to.
462 463 464 |
# File 'lib/openhab/core/items/item.rb', line 462 def channel_uids Things::Links::Provider.registry.get_bound_channels(name) end |
#channels ⇒ Array<Things::Channel> (readonly)
Return all of the channels this item is linked to.
479 480 481 |
# File 'lib/openhab/core/items/item.rb', line 479 def channels channel_uids.map(&:channel) end |
#command_description ⇒ Types::CommandDescription? (readonly)
|
# File 'lib/openhab/core/items/item.rb', line 70
|
#display_state ⇒ String (readonly) Also known as: transformed_state, formatted_state
While this method is also aliased to #transformed_state
to
match the field available in the openHAB REST API, unlike the API it
will always return a value even if the state description has no
transformation.
Format the item's state according to its state description
This may include running a transformation, if one is defined in the state description's pattern, otherwise it will format according to the pattern. If no state description is defined, the state will simply be returned as a string.
113 114 115 |
# File 'lib/openhab/core/items/item.rb', line 113 def display_state Item.item_states_event_builder.get_display_state(self) end |
#groups ⇒ Array<GroupItem> (readonly)
Returns all groups that this item is part of
244 245 246 |
# File 'lib/openhab/core/items/item.rb', line 244 def groups group_names.filter_map { |name| EntityLookup.lookup_item(name) } end |
#last_state ⇒ State (readonly)
Returns The previous state of the item.
|
# File 'lib/openhab/core/items/item.rb', line 73
|
#last_state_change ⇒ ZonedDateTime (readonly)
Returns The time of the last state change.
|
# File 'lib/openhab/core/items/item.rb', line 77
|
#last_state_update ⇒ ZonedDateTime (readonly)
Returns The time of the last state update.
|
# File 'lib/openhab/core/items/item.rb', line 81
|
#links ⇒ ItemChannelLinks (readonly)
Returns all of the item's links (channels and link configurations).
498 499 500 |
# File 'lib/openhab/core/items/item.rb', line 498 def links ItemChannelLinks.new(name, Things::Links::Provider.registry.get_links(name)) end |
#metadata ⇒ Metadata::NamespaceHash (readonly)
Access to the item's metadata.
Both the return value of this method as well as the individual namespaces can be treated as Hashes.
Examples assume the following items:
Switch Item1 { namespace1="value" [ config1="foo", config2="bar" ] }
String StringItem1
402 403 404 |
# File 'lib/openhab/core/items/item.rb', line 402 def metadata Metadata::NamespaceHash.new(name) end |
#name ⇒ String (readonly)
The item's name.
|
# File 'lib/openhab/core/items/item.rb', line 57
|
#provider ⇒ org.openhab.core.common.registry.Provider? (readonly)
Returns the provider for this item.
591 592 593 |
# File 'lib/openhab/core/items/item.rb', line 591 def provider Provider.registry.provider_for(self) end |
#state_description ⇒ Types::StateDescription? (readonly)
|
# File 'lib/openhab/core/items/item.rb', line 67
|
#thing ⇒ Things::Thing? (readonly) Also known as: linked_thing
Return the item's thing if this item is linked with a thing. If an item is linked to more than one channel, this method only returns the first thing.
435 436 437 |
# File 'lib/openhab/core/items/item.rb', line 435 def thing all_linked_things.first end |
#things ⇒ Array<Things::Thing> (readonly) Also known as: all_linked_things
Returns all of the item's linked things.
444 445 446 |
# File 'lib/openhab/core/items/item.rb', line 444 def things Things::Links::Provider.registry.get_bound_things(name).map { |thing| Things::Proxy.new(thing) } end |
Instance Method Details
#call_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a call item.
Check if the item is a call item.
612 |
# File 'lib/openhab/core/items/item.rb', line 612 def_type_predicate(:call) |
#color_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a color item.
Check if the item is a color item.
613 |
# File 'lib/openhab/core/items/item.rb', line 613 def_type_predicate(:color) |
#command(command, source: nil) ⇒ self? Also known as: command!, <<
Send a command to this item
When this method is chained after the ensure method, or issued inside an ensure_states block, or after ensure_states! have been called, the command will only be sent if the item is not already in the same state.
The similar method command!
, however, will always send the command regardless of the item's state.
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/openhab/core/items/item.rb', line 152 def command(command, source: nil) command = format_command(command) logger.trace { "Sending Command #{command} to #{name}" } if source Events.publisher.post(Events::ItemEventFactory.create_command_event(name, command, source.to_s)) else $events.send_command(self, command) end Proxy.new(self) end |
#contact_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a contact item.
Check if the item is a contact item.
614 |
# File 'lib/openhab/core/items/item.rb', line 614 def_type_predicate(:contact) |
#date_time_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a date_time item.
Check if the item is a date_time item.
615 |
# File 'lib/openhab/core/items/item.rb', line 615 def_type_predicate(:date_time) |
#dimmer_item? ⇒ true, false
Color items are also considered dimmer items.
If the item is a group item, it will also return true if the base item is a dimmer item.
Check if the item is a dimmer item.
617 |
# File 'lib/openhab/core/items/item.rb', line 617 def_type_predicate(:dimmer) |
#group_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a group item.
Check if the item is a group item.
618 |
# File 'lib/openhab/core/items/item.rb', line 618 def_type_predicate(:group) |
#image_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a image item.
Check if the item is a image item.
619 |
# File 'lib/openhab/core/items/item.rb', line 619 def_type_predicate(:image) |
#inspect ⇒ String
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/openhab/core/items/item.rb', line 573 def inspect s = "#<OpenHAB::Core::Items::#{type}Item#{type_details} #{name} #{label.inspect} state=#{raw_state.inspect}" # @deprecated OH 4.3 Remove if guard when dropping support for OH 4.3 if respond_to?(:last_state) s += " last_state=#{last_state.inspect}" if last_state s += " last_state_update=#{last_state_update}" if last_state_update s += " last_state_change=#{last_state_change}" if last_state_change end s += " category=#{category.inspect}" if category s += " tags=#{tags.to_a.inspect}" unless tags.empty? s += " groups=#{group_names}" unless group_names.empty? meta = metadata.to_h s += " metadata=#{meta.inspect}" unless meta.empty? "#{s}>" end |
#link ⇒ Things::ItemChannelLink? #link(channel, config = {}) ⇒ Things::ItemChannelLink
536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/openhab/core/items/item.rb', line 536 def link(channel = nil, config = nil) return Things::Links::Provider.registry.get_links(name).first if channel.nil? && config.nil? config ||= {} Core::Things::Links::Provider.create_link(self, channel, config).tap do |new_link| provider = Core::Things::Links::Provider.current if !(current_link = provider.get(new_link.uid)) provider.add(new_link) elsif current_link.configuration != config provider.update(new_link) end end end |
#location_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a location item.
Check if the item is a location item.
620 |
# File 'lib/openhab/core/items/item.rb', line 620 def_type_predicate(:location) |
#member_of?(*groups) ⇒ true, false
Checks if this item is a member of at least one of the given groups.
257 258 259 260 261 262 |
# File 'lib/openhab/core/items/item.rb', line 257 def member_of?(*groups) groups.map! do |group| group.is_a?(GroupItem) ? group.name : group end !!group_names.intersect?(groups) end |
#number_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a number item.
Check if the item is a number item.
621 |
# File 'lib/openhab/core/items/item.rb', line 621 def_type_predicate(:number) |
#player_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a player item.
Check if the item is a player item.
622 |
# File 'lib/openhab/core/items/item.rb', line 622 def_type_predicate(:player) |
#refresh ⇒ Item
Send the REFRESH command to the item
|
# File 'lib/openhab/core/items/item.rb', line 171
|
#rollershutter_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a rollershutter item.
Check if the item is a rollershutter item.
623 |
# File 'lib/openhab/core/items/item.rb', line 623 def_type_predicate(:rollershutter) |
#string_item? ⇒ true, false
If the item is a group item, it will also return true if the base item is a string item.
Check if the item is a string item.
624 |
# File 'lib/openhab/core/items/item.rb', line 624 def_type_predicate(:string) |
#switch_item? ⇒ true, false
Color and dimmer items are also considered switch items.
If the item is a group item, it will also return true if the base item is a switch item.
Check if the item is a switch item.
626 |
# File 'lib/openhab/core/items/item.rb', line 626 def_type_predicate(:switch) |
#tagged?(*tags) ⇒ true, false
Checks if this item has at least one of the given tags.
419 420 421 422 423 424 425 426 427 428 |
# File 'lib/openhab/core/items/item.rb', line 419 def tagged?(*tags) tags.map! do |tag| if tag.is_a?(Semantics::SemanticTag) tag.name else tag end end !!self.tags.to_a.intersect?(tags) end |
#to_s ⇒ String
90 91 92 |
# File 'lib/openhab/core/items/item.rb', line 90 def to_s label || name end |
#unlink(channel) ⇒ Things::ItemChannelLink?
Removes a link to a channel from managed link providers.
561 562 563 564 565 566 567 568 569 570 |
# File 'lib/openhab/core/items/item.rb', line 561 def unlink(channel) link_to_delete = Things::Links::Provider.create_link(self, channel, {}) provider = Things::Links::Provider.registry.provider_for(link_to_delete.uid) unless provider.is_a?(ManagedProvider) raise FrozenError, "Cannot remove the link #{link_to_delete.uid} from non-managed provider #{provider.inspect}" end provider.remove(link_to_delete.uid) end |
#update(state) ⇒ self? Also known as: update!
Send an update to this item
199 200 201 202 203 204 |
# File 'lib/openhab/core/items/item.rb', line 199 def update(state) state = format_update(state) logger.trace { "Sending Update #{state} to #{name}" } $events.post_update(self, state) Proxy.new(self) end |