iOS 應用程式管理的自動化動作

應用程式管理的自動化動作是指合作夥伴應用程式使用 Automation API,以程式輔助方式建構的自動化動作。

這類自動化動作不會顯示在自動化動作清單中。Google Home app (GHA)

應用程式管理的自動化動作觸發時,寫入 Home API 活動動態消息的資訊可以自訂,也可以完全隱藏。舉例來說,如果合作夥伴提供名為「Arctic」的品牌服務,並使用應用程式管理的自動化功能實作,合作夥伴可以指定在執行應用程式管理的自動化功能時,活動動態消息會參照 Arctic 服務,而非實際的自動化功能名稱。

建立應用程式管理的自動化動作

如要建立應用程式管理的自動化動作,請按照下列步驟操作。

  1. 建構自動化動作的邏輯圖。

  2. 選用:如要讓自動化動作的活動顯示在活動動態消息中,可以填入 AppManagedAutomationConfig 執行個體,並在動態消息中顯示您想顯示的本地化訊息,取代自動化動作名稱。可以是應用程式或服務的本地化名稱,也可以是任何其他訊息。如果您未提供任何在地化字串,系統會像記錄其他自動化動作一樣,記錄自動化執行作業。Google 建議您設定這些本地化字串,確保提供最佳使用者體驗。

    let automationConfig = AppManagedAutomationConfig(
        managingAppDisplayNames: [
          LocalizedText(text: "My Custom SmartApp", locale: "en-US"),
          LocalizedText(text: "Mi aplicación personalizada", locale: "es-MX")
        ]
    )
    
  3. 使用 Automation.Builder 建立自動化執行個體。

    1. 設定自動化動作的 name
    2. client_feature_group 設為 "APP_MANAGED_EXECUTION_REPORTED",即可在 GHA 的「活動」分頁中查看自動化動作的執行記錄;設為 "APP_MANAGED_EXECUTION_UNREPORTED" 則不會顯示執行記錄。
    3. 設定 automationGraph
    4. 選用 設定自動化動作的 appManagedAutomationConfig (請參閱步驟 2)。
  4. 最後,呼叫 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_UNREPORTED") // b. set the clientFeatureGroup
    .setAutomationGraph(automationGraph)                       // c. Attach the logic graph
    .setAppManagedAutomationConfig(automationConfig)           // d. Set the automationConfig
    .build() // 4. build the automation