iOS 上的多部分设备自动化操作

自动化操作可以引用多部分设备,就像不使用多部分设备的自动化操作一样。

首先,按照正常方式获取组件部分。如需了解如何使用 多部分设备,请参阅 多部分设备

然后,对于您要在自动化操作中使用的每个部分,创建一个 AutomationPartPath, 以便您在自动化启动器、条件和 操作中引用该部分。

//  Obtain a reference to the device:
let multipartDevices = try await self.home.devices(enableMultipartDevices: true).list()

let light = multipartDevices.first(where: {
  $0.parts.contains(OnOffLightDeviceType.self) && $0.structureID == structure.id
})

let lightDeviceType = await light.parts.get(OnOffLightDeviceType.self)

let lightPartPath = light.automationPart(lightDeviceType)

另一点需要注意的是,自动化操作通常将设备和设备类型作为参数。不过,引用 组件部分设备的自动化操作(同样使用AutomationPartPath)只需要 AutomationPartPath,因为除了组件设备引用之外,AutomationPartPath还包含对 设备类型的引用。

例如,在 Automation API 中,Refrigerator 设备类型可能会被视为多部分设备。它由根 RefrigeratorDevice 组成,该根设备可以包含多个子部分柜,例如冷冻柜或类型为 TemperatureControlledCabinetDevice 的标准柜。

如需构建冰箱自动化操作,您主要需要与两个标准 Matter特征进行交互:

  • RefrigeratorAlarm:通过 state 字段的 doorOpen 属性公开门状态闹钟。
  • RefrigeratorAndTemperatureControlledCabinetMode:允许读取和命令模式。例如,运行 changeToMode 等命令以切换到 LowEnergyRapidCoolLowNoise 等模式。

此示例自动化操作会在冰箱门打开时触发。如果门打开的时间超过两分钟,自动化操作会在智能扬声器上广播语音通知,闪烁厨房灯,并发送推送通知。请注意,此自动化操作仅影响设备的冰箱隔间,而忽略冷冻柜隔间(如果存在):

import GoogleHomeSDK
import GoogleHomeTypes

typealias RefrigeratorAlarmTrait = Matter.RefrigeratorAlarmTrait
typealias OnOffTrait = Matter.OnOffTrait

// Fetch devices using the multipart device model.
let multipartDevices = try await self.home.devices(enableMultipartDevices: true).list()

// Obtain a reference to the refrigerator device.
guard let refrigeratorDevice = multipartDevices.first {
    $0.types.contains(TemperatureControlledCabinetDeviceType.self) &&
    $0.traits.contains(Matter.RefrigeratorAndTemperatureControlledCabinetModeTrait.self) &&
    $0.traits.contains(Matter.RefrigeratorAlarmTrait.self)
  }

let refrigeratorDeviceType = await refrigeratorDevice.parts.get(RefrigeratorDeviceType.self).first

// Get all temperature-controlled cabinet parts of the refrigerator
let cabinets = refrigeratorDeviceType.parts(type: TemperatureControlledCabinetDeviceType.self)

// Find the cabinet part with the 'refrigerator' semantic tag
let refrigeratorCabinet = cabinets.first {
  $0.metadata.tags.contains(SemanticTag.Refrigerator.refrigerator)
}
var cabinetPartPath = refrigeratorDevice.automationPart(refrigeratorCabinet)

let structure = home.structures().list().first

let speaker = multipartDevices.first(where: {
        $0.types.contains(SpeakerDeviceType.self) && $0.structureID == structure.id
      })

let refrigeratorDoorAlert = automation(
  name: "Refrigerator Door Open Alert",
  description: "Warn when the refrigerator door has been open for over 2 min."
) {
  // 1. Starter: Monitor the refrigerator door alarm trait
  let alarmStarter = starter(
    cabinetPartPath,
    RefrigeratorAlarmTrait.self
  )

  alarmStarter

  // 2. Condition: Ensure the 'doorOpen' alarm remains active for 120 seconds
  condition(for: .seconds(120)) {
    alarmStarter.state.doorOpen.equals(true)
  }

  // 3. Actions: Execute parallel reactions
  parallel {
    // Broadcast warning to household speakers
    action(speaker, SpeakerDeviceType.self) {
      Google.AssistantBroadcastTrait.broadcast(msg: "The refrigerator door has been left open!")
    }

    // Push a notification alerts to home members' mobile devices
    action(structure) {
      Google.NotificationTrait.sendNotifications(
        title: "Fridge Alert",
        body: "The refrigerator door has been open for over 2 min.",
        optInMemberEmailsArray: ["222larabrown@gmail.com"]
      )
    }
  }
}

以下示例会在检测到家中无人时将冰箱切换到低能耗模式。

import GoogleHomeSDK
import GoogleHomeTypes

typealias AreaPresenceStateTrait = Google.AreaPresenceStateTrait
typealias RefrigeratorAndTemperatureControlledCabinetModeTrait = Matter.RefrigeratorAndTemperatureControlledCabinetModeTrait

let structure = home.structures().list().first()

// Fetch devices using the multipart device model.
let devices = try await self.home.devices(enableMultipartDevices: true).list()

// Obtain a reference to the refrigerator device.
guard let refrigeratorDevice = multipartDevices.first {
    $0.types.contains(TemperatureControlledCabinetDeviceType.self) &&
    $0.traits.contains(Matter.RefrigeratorAndTemperatureControlledCabinetModeTrait.self) &&
    $0.traits.contains(Matter.RefrigeratorAlarmTrait.self)
  }

let refrigeratorDeviceType = await refrigeratorDevice.parts.get(RefrigeratorDeviceType.self)

// Get all temperature-controlled cabinet parts of the refrigerator
let cabinets = refrigeratorDeviceType.parts(type: TemperatureControlledCabinetDeviceType.self)

// Find the cabinet part with the 'refrigerator' semantic tag
let refrigeratorCabinet = cabinets.first {
  $0.metadata.tags.contains(SemanticTag.Refrigerator.refrigerator)
}

let cabinetPartPath = refrigeratorDevice.automationPart(refrigeratorCabinet)

let refrigeratorEcoMode = automation(
  name: "Refrigerator Eco Mode",
  description: "Automatically changes refrigerator to low energy mode when house is vacant."
) {
  // 1. Starter: Monitor household presence changes
  let presenceStarter = starter(structure, AreaPresenceStateTrait.self)

  presenceStarter

  // 2. Condition: Verify presence state transitions to vacant
  condition {
    presenceStarter.presenceState.equals(.presenceStateVacant)
  }
  // 3. Action: Set refrigerator cabinet Mode to 'Low Energy' (commonly option index 1)
  action(cabinetPartPath) {
    RefrigeratorAndTemperatureControlledCabinetModeTrait.changeToMode(newMode: 1)
  }
}