자동화는 다중 부분 기기를 사용하지 않는 자동화와 유사하게 다중 부분 기기를 참조할 수 있습니다.
먼저 평소와 같이 구성요소 부품을 확보합니다. 멀티파트 기기 사용 방법은 멀티파트 기기를 참고하세요.
그런 다음 자동화에 사용할 각 부분에 대해 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 기기 유형은 다중 부분 기기로 취급될 수 있습니다. 이는 여러 하위 파트 캐비닛(예: 냉동고 또는 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))
}
}
}