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 的「活動記錄」分頁中。
    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_REPORTED")   // b. set the clientFeatureGroup
    .setAutomationGraph(automationGraph)                       // c. Attach the logic graph
    .setAppManagedAutomationConfig(automationConfig)           // d. Set the automationConfig
    .build() // 4. build the automation