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

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

首先,像往常一样获取组件部件。如需了解如何使用多部件设备,请参阅多部件设备

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

// Obtain a reference to the device:
val multipartDevices = structure.devices(enableMultipartDevices = true).first().toList()

val lights = multipartDevices.filter { it.has(OnOffLightDevice) && it.has(OnOff) }

val light = lights.first()

// get the AutomationPartPath for the device
val lightPartPath = light.automationPart(light.part(OnOffLightDevice).first())

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

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

如需构建冰箱自动化操作,您主要需要与以下两种标准 Matter 特征互动:

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

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

import com.google.home.automation.action
import com.google.home.automation.automation
import com.google.home.automation.condition
import com.google.home.automation.equals
import com.google.home.automation.starter
import com.google.home.google.AssistantBroadcast
import com.google.home.google.Notification
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffLightDevice
import com.google.home.matter.standard.RefrigeratorAlarm
import com.google.home.matter.standard.RefrigeratorAlarm.Companion.state
import com.google.home.matter.standard.RefrigeratorDevice
import com.google.home.matter.standard.SpeakerDevice
import java.time.Duration

val structure: Structure = home.structures().first()

// Fetch devices using the multipart device model.
var multipartDevices = homeManager.devices(enableMultipartDevices = true)

// Obtain a reference to the refrigerator device.
val refrigeratorDevice = multipartDevices.first {
  it.has(TemperatureControlledCabinetDevice) &&
  it.has(RefrigeratorAndTemperatureControlledCabinetMode) &&
  it.has(RefrigeratorAlarm)
}

// Get all temperature-controlled cabinet parts of the refrigerator
val cabinets = refrigeratorDevice.parts(TemperatureControlledCabinetDevice)

// Find the cabinet part with the 'refrigerator' semantic tag
val refrigeratorCabinet = cabinets.firstOrNull {
  it.metadata.tags.contains(SemanticTag.Refrigerator.refrigerator)
}

val cabinetPartPath = refrigeratorCabinet.automationPath(TemperatureControlledCabinetDevice)

val speaker = home.devices().list().first { device -> device.has(SpeakerDevice) }

val refrigeratorDoorAlert = automation {
  name = "Refrigerator Door Open Alert"
  description = "Warn when the refrigerator door has been open for over 2 min"
  isActive = true

  sequential {
    // 1. Starter: Monitor the refrigerator door alarm trait
    val alarmStarter = starter<_>(cabinetPartPath, RefrigeratorAlarm)

    // 2. Condition: Ensure the 'doorOpen' alarm remains active for 2 continuous min
    condition {
      expression = alarmStarter.state.doorOpen equals true
      forDuration(Duration.ofMinutes(2))
    }

    // 3. Actions: Execute parallel reactions
    parallel {
      // Broadcast warning to household speakers
      action(speaker, SpeakerDevice) {
        command(AssistantBroadcast.broadcast("The refrigerator door has been left open!"))
      }

      // Push a notification alerts to home members' mobile devices
      action(structure) {
        command(Notification.sendNotifications(
          "Fridge Alert",
          { body = "The refrigerator door has been open for over 2 min" }
        ))
      }
    }
  }
}

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

import com.google.home.automation.action
import com.google.home.automation.automation
import com.google.home.automation.condition
import com.google.home.automation.equals
import com.google.home.automation.starter
import com.google.home.google.AreaPresenceState
import com.google.home.google.AreaPresenceState.Companion.presenceState
import com.google.home.google.AreaPresenceStateTrait.PresenceState
import com.google.home.matter.standard.RefrigeratorAndTemperatureControlledCabinetMode
import com.google.home.matter.standard.RefrigeratorAndTemperatureControlledCabinetMode.Companion.changeToMode
import com.google.home.matter.standard.RefrigeratorDevice
import com.google.home.matter.standard.TemperatureControlledCabinetDevice

val structure: Structure = home.structures().first()

// Fetch devices using the multipart device model.
var multipartDevices = homeManager.devices(enableMultipartDevices = true)

// Obtain a reference to the refrigerator device.
val refrigeratorDevice = multipartDevices.first {
  it.has(TemperatureControlledCabinetDevice) &&
  it.has(RefrigeratorAndTemperatureControlledCabinetMode) &&
  it.has(RefrigeratorAlarm)
}

val refrigeratorPartPath = refrigeratorDevice.automationPart(refrigeratorDevice.part(RefrigeratorDevice).first())

// Get all temperature-controlled cabinet parts of the refrigerator
val cabinets = refrigeratorDevice.parts(TemperatureControlledCabinetDevice )

// Find the cabinet part with the 'refrigerator' semantic tag
val refrigeratorCabinet = cabinets.firstOrNull {
  it.metadata.tags.contains(SemanticTag.Refrigerator.refrigerator)
}

val refrigeratorEcoMode = automation {
  name = "Refrigerator Eco Mode"
  description = "Automatically changes refrigerator to low energy mode when house is vacant."
  isActive = true

  sequential {
    // 1. Starter: Monitor household presence changes
    val presenceStarter = starter<_>(structure, AreaPresenceState)

    // 2. Condition: Verify presence state transitions to vacant
    condition {
      expression = presenceStarter.presenceState equals PresenceState.PresenceStateVacant
    }

    // 3. Action: Set refrigerator cabinet Mode to 'Low Energy' (e.g. Mode 1u)
    action(refrigeratorPartPath {
      command(RefrigeratorAndTemperatureControlledCabinetMode.changeToMode(1u))
    }
  }
}