应用管理的自动化操作是指由合作伙伴应用使用 Automation API 以编程方式构建的自动化操作。
此类自动化操作不会显示在 Google Home app (GHA) 自动化操作列表中。
当应用管理的自动化操作触发时,写入 Home API 活动动态的信息可以自定义。例如,如果合作伙伴提供名为“Arctic”的品牌服务,该服务使用应用管理的自动化操作实现,则合作伙伴可以指定,当其应用管理的自动化操作运行时,活动动态中会引用 Arctic 服务,而不是实际的自动化操作名称。
创建应用管理的自动化操作
如需创建应用管理的自动化操作,请按以下步骤操作。
构建自动化操作的逻辑图。
如需自定义自动化操作的活动在活动动态中的显示方式,请使用您希望在动态中显示(而非自动化操作名称)的本地化消息填充
AppManagedAutomationConfig实例。这可以是应用或服务的本地化名称,也可以是您想要的任何其他消息。如果您未提供任何本地化字符串,则自动化执行会像任何其他自动化一样记录。Google 建议您设置这些本地化字符串,以确保提供最佳用户体验。let automationConfig = AppManagedAutomationConfig( managingAppDisplayNames: [ LocalizedText(text: "My Custom SmartApp", locale: "en-US"), LocalizedText(text: "Mi aplicación personalizada", locale: "es-MX") ] )使用
Automation.Builder创建自动化实例。- 设置自动化操作的
name。 - 将
client_feature_group设置为"APP_MANAGED_EXECUTION_REPORTED",以便自动化的执行历史记录显示在 GHA 的“活动”标签页中。 - 设置
automationGraph。 - 可选:设置自动化的
appManagedAutomationConfig(请参阅第 2 步)。
- 设置自动化操作的
最后,调用
build()以构建Automation实例。
以下示例展示了整个过程:
import GoogleHomeSDK
import GoogleHomeTypes
let devices = try await home.devices().list()
let mySensor: HomeDevice = devices.first(where: {
$0.types.contains(OccupancySensorDeviceType.self) &&
$0.structureID == structure.id
})
let myLight: HomeDevice = devices.first(
where: {
$0.types.contains(OnOffLightDeviceType.self) &&
$0.structureID == structure.id
}
)
// 1. Create logic graph
let starterNode = StarterNode(
device: mySensor,
trait: Matter.OccupancySensingTrait.self
)
let actionNode = ActionNode(
device: myLight,
command: Matter.OnOffTrait.on()
)
// Bundle nodes into a sequential graph
let automationGraph = SequentialFlow(
nodes: [starterNode, actionNode]
)
// 2. Build the AppManagedAutomationConfig with the localized app names
let automationConfig = AppManagedAutomationConfig(
managingAppDisplayNames: [
LocalizedText(text: "My Custom SmartApp", locale: "en-US"),
LocalizedText(text: "Mi aplicación personalizada", locale: "es-MX")
]
)
// 3. Build the Automation with both configuration and the graph
let myFunctionalAutomation = try AutomationBuilder()
.setName("Motion Activated Light") // a. Set the name
.setClientFeatureGroup("APP_MANAGED_EXECUTION_REPORTED") // b. set the clientFeatureGroup
.setAutomationGraph(automationGraph) // c. Attach the logic graph
.setAppManagedAutomationConfig(automationConfig) // d. Set the automationConfig
.build() // 4. build the automation