Class: OpenHAB::Core::Items::SwitchItem

Inherits:
GenericItem show all
Defined in:
lib/openhab/core/items/switch_item.rb

Overview

A SwitchItem represents a normal switch that can be ON or OFF. Useful for normal lights, presence detection etc.

Examples:

Turn on all switches in a Group:Switch called Switches

Switches.on

Turn on all switches in a group called Switches that are off

Switches.select(&:off?).each(&:on)

Switches accept booelan commands (true/false)

# Turn on switch
SwitchItem << true

# Turn off switch
SwitchItem << false

# Turn off switch if any in another group is on
SwitchItem << Switches.any?(&:on?)

Invert all Switches

items.grep(SwitchItem)
     .each(&:toggle)

Direct Known Subclasses

DimmerItem

Constant Summary

Constants included from Semantics

OpenHAB::Core::Items::Semantics::Equipment, OpenHAB::Core::Items::Semantics::Location, OpenHAB::Core::Items::Semantics::Point, OpenHAB::Core::Items::Semantics::Property, OpenHAB::Core::Items::Semantics::Tag

Constants included from Persistence

Persistence::HistoricState

Instance Attribute Summary collapse

Attributes inherited from GenericItem

#category, #formatted_state, #label, #name, #raw_state, #tags

Attributes included from Semantics

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

Attributes included from Item

#accepted_command_types, #accepted_data_types, #all_groups, #channel, #channel_uid, #channel_uids, #channels, #groups, #links, #metadata, #name, #provider, #thing, #things

Instance Method Summary collapse

Methods inherited from GenericItem

#command, #modify, #null?, #refresh, #state?, #time_series=, #undef?, #update

Methods included from Semantics

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

Methods included from Item

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

Methods included from DSL::Items::TimedCommand

#command

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, #sum_between, #sum_since, #sum_until, #updated_between?, #updated_since?, #updated_until?, #variance_between, #variance_since, #variance_until

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

#ensure

Instance Attribute Details

#stateOnOffType? (readonly)

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/openhab/core/items/switch_item.rb', line 38

class SwitchItem < GenericItem
  # Convert boolean commands to ON/OFF
  # @!visibility private
  def format_type(command)
    return Types::OnOffType.from(command) if [true, false].include?(command)

    super
  end

  #
  # Send a command to invert the state of the switch
  #
  # @return [self]
  #
  def toggle
    return on! unless state?

    command!(!state)
  end

  # @!method on?
  #   Check if the item state == {ON}
  #   @return [true,false]

  # @!method off?
  #   Check if the item state == {OFF}
  #   @return [true,false]

  # @!method on
  #   Send the {ON} command to the item
  #   @return [SwitchItem] `self`

  # @!method on!
  #   Send the {ON} command to the item, even when {OpenHAB::DSL.ensure_states! ensure_states!} is in effect.
  #   @return [SwitchItem] `self`

  # @!method off
  #   Send the {OFF} command to the item
  #   @return [SwitchItem] `self`

  # @!method off!
  #   Send the {OFF} command to the item, even when {OpenHAB::DSL.ensure_states! ensure_states!} is in effect.
  #   @return [SwitchItem] `self`
end

Instance Method Details

#offSwitchItem

Send the OFF command to the item

Returns:



# File 'lib/openhab/core/items/switch_item.rb', line 74

#off!SwitchItem

Send the OFF command to the item, even when ensure_states! is in effect.

Returns:



# File 'lib/openhab/core/items/switch_item.rb', line 78

#off?true, false

Check if the item state == OFF

Returns:

  • (true, false)


# File 'lib/openhab/core/items/switch_item.rb', line 62

#onSwitchItem

Send the ON command to the item

Returns:



# File 'lib/openhab/core/items/switch_item.rb', line 66

#on!SwitchItem

Send the ON command to the item, even when ensure_states! is in effect.

Returns:



# File 'lib/openhab/core/items/switch_item.rb', line 70

#on?true, false

Check if the item state == ON

Returns:

  • (true, false)


# File 'lib/openhab/core/items/switch_item.rb', line 58

#toggleself

Send a command to invert the state of the switch

Returns:

  • (self)


52
53
54
55
56
# File 'lib/openhab/core/items/switch_item.rb', line 52

def toggle
  return on! unless state?

  command!(!state)
end