Control devices on iOS

This guide continues from Access devices and device metadata on iOS and introduces additional examples of device control and access.

To use specific device types or traits, such as the Matter OnOffTrait used in many of the examples here, import them:

import GoogleHomeSDK
import GoogleHomeTypes

Check if a trait supports a command

Use the trait-level supports function to check if a command is supported for a particular device.

For example, to check for a device's support of the On/Off trait's toggle command:

// Check if the OnOff trait supports the toggle command.
if onOffTraitTest.supportsToggleCommand {
  print("onOffTrait supports toggle command")
} else {
  print("onOffTrait does not support stateful toggle command")
}

Send a command to a device

Sending a command is similar to reading a state attribute from a trait. To turn the device on or off, use the OnOffTrait Toggle command, which is defined in the Google Home ecosystem data model as toggle(). This method changes onOff to false if it is true, or to true if it is false:

// Calling a command on a trait.
do {
  try await onOffTraitTest.toggle()
} catch let _ as HomeError {
  // Code for handling the exception
}

Commands might return an exception if an issue is detected with the execution flow. As a developer, you should use a do-catch block to properly handle these exceptions, and surface detailed information to users on cases where the errors are actionable. Unhandled exceptions will stop the app runtime and can result in crashes in your app.

Alternatively, use the off() or on() commands to explicitly set the state:

do {
  try await onOffTraitTest.off()
  try await onOffTraitTest.on()
} catch let _ as HomeError {
  // Code for handling the exception
}

After sending a command to change the state, once it completes you can read the state as described in Read a device state to handle it in your app.

Send a command with parameters

Some commands may use parameters, like those on the OnOffTrait or LevelControlTrait:

offWithEffect

// Turn off the light using the DyingLight effect.
do {
  try await onOffTraitTest.offWithEffect(
    effectIdentifier: Matter.OnOffTrait.EffectIdentifierEnum.dyingLight,
    effectVariant: 0
  )
} catch let _ as HomeError {
  // Code for handling the exception
}

moveToLevel

// Change the brightness of the light to 50%
do {
  try await levelControlTraitTest.moveToLevel(
    level: UInt8(127),
    transitionTime: 0,
    optionsMask: Matter.LevelControlTrait.OptionsBitmap(),
    optionsOverride: Matter.LevelControlTrait.OptionsBitmap()
  )
} catch let _ as HomeError {
  // Code for handling the exception
}

Check if a trait supports an attribute

Some devices may support a Matter trait, but not a specific attribute. For example, a Cloud-to-cloud device that was mapped to Matter may not support every Matter attribute. To handle cases like these, use the trait-level isSupported property to check if the attribute is supported for a particular device.

For example, to check for a device's support of the On/Off trait's onOff attribute:

// Check if the OnOff trait supports the onOff attribute.
if onOffTrait.attributes.$onOff.isSupported {
  print("onOffTrait supports onOff state")
} else {
  print("onOffTrait is for a command only device!")
}

Some attributes are nullable in the Matter specification or the Cloud-to-cloud smart home schema. For these attributes, you can determine whether a nil returned by the attribute is due to the device not reporting that value, or if the attribute's value actually is nil, by using isNullable in addition to isSupported:

// Check if a nullable attribute is set or is not supported.
if let deviceType = await device.types.get(OnOffLightDeviceType.self) {
  if let onOffTrait = deviceType.traits[Matter.OnOffTrait.self] {
    if onOffTrait.attributes.startUpOnOff == nil {
      if onOffTrait.attributes.$startUpOnOff.isSupported {
        print(
          "onOffTrait supports startUpOnOff and it is nil. Check the spec for the contextual meaning."
        )
      } else {
        print("onOffTrait does not support startUpOnOff!")
      }
    } else {
      print(
        "onOffTrait supports startUpOnOff and it is set to \(String(describing: onOffTrait.attributes.startUpOnOff))"
      )
    }
  }
}

Update trait attributes

If you want to change the value of a given attribute, and none of the trait's commands does so, the attribute may support having its value explicitly set.

Whether the value of an attribute can be changed depends on two factors:

  • Is the attribute writable?
  • Can the value of the attribute change as a side effect of sending a trait command?

The reference documentation for traits and their attributes provides this information.

Therefore, the combinations of properties that dictate how an attribute's value might be changed are:

Example of using the update function to change an attribute's value

This example shows how to explicitly set the value of the DoorLockTrait.wrongCodeEntryLimit attribute.

To set an attribute value, call the trait's update function and pass it an update function that sets the new value. It's a good practice to first verify that the trait supports an attribute.

For example:

if doorLockTraitTest.attributes.$wrongCodeEntryLimit.isSupported {
  let _ = try await doorLockTraitTest.update {
    $0.setWrongCodeEntryLimit(3)
  }
}

Control a device group

When you want to send a command to multiple devices, you can iterate over the devices, sending the same command to each individual device, one by one. The downside of this approach is that the command doesn't take effect all at once. When you're turning a set of lights on or off this way, the user experiences the so-called 'popcorn effect', which gives the impression that the underlying system is chaotic and unreliable.

When you want all the devices to execute the command simultaneously, a better approach is to send the command to the DeviceGroup's device type part. When you do this, the SDK relays the command simultaneously to all associated member devices in the group that support that capability, regardless of their underlying connectivity or transport (such as Matter or Cloud-to-cloud).

DeviceGroup conforms to the HasParts protocol and exposes the DeviceType parts supported by the devices in the group:

  • await group.part(_:) returns the first instance of the specified DeviceType if the group supports it, or nil otherwise.
  • try await group.parts(_:) returns an array of all top-level parts matching the specified DeviceType.

Group part trait attributes and error handling

When working with traits and commands on a DeviceGroup part, keep the following behaviors in mind:

  • Dynamic state attributes (such as the onOff attribute of the OnOffTrait or the currentLevel attribute of the LevelControlTrait) are generally not populated on a group part's trait because member devices in a group can be in different states that cannot be combined into a single value. However, supported commands (checked with properties such as supportsMoveToLevelWithOnOffCommand on a dimmable light) and select capability attributes (such as colorTempPhysicalMinMireds and colorTempPhysicalMaxMireds on ColorControlTrait for color lights) are populated so that your app can configure UI controls appropriately.
  • Not all commands on traits of group members are available on a group part. In particular, commands that return values are not available.
  • A command on a group part doesn't return individual results for each underlying member device command. To inspect individual command results or call commands that return values, iterate through the group's member devices directly.
  • Commands sent to a part on a DeviceGroup complete once all underlying command outcomes are collected. A command only fails (throws a HomeError) if the command fails for every associated member device or if all target devices are unavailable, surfacing as a single command failure matching the single-command call syntax. If at least one member device executes the command, the group command completes without throwing an error.

Send commands to a group part

To turn on all OnOffLightDeviceType lights in a device group:

if let lightPart = await group.part(OnOffLightDeviceType.self),
   let onOffTrait = lightPart.matterTraits.onOffTrait,
   onOffTrait.supportsOnCommand {
  try await onOffTrait.on()
}

You can also check multiple related device types (for example, turning off any light device type in the group that supports OnOffTrait):

func onOffTrait(in group: DeviceGroup) async -> Matter.OnOffTrait? {
  if let light = await group.part(OnOffLightDeviceType.self) {
    return light.matterTraits.onOffTrait
  }
  if let light = await group.part(DimmableLightDeviceType.self) {
    return light.matterTraits.onOffTrait
  }
  if let light = await group.part(ColorTemperatureLightDeviceType.self) {
    return light.matterTraits.onOffTrait
  }
  if let light = await group.part(ExtendedColorLightDeviceType.self) {
    return light.matterTraits.onOffTrait
  }
  return nil
}

if let trait = await onOffTrait(in: group) {
  try await trait.off()
}

You can check command support or read capability bounds on a group part before sending a command (for example, checking supportsMoveToLevelWithOnOffCommand on a dimmable light part or reading colorTempPhysicalMinMireds and colorTempPhysicalMaxMireds on a color temperature light part):

if let dimmableLight = await group.part(DimmableLightDeviceType.self),
   let levelTrait = dimmableLight.matterTraits.levelControlTrait,
   levelTrait.supportsMoveToLevelWithOnOffCommand {
  try await levelTrait.moveToLevelWithOnOff(
    level: 127,
    transitionTime: nil,
    optionsMask: [],
    optionsOverride: []
  )
}

if let colorLight = await group.part(ColorTemperatureLightDeviceType.self),
   let colorTrait = colorLight.matterTraits.colorControlTrait,
   let minMireds = colorTrait.attributes.colorTempPhysicalMinMireds,
   let maxMireds = colorTrait.attributes.colorTempPhysicalMaxMireds {
  let midMireds = UInt16((UInt32(minMireds) + UInt32(maxMireds)) / 2)
  try await colorTrait.moveToColorTemperature(
    colorTemperatureMireds: midMireds,
    transitionTime: 0,
    optionsMask: [],
    optionsOverride: []
  )
}

Supported group part device types

A DeviceGroup synthesizes parts for two Matter Light device types: OnOffLightDeviceType and DimmableLightDeviceType.

At least one member device in the group must support one of these device types. If a group has both an OnOffLightDeviceType and a DimmableLightDeviceType, sending Matter.OnOffTrait.on() to the group's DimmableLightDeviceType part also turns on the OnOffLightDeviceType (its supertype), because both belong to the same light component and support OnOffTrait.

Be sure to check at runtime to see if the command you want to send is supported on a DeviceGroup part.

Monitor group parts and connectivity

When devices are added to or removed from a DeviceGroup, or when member device capabilities change, the group dynamically recomputes its parts. Subscribe to home.entities(DeviceGroup.self).batched() to receive updated DeviceGroup snapshots and query group.part(_:) or group.parts(_:) to reflect the latest capabilities.

For example, to find supported DimmableLightDeviceType parts and check whether they support the OnOffTrait on command:

for try await groups in home.entities(DeviceGroup.self).batched().values {
  for group in groups {
    let dimmableParts = try await group.parts(DimmableLightDeviceType.self)
    for part in dimmableParts {
      let supportsOn = part.matterTraits.onOffTrait?.supportsOnCommand == true
      print("Group \(group.id) dimmable part supports on: \(supportsOn)")
    }
  }
}

A DeviceGroup also reports aggregated connectivity across the traits on its supported parts through group.sourceConnectivity.connectivityState:

  • When cloud connectivity is available, a group with supported parts reports .online even if member devices are offline, because the cloud provides a fallback route without tracking member connectivity on group parts. Check the member HomeDevice instances directly for individual device status.
  • When cloud connectivity is unavailable, connectivity reflects local Matter reachability across the group's parts: .online when all traits have a reachable local member, .partiallyOnline when only some traits are reachable locally, and .offline when none are reachable. A group with no supported parts reports .unknown.

When a home hub and the group's member Matter devices are reachable on the local network, commands sent to a DeviceGroup part are expanded and dispatched locally without requiring internet connectivity. Group management operations, such as creating, renaming, or deleting a group, or adding and removing member devices, require cloud connectivity.

For information about device connectivity, see Connectivity.

Target a group in an automation

You can also control a DeviceGroup in an automation by passing the DeviceGroup instance and a supported DeviceType.Type to an action node. Ensure that the constituent device types and traits are included in HomeClientConfiguration.referencedAutomationTypes.

automation(
  name: "Turn on evening lights",
  description: "Turn on all lights in the group at 7:00 PM"
) {
  starter(structure, Google.TimeTrait.ScheduledEvent.self) {
    Google.TimeTrait.ScheduledEvent.clockTime(
      TimeOfDay(hours: 19, minutes: 0, seconds: 0, nanos: 0)
    )
  }
  action(lightGroup, OnOffLightDeviceType.self) {
    Matter.OnOffTrait.on()
  }
}

To discover which automation actions a group supports, call structure.candidates(for: group) (or structure.candidates(includeDescendants: true)). Because a DeviceGroup does not aggregate dynamic state attributes or emit state-change events, it only produces CommandCandidate instances and can only be used in action nodes (not in starter or stateReader nodes).