Module: Enumerable

Included in:
OpenHAB::Core::EmulateHash, OpenHAB::Core::LazyArray, OpenHAB::Core::Provider
Defined in:
lib/openhab/core/items/semantics/enumerable.rb,
lib/openhab/core/items/semantics.rb

Overview

Additions to Enumerable to allow easily filtering groups of items based on the semantic model

Filtering Methods collapse

Items State and Command Methods collapse

Instance Method Details

#all_groupsArray<GroupItem>

Returns all groups all elements are a part of, recursively

Returns:



91
92
93
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 91

def all_groups
  flat_map(&:all_groups).uniq
end

#all_membersArray<Item>

Returns all non-group members of all group elements, recursively

Returns:



79
80
81
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 79

def all_members
  grep(OpenHAB::Core::Items::GroupItem).flat_map(&:all_members).uniq
end

#command(command, **kwargs) ⇒ self?

Send a command to every item in the collection

Returns:

  • (self, nil)

    nil when ensure is in effect and all the items were already in the same state, otherwise self



100
101
102
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 100

def command(command, **kwargs)
  self if count { |i| i.command(command, **kwargs) }.positive?
end

#command!(command, **kwargs) ⇒ self

Send a command to every item in the collection, even when ensure_states! is in effect.

Returns:

  • (self)


106
107
108
109
110
111
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 106

def command!(command, **kwargs)
  # We cannot alias this to #command above, otherwise it will call
  # DSL::Items::Ensure::Item#command which checks for ensure_states
  each { |i| i.command!(command, **kwargs) }
  self
end

#decreaseself

Send the DECREASE command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 162

#downself

Send the DOWN command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 146

#equipments(*types, subclasses: true) ⇒ Array<Item>

Note:

As equipments are usually GroupItems, this method therefore returns an array of GroupItems. In order to get the points that belong to the equipments, use #members before calling #points. See the example with #points.

Returns a new array of items that are a semantics equipment (optionally of one of the given types)

Examples:

Get all TVs in a room

lGreatRoom.equipments(Semantics::Television)

Get all TVs and Speakers in a room

lGreatRoom.equipments(Semantics::Television, Semantics::Speaker)

Parameters:

  • types (SemanticTag)

    Pass 1 or more tags that are sub-classes of Semantics::Equipment. Note that when comparing against semantic tags, it does a sub-class check by default, so if you search for Fan, you'll get items tagged with CeilingFan, KitchenHood, etc.

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

    If true, match all subclasses of the given types. If false, only match the exact type.

Returns:



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/openhab/core/items/semantics.rb', line 631

def equipments(*types, subclasses: true)
  begin
    raise ArgumentError unless types.all? { |type| type < Semantics::Equipment }
  rescue ArgumentError, TypeError
    raise ArgumentError, "type must be a subclass of Equipment"
  end

  result = select(&:equipment?)
  unless types.empty?
    if subclasses
      result.select! { |i| types.any? { |type| i.equipment_type <= type } }
    else
      result.select! { |i| types.include?(i.equipment_type) }
    end
  end

  result
end

#fast_forwardself

Send the FASTFORWARD command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 178

#groupsArray<GroupItem>

Returns the groups of all elements

Returns:



85
86
87
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 85

def groups
  flat_map(&:groups).uniq
end

#increaseself

Send the INCREASE command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 158

#locations(*types, subclasses: true) ⇒ Array<Item>

Returns a new array of items that are a semantics Location (optionally of one of the given types)

Examples:

Get all rooms

items.locations(Semantics::Room)

Get all bedrooms and bathrooms

items.locations(Semantics::Bedroom, Semantics::Bathroom)

Parameters:

  • types (SemanticTag)

    Pass 1 or more tags that are sub-tags of Semantics::Location. Note that when comparing against semantic tags, it does a sub-class check by default. So if you search for Room, you'll get items tagged with LivingRoom, Kitchen, etc.

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

    If true, match all subclasses of the given types. If false, only match the exact type.

Returns:



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/openhab/core/items/semantics.rb', line 588

def locations(*types, subclasses: true)
  begin
    raise ArgumentError unless types.all? { |type| type < Semantics::Location }
  rescue ArgumentError, TypeError
    raise ArgumentError, "type must be a subclass of Location"
  end

  result = select(&:location?)
  unless types.empty?
    if subclasses
      result.select! { |i| types.any? { |type| i.location_type <= type } }
    else
      result.select! { |i| types.include?(i.location_type) }
    end
  end

  result
end

#member_of(*groups) ⇒ Array<Item>

Returns a new array of items that are a member of at least one of the given groups

Parameters:

Returns:



60
61
62
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 60

def member_of(*groups)
  select { |i| i.member_of?(*groups) }
end

#membersArray<Item>

Returns the group members of all group elements

Returns:



73
74
75
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 73

def members
  grep(OpenHAB::Core::Items::GroupItem).flat_map(&:members).uniq
end

#moveself

Send the MOVE command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 154

#nextself

Send the NEXT command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 182

#not_member_of(*groups) ⇒ Array<Item>

Returns a new array of items that are not a member of any of the given groups

Parameters:

Returns:



67
68
69
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 67

def not_member_of(*groups)
  reject { |i| i.member_of?(*groups) }
end

#not_tagged(*tags) ⇒ Array<Item>

Returns a new array of items that do not have any of the given tags

Parameters:

Returns:



53
54
55
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 53

def not_tagged(*tags)
  reject { |i| i.tagged?(*tags) }
end

#offself

Send the OFF command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 138

#onself

Send the ON command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 134

#pauseself

Send the PAUSE command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 170

#playself

Send the PLAY command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 166

#points(*point_or_property_types, subclasses: true) ⇒ Array<Item>

Returns a new array of items that are semantics points (optionally of a given type)

Examples:

Get all the power switch items for every equipment in a room

lGreatRoom.equipments.members.points(Semantics::Switch)

Get (all) the temperature setpoints for a thermostat

eMainFloorThermostat.points(Semantics::Setpoint, Semantics::Temperature)

Get all the brightness and color (light) points in a room

lKitchen.equipments.members.points(Semantics::Brightness, Semantics::Color)

Get all the brightness and color (light) control points in a room

lKitchen.equipments.members.points(Semantics::Control, Semantics::Brightness, Semantics::Color)

Get the air temperature and speed control points in a room

lGreatRoom.equipments.members.points([Semantics::Measurement, Semantics::Temperature],
                                     [Semantics::Control, Semantics::Speed])
# => [GreatRoom_AirTemp, GreatRoom_FanSpeed]

Parameters:

  • point_or_property_types (SemanticTag, Array<SemanticTag>)

    Pass 1 or more tags that are sub-tags of Semantics::Point or Semantics::Property. If multiple point or property tags are given, the point only has to match one of them. But if point and property tag(s) are given, the point must match both a point and property tag. You may also pass an array of tags, to match multiple combination of tags at once. See the air temperature/speed control example below. Note that when comparing against semantic tags, it does a sub-class check by default, so if you search for Control, you'll get items tagged with Switch.

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

    If true, match all subclasses of the given types. If false, only match the exact type.

Returns:

See Also:



685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
# File 'lib/openhab/core/items/semantics.rb', line 685

def points(*point_or_property_types, subclasses: true)
  return select(&:point?) if point_or_property_types.empty?

  begin
    arrays, non_arrays = point_or_property_types.partition { |tag| tag.is_a?(Array) }
    arrays << non_arrays unless non_arrays.empty?

    arrays.map! do |array|
      points = array.select { |tag| tag < Semantics::Point }
      properties = array.select { |tag| tag < Semantics::Property }
      raise ArgumentError unless points.length + properties.length == array.length

      [points, properties]
    end
  rescue ArgumentError, TypeError
    raise ArgumentError, "point_or_property_types must all be arrays, or a subclass of Point or Property"
  end

  select do |point|
    point.point? && arrays.any? do |(points, properties)|
      if subclasses
        next false if !points.empty? &&
                      ((point_type = point.point_type).nil? ||
                       points.none? { |tag| point_type <= tag })
        next false if !properties.empty? &&
                      ((property_type = point.property_type).nil? ||
                       properties.none? { |tag| property_type <= tag })
      else
        next false if !points.empty? &&
                      ((point_type = point.point_type).nil? ||
                       points.none?(point_type))
        next false if !properties.empty? &&
                      ((property_type = point.property_type).nil? ||
                       properties.none?(property_type))
      end
      true
    end
  end
end

#previousself

Send the PREVIOUS command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 186

#refreshself

Send the REFRESH command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 130

#rewindself

Send the REWIND command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 174

#stopself

Send the STOP command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 150

#tagged(*tags) ⇒ Array<Item>

Returns a new array of items that have at least one of the given tags

Parameters:

Returns:



46
47
48
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 46

def tagged(*tags)
  select { |i| i.tagged?(*tags) }
end

#toggle(source: nil) ⇒ self

Send a toggle command to every item in the collection

Returns:

  • (self)


201
202
203
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 201

def toggle(source: nil)
  each { |i| i.toggle(source:) }
end

#upself

Send the UP command to every item in the collection

Returns:

  • (self)


# File 'lib/openhab/core/items/semantics/enumerable.rb', line 142

#update(state) ⇒ self?

Update the state of every item in the collection

Returns:

  • (self, nil)

    nil when ensure is in effect and all the items were already in the same state, otherwise self



116
117
118
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 116

def update(state)
  self if count { |i| i.update(state) }.positive?
end

#update!(state) ⇒ self

Update the state of every item in the collection, even when ensure_states! is in effect.

Returns:

  • (self)


123
124
125
126
127
128
# File 'lib/openhab/core/items/semantics/enumerable.rb', line 123

def update!(state)
  # We cannot alias this to #update above, otherwise it will call
  # DSL::Items::Ensure::Item#update which checks for ensure_states
  each { |i| i.update!(state) }
  self
end