Class: OpenHAB::Core::Items::GenericItem

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

Overview

The abstract base class for all items.

Constant Summary

Constants included from Semantics

Semantics::Equipment, Semantics::Location, Semantics::Point, Semantics::Property, Semantics::Tag

Constants included from Persistence

Persistence::HistoricState

Instance Attribute Summary collapse

Attributes included from Item

#accepted_command_types, #accepted_data_types, #all_groups, #channel, #channel_uid, #channel_uids, #channels, #command_description, #formatted_state, #groups, #links, #metadata, #provider, #state_description, #thing, #things

Attributes included from Semantics

#equipment, #equipment_type, #location, #location_type, #point_type, #property_type, #semantic_type

Instance Method Summary collapse

Methods included from Item

#call_item?, #color_item?, #command, #contact_item?, #date_time_item?, #dimmer_item?, #group_item?, #image_item?, #inspect, #link, #location_item?, #member_of?, #number_item?, #player_item?, #refresh, #rollershutter_item?, #string_item?, #switch_item?, #tagged?, #to_s, #unlink, #update

Methods included from Semantics

add, #equipment?, #location?, lookup, #point?, #points, remove, #semantic?, tags

Methods included from DSL::Items::Ensure::Ensurable

#ensure

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

Methods included from DSL::Items::TimedCommand

#command

Instance Attribute Details

#categoryString Also known as: icon

The item's category (icon).

Returns:

  • (String)


173
174
175
176
177
178
179
180
181
# File 'lib/openhab/core/items/generic_item.rb', line 173

def category=(value)
  modify do
    value = value&.to_s
    next if category == value

    @modified = true
    set_category(value)
  end
end

#labelString

The item's descriptive label.

Returns:

  • (String)


# File 'lib/openhab/core/items/generic_item.rb', line 48

#nameString (readonly)

The item's name.

Returns:

  • (String)


# File 'lib/openhab/core/items/generic_item.rb', line 44

#raw_stateState (readonly)

Get the raw item state.

The state of the item, including possibly NULL or UNDEF

Returns:



63
# File 'lib/openhab/core/items/generic_item.rb', line 63

alias_method :raw_state, :state

#stateState? (readonly)

Returns openHAB item state if state is not UNDEF or NULL, nil otherwise. This makes it easy to use with the Ruby safe navigation operator &. Use #undef? or #null? to check for those states.

Returns:



82
83
84
# File 'lib/openhab/core/items/generic_item.rb', line 82

def state
  raw_state if state?
end

#tagsArray<String> #tags=(values) ⇒ void

The item's tags

Overloads:

  • #tagsArray<String>

    Returns the item's tags.

    Returns:

  • #tags=(values) ⇒ void

    This method returns an undefined value.

    Sets the item's tags.

    To remove all tags, assign an empty array or nil.

    Parameters:

Returns:



197
198
199
200
201
202
203
204
205
206
# File 'lib/openhab/core/items/generic_item.rb', line 197

def tags=(values)
  modify do
    values = DSL::Items::ItemBuilder.normalize_tags(*values)
    next if values.to_set == tags.to_set

    @modified = true
    remove_all_tags
    add_tags(values)
  end
end

Instance Method Details

#modify(force: false) {|self| ... } ⇒ Object

Defers notifying openHAB of modifications to multiple attributes until the block is complete.

Examples:

Modify label and tags for an item

MySwitch.modify do
  MySwitch.label = "New Label"
  MySwitch.tags = :labeled
end

Using the block argument to access the item

MySwitch.modify do |item|
  item.label = "New Label"
  item.icon = :switch
  item.tags = Semantics::Switch, Semantics::Light
end

Parameters:

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

    When true, allow modifications to file-based items. Normally a FrozenError is raised when attempting to modify file-based items, since they will then be out-of-sync with the definition on disk. Advanced users may do this knowingly and intentionally though, so an escape hatch is provided to allow runtime modifications.

Yields:

Yield Parameters:

  • self (Item)

    The item

Returns:

  • (Object)

    the block's return value

Raises:

  • (ArgumentError)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/openhab/core/items/generic_item.rb', line 131

def modify(force: false)
  raise ArgumentError, "you must pass a block to modify" unless block_given?

  proxied_self = Proxy.new(self)

  return yield(proxied_self) if instance_variable_defined?(:@modifying) && @modifying

  begin
    provider = self.provider
    if provider && !provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
      raise FrozenError, "Cannot modify item #{name} from provider #{provider.inspect}." unless force

      provider = nil
      logger.debug { "Forcing modifications to non-managed item #{name}" }
    end
    @modified = false
    @modifying = true

    r = yield(proxied_self)

    provider&.update(self) if @modified
    r
  ensure
    @modifying = false
  end
end

#null?true, false

Check if the item state == NULL

Returns:

  • (true, false)


# File 'lib/openhab/core/items/generic_item.rb', line 86

#state?true, false

Check if the item has a state (not UNDEF or NULL)

Returns:

  • (true, false)


70
71
72
# File 'lib/openhab/core/items/generic_item.rb', line 70

def state?
  !raw_state.is_a?(Types::UnDefType)
end

#time_series=(time_series) ⇒ void

This method returns an undefined value.

Set a new time series.

This will trigger a time_series_updated event.

Parameters:

Since:

  • openHAB 4.1



# File 'lib/openhab/core/items/generic_item.rb', line 94

#undef?true, false

Check if the item state == UNDEF

Returns:

  • (true, false)


# File 'lib/openhab/core/items/generic_item.rb', line 90