iOS पर एक से ज़्यादा डिवाइसों को अपने-आप कंट्रोल करने की सुविधा

ऑटोमेशन, मल्टीपार्ट डिवाइसों को उसी तरह से रेफ़र कर सकता है जिस तरह से मल्टीपार्ट डिवाइसों का इस्तेमाल न करने वाला ऑटोमेशन करता है.

सबसे पहले, कॉम्पोनेंट के उन हिस्सों को हासिल करें जिन्हें आपको आम तौर पर हासिल करना होता है. मल्टीपार्ट डिवाइसों के साथ काम करने का तरीका जानने के लिए, मल्टीपार्ट डिवाइस देखें.

इसके बाद, ऑटोमेशन में इस्तेमाल किए जाने वाले हर हिस्से के लिए, एक 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 डिवाइस टाइप को मल्टीपार्ट डिवाइस माना जा सकता है. इसमें एक रूट RefrigeratorDevice होता है. इसमें कई सबपार्ट कैबिनेट शामिल हो सकते हैं. जैसे, फ़्रीज़र या TemperatureControlledCabinetDevice टाइप के स्टैंडर्ड कैबिनेट.

रेफ़्रिजरेटर के लिए ऑटोमेशन बनाने के लिए, मुख्य रूप से इन दो स्टैंडर्ड Matter ट्रेट के साथ इंटरैक्ट किया जाता है:

  • RefrigeratorAlarm: state फ़ील्ड के doorOpen एट्रिब्यूट के ज़रिए, दरवाज़े के स्टेटस के अलार्म दिखाता है.
  • RefrigeratorAndTemperatureControlledCabinetMode: इससे पढ़ने और निर्देश देने वाले मोड को चालू किया जा सकता है. उदाहरण के लिए, changeToMode जैसे निर्देश देकर LowEnergy, RapidCool या LowNoise जैसे मोड पर स्विच करना.

इस उदाहरण में दिखाया गया ऑटोमेशन, रेफ़्रिजरेटर का दरवाज़ा खुलने पर ट्रिगर होता है. अगर दरवाज़ा दो मिनट से ज़्यादा समय तक खुला रहता है, तो ऑटोमेशन की सुविधा के ज़रिए स्मार्ट स्पीकर पर एक सूचना ब्रॉडकास्ट की जाती है. साथ ही, किचन की लाइटें चालू हो जाती हैं और एक पुश सूचना भेजी जाती है. ध्यान दें कि ऑटोमेशन की इस सुविधा का असर सिर्फ़ डिवाइस के रेफ़्रिजरेटर कंपार्टमेंट पर पड़ता है. यह फ़्रीज़र कंपार्टमेंट (अगर मौजूद है) पर लागू नहीं होता:

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)
  }
}