Automatyzacja może odwoływać się do urządzeń wieloczęściowych podobnie jak automatyzacja, która nie korzysta z takich urządzeń.
Najpierw uzyskaj komponenty w zwykły sposób. Informacje o tym, jak pracować z urządzeniami wieloczęściowymi, znajdziesz w artykule Urządzenia wieloczęściowe.
Następnie dla każdej części, której chcesz użyć w automatyzacji, utwórz
AutomationPartPath,
który umożliwi Ci odwoływanie się do tej części w starterach, warunkach i
działaniach automatyzacji.
// 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)
Pamiętaj też, że działania automatyzacji zwykle przyjmują jako parametry urządzenie i jego typ. Jednak działanie automatyzacji, które odwołuje się do
urządzenia będącego częścią składową – ponownie za pomocą AutomationPartPath – potrzebuje tylko
AutomationPartPath, ponieważ oprócz odniesienia do urządzenia będącego częścią składową zawiera ono też
odniesienie do typu urządzenia.AutomationPartPath
Na przykład w Automation API typ urządzenia Refrigerator może być traktowany jako urządzenie wieloczęściowe. Składa się ono z głównego urządzenia RefrigeratorDevice, które może zawierać wiele podrzędnych części składowych, takich jak zamrażarki lub standardowe szafki typu TemperatureControlledCabinetDevice.
Aby tworzyć automatyzacje lodówki, musisz przede wszystkim korzystać z 2 standardowych Matter cech:
RefrigeratorAlarm: udostępnia alarmy stanu drzwi za pomocą atrybutudoorOpenpolastate.RefrigeratorAndTemperatureControlledCabinetMode: umożliwia odczytywanie i sterowanie trybami. Na przykład uruchamianie poleceń takich jakchangeToMode, aby przełączać się między trybamiLowEnergy,RapidCoollubLowNoise.
Ta przykładowa automatyzacja jest uruchamiana, gdy otworzą się drzwi lodówki. Jeśli drzwi pozostaną otwarte przez ponad 2 minuty, automatyzacja wyśle powiadomienie głosowe na głośniki inteligentne, włączy światła w kuchni i wyśle powiadomienie push. Pamiętaj, że ta automatyzacja wpływa tylko na komorę lodówki, ignorując komorę zamrażarki (jeśli istnieje):
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"]
)
}
}
}
Poniższy przykład przełącza lodówkę w tryb niskiego zużycia energii, gdy wykryje, że nikogo nie ma w domu.
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)
}
}