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