Module: Enumerable

Included in:
OpenHAB::Core::Items::Metadata::Hash, OpenHAB::Core::Items::Metadata::NamespaceHash, OpenHAB::Core::LazyArray, OpenHAB::Core::Provider
Defined in:
lib/openhab/core/items/semantics.rb,
lib/openhab/core/items/semantics/enumerable.rb

Overview

Additions to Enumerable to allow easily filtering and commanding groups of items.

Examples:

Turn on all members of a group

gOutsideLights.members.on

Turn on all lights that are tagged `NightLights`

items.tagged("NightLights").on

Close all blinds in the same room when the TV is turned on

rule "Close Blinds" do
  changed gTVPower.members, to: ON
  triggered do |item|
    item.location
        .equipments(Semantics::Blinds)
        .points(Semantics::OpenLevel)
        .down
  end
end

Ensure works on Enumerable

gLights.members.ensure.on
# or
gLights.members.ensure << ON

Send a command to a list of items

[Light1, Light2, Light3].on
# or
[Light1, Light2, Light3].command(ON) # can't use <<, because that's already defined on Array

See Also:

Filtering Methods collapse

Filtering Methods Methods to help filter the members of the Enumerable 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) ⇒ 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)
  self if count { |i| i.command(command) }.positive?
end

#decreaseself

Send the DECREASE command to every item in the collection

Returns:

  • (self)

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

#downself

Send the DOWN command to every item in the collection

Returns:

  • (self)

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

#equipments(type = nil) ⇒ 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 the given type)

Examples:

Get all TVs in a room

lGreatRoom.equipments(Semantics::Screen)

Returns:


431
432
433
434
435
436
437
438
439
440
# File 'lib/openhab/core/items/semantics.rb', line 431

def equipments(type = nil)
  if type && (!type.is_a?(Module) || !(type < Semantics::Equipment))
    raise ArgumentError, "type must be a subclass of Equipment"
  end

  result = select(&:equipment?)
  result.select! { |i| i.equipment_type <= type } if type

  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 159

#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 139

#locations(type = nil) ⇒ Array<Item>

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

Returns:


408
409
410
411
412
413
414
415
416
417
# File 'lib/openhab/core/items/semantics.rb', line 408

def locations(type = nil)
  if type && (!type.is_a?(Module) || !(type < Semantics::Location))
    raise ArgumentError, "type must be a subclass of Location"
  end

  result = select(&:location?)
  result.select! { |i| i.location_type <= type } if type

  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 135

#nextself

Send the NEXT command to every item in the collection

Returns:

  • (self)

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

#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 119

#onself

Send the ON command to every item in the collection

Returns:

  • (self)

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

#pauseself

Send the PAUSE command to every item in the collection

Returns:

  • (self)

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

#playself

Send the PLAY command to every item in the collection

Returns:

  • (self)

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

#points(*point_or_property_types) ⇒ 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)

Returns:

See Also:


451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/openhab/core/items/semantics.rb', line 451

def points(*point_or_property_types)
  unless (0..2).cover?(point_or_property_types.length)
    raise ArgumentError, "wrong number of arguments (given #{point_or_property_types.length}, expected 0..2)"
  end
  unless point_or_property_types.all? do |tag|
           tag.is_a?(Module) &&
           (tag < Semantics::Point || tag < Semantics::Property)
         end
    raise ArgumentError, "point_or_property_types must all be a subclass of Point or Property"
  end
  if point_or_property_types.count { |tag| tag < Semantics::Point } > 1 ||
     point_or_property_types.count { |tag| tag < Semantics::Property } > 1
    raise ArgumentError, "point_or_property_types cannot both be a subclass of Point or Property"
  end

  select do |point|
    point.point? && point_or_property_types.all? do |tag|
      (tag < Semantics::Point && point.point_type <= tag) ||
        (tag < Semantics::Property && point.property_type&.<=(tag))
    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 167

#refreshself

Send the REFRESH command to every item in the collection

Returns:

  • (self)

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

#rewindself

Send the REWIND command to every item in the collection

Returns:

  • (self)

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

#stopself

Send the STOP command to every item in the collection

Returns:

  • (self)

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

#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

#upself

Send the UP command to every item in the collection

Returns:

  • (self)

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

#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


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

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