자동화는 멀티파트 기기를 사용하지 않는 자동화와 마찬가지로 멀티파트 기기를 참조할 수 있습니다.
먼저 평소와 같이 구성요소 파트를 획득합니다. 멀티파트 기기 작업 방법은 멀티파트 기기를 참고하세요.
그런 다음 자동화에서 사용할 각 파트에 대해 자동화 시작, 조건, 작업에서 파트를 참조할 수 있는 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에는 구성요소 기기 참조 외에도 기기 유형에 대한 참조가 이미 포함되어 있기 때문입니다.
예를 들어 자동화 API에서 Refrigerator 기기 유형은 멀티파트 기기로 취급될 수 있습니다. TemperatureControlledCabinetDevice 유형의 냉동고나 표준 캐비닛과 같은 여러 하위 부품 캐비닛을 포함할 수 있는 루트 RefrigeratorDevice로 구성됩니다.
냉장고 자동화를 빌드하려면 주로 다음 두 가지 표준 Matter 특성과 상호작용합니다.
RefrigeratorAlarm:state필드의doorOpen속성을 통해 문 상태 알람을 노출합니다.RefrigeratorAndTemperatureControlledCabinetMode: 모드를 읽고 명령할 수 있습니다. 예를 들어changeToMode과 같은 명령어를 실행하여LowEnergy,RapidCool또는LowNoise과 같은 모드로 전환합니다.
이 자동화 예시는 냉장고 문이 열릴 때 트리거됩니다. 문이 2분 이상 열려 있으면 자동화가 스마트 스피커에 음성 알림을 브로드캐스트하고, 주방 조명을 깜박이고, 푸시 알림을 전송합니다. 이 자동화는 기기의 냉장고 칸에만 영향을 미치며 냉동고 칸 (있는 경우)은 무시합니다.
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))
}
}
}