Android पर ऑटोमेशन बनाना

Android के लिए Home API की मदद से, Automation API ऐक्सेस किए जा सकते हैं. हालांकि, इनका एंट्री पॉइंट एक स्ट्रक्चर के ज़रिए होता है. इसलिए, इनका इस्तेमाल करने से पहले, स्ट्रक्चर पर अनुमति दी जानी चाहिए.

किसी स्ट्रक्चर के लिए अनुमतियां मिलने के बाद, अपने ऐप्लिकेशन में ये पैकेज इंपोर्ट करें:


import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.Id
import com.google.home.Structure

किसी स्ट्रक्चर में HasAutomations इंटरफ़ेस होता है. इसमें, ऑटोमेशन से जुड़े ये तरीके शामिल होते हैं:

एपीआई ब्यौरा
automations() स्ट्रक्चर से जुड़े सभी ऑटोमेशन की सूची दिखाता है. सिर्फ़ वे ऑटोमेशन दिखाए जाते हैं जिन्हें आपने Home API की मदद से बनाया है.
createAutomation(automation) किसी स्ट्रक्चर के लिए, ऑटोमेशन का इंस्टेंस बनाता है.
deleteAutomation(automationId) आईडी के हिसाब से, ऑटोमेशन का इंस्टेंस मिटाता है.

कोई ऑटोमेशन बनाएं

Home का इंस्टेंस बनाने और उपयोगकर्ता से अनुमतियां पाने के बाद, स्ट्रक्चर और डिवाइस(डिवाइसों) की जानकारी पाएं:

val structure = homeManager.structures().list().single()
val device = homeManager.devices().get(Id("myDevice"))!!

इसके बाद, Automation DSL का इस्तेमाल करके, अपने ऑटोमेशन की लॉजिक तय करें. Home API में, ऑटोमेशन को Automation इंटरफ़ेस से दिखाया जाता है. इस इंटरफ़ेस में, प्रॉपर्टी का एक सेट होता है:

  • मेटाडेटा, जैसे कि नाम और ब्यौरा.
  • फ़्लैग. इनसे यह पता चलता है कि ऑटोमेशन को लागू किया जा सकता है या नहीं.
  • नोड की सूची. इसमें ऑटोमेशन की लॉजिक शामिल होती है. इसे ऑटोमेशन ग्राफ़ कहा जाता है. इसे automationGraph प्रॉपर्टी से दिखाया जाता है.

automationGraphडिफ़ॉल्ट रूप से, SequentialFlow टाइप का होता है. यह एक क्लास है, जिसमें नोड की सूची होती है. ये नोड क्रम से लागू होते हैं. हर नोड, ऑटोमेशन के किसी एलिमेंट को दिखाता है. जैसे, स्टार्टर, शर्त या कार्रवाई.

ऑटोमेशन को name और description असाइन करें.

ऑटोमेशन बनाने पर, isActive फ़्लैग डिफ़ॉल्ट रूप से true पर सेट होता है. इसलिए, इस फ़्लैग को साफ़ तौर पर सेट करने की ज़रूरत नहीं होती. हालांकि, अगर आपको शुरुआत में ऑटोमेशन को बंद रखना है, तो इसे सेट किया जा सकता है. ऐसे में, ऑटोमेशन बनाते समय, फ़्लैग को false पर सेट करें.

DraftAutomation इंटरफ़ेस का इस्तेमाल, ऑटोमेशन बनाने और तैयार करने के लिए किया जाता है. वहीं, Automation इंटरफ़ेस का इस्तेमाल, ऑटोमेशन की जानकारी पाने के लिए किया जाता है. उदाहरण के लिए, यहां Automation DSL दिया गया है. यह एक ऐसे ऑटोमेशन के लिए है जो किसी डिवाइस के चालू होने पर, दूसरे डिवाइस को चालू करता है:

import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Condition
import com.google.home.automation.DraftAutomation
import com.google.home.automation.Equals
import com.google.home.automation.Node
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.matter.standard.OnOff
import com.google.home.Structure

...

val automation: DraftAutomation = automation {
  name = "MyFirstAutomation"
  description = "Turn on a device when another device is turned on."
  sequential {
    val starterNode = starter<_>(device1, OnOffLightDevice, trait=OnOff)
    condition() { expression = stateReaderNode.onOff equals true }
    action(device2, OnOffLightDevice) { command(OnOff.on()) }
  }
}

ऑटोमेशन DSL तय करने के बाद, createAutomation() तरीके में पास करें ताकि DraftAutomation इंस्टेंस बनाया जा सके:

val createdAutomation = structure.createAutomation(automation)

इसके बाद, ऑटोमेशन पर, ऑटोमेशन के अन्य सभी तरीकों का इस्तेमाल किया जा सकता है. जैसे, execute(), stop() और update().

पुष्टि से जुड़ी गड़बड़ियां

अगर ऑटोमेशन बनाने की प्रोसेस, पुष्टि के दौरान पास नहीं होती है, तो चेतावनी या गड़बड़ी के मैसेज में समस्या के बारे में जानकारी मिलती है. ज़्यादा जानकारी के लिए, ValidationIssueType रेफ़रंस देखें.

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

आपको हमेशा isValid की पुष्टि करनी चाहिए. साथ ही, दिखाए गए validationIssues इंस्टेंस पर Automation की जांच करनी चाहिए:

val createdAutomation = structure.createAutomation(automation)

if (!createdAutomation.isValid) {
  // Iterate through validation issues to identify errors and warnings
  for (issue in createdAutomation.validationIssues) {
    when (issue.severity) {
      ValidationIssueSeverity.ERROR -> {
        Log.e(
          "Automation",
          "Validation error on node ${issue.node}: ${issue.issueType}"
        )
        // Handle error (for example, prompt the user to enable missing
        // consents or device features)
      }
      ValidationIssueSeverity.WARNING -> {
        Log.w(
          "Automation",
          "Validation warning on node ${issue.node}: ${issue.issueType}"
        )
      }
      else -> {}
    }
  }
}

कोड के उदाहरण

यहां कोड का एक उदाहरण दिया गया है. इसका इस्तेमाल, Android पर ऑटोमेशन डिज़ाइन करना पेज पर बताए गए, काल्पनिक ऑटोमेशन के कुछ हिस्सों को लागू करने के लिए किया जा सकता है.

सामान्य ऑटोमेशन

सुबह 8:00 बजे पर्दे उठाने वाले ऑटोमेशन को इस तरह लागू किया जा सकता है:

// get all the automation node candidates in the structure
val allCandidates = structure.allCandidates().first()
// determine whether a scheduled automation can be constructed
val isSchedulingSupported =
  allCandidates.any {
    it is EventCandidate &&
      it.eventFactory == Time.ScheduledTimeEvent &&
      it.unsupportedReasons.isEmpty()
  }
// get the blinds present in the structure
val blinds =
  allCandidates
    .filter {
      it is CommandCandidate &&
        it.commandDescriptor == WindowCoveringTrait.UpOrOpenCommand &&
        it.unsupportedReasons.isEmpty()
    }
    .map { it.entity }
    .filterIsInstance<HomeDevice>()
    .filter { it.has(WindowCoveringDevice) }
 if (isSchedulingSupported && blinds.isNotEmpty()) {
  // Proceed to create automation
  val automation: DraftAutomation = automation {
    name = "Day time open blinds"
    description = "Open all blinds at 8AM everyday"
    isActive = true
    sequential {
      // At 8:00am local time....
      val unused =
        starter(structure, Time.ScheduledTimeEvent) {
          parameter(Time.ScheduledTimeEvent.clockTime(LocalTime.of(8, 0, 0, 0)))
        }
        // ...open all the blinds
       parallel {
        for (blind in blinds) {
          action(blind, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
        }
      }
    }
  }
   val createdAutomation = structure.createAutomation(automation)
} else if (!isSchedulingSupported) {
  // Cannot create automation.
  // Set up your address on the structure, then try again.
} else {
  // You don't have any WindowCoveringDevices.
  // Try again after adding some blinds to your structure.
}

जटिल ऑटोमेशन

मोशन का पता चलने पर, लाइटें ब्लिंक करने वाले ऑटोमेशन को इस तरह लागू किया जा सकता है:

import com.google.home.Home
import com.google.home.HomeClient
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.action
import com.google.home.automation.automation
import com.google.home.automation.equals
import com.google.home.automation.parallel
import com.google.home.automation.starter
import com.google.home.google.AssistantBroadcast
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.toggle
import com.google.home.matter.standard.OnOffLightDevice
import java.time.Duration

// get all the automation node candidates in the structure
val allCandidates = structure.allCandidates().first()

// get the lights present in the structure
val availableLights = allCandidates.filter {
   it is CommandCandidate &&
   it.commandDescriptor == OnOffTrait.OnCommand
}.map { it.entity }
.filterIsInstance<HomeDevice>()
.filter {it.has(OnOffLightDevice) ||
         it.has(ColorTemperatureLightDevice) ||
         it.has(DimmableLightDevice) ||
         it.has(ExtendedColorLightDevice)}

val selectedLights = ... // user selects one or more lights from availableLights

automation {
isActive = true

sequential {
   // If the presence state changes...
   val starterNode = starter<_>(structure, AreaPresenceState)
   // ...and if the area is occupied...
   condition() {
      expression = starterNode.presenceState equals PresenceState.PresenceStateOccupied
   }
   // "blink" the light(s)
   parallel {
            for(light in selectedLights) {
            action(light, OnOffLightDevice) { command(OnOff.toggle()) }
            delayFor(Duration.ofSeconds(1))
            action(light, OnOffLightDevice) { command(OnOff.toggle()) }
            delayFor(Duration.ofSeconds(1))
            action(light, OnOffLightDevice) { command(OnOff.toggle()) }
            delayFor(Duration.ofSeconds(1))
            action(light, OnOffLightDevice) { command(OnOff.toggle())}
         }
      }
   }
}

इकाई फ़िल्टर की मदद से, डिवाइसों को डाइनैमिक तरीके से चुनना

ऑटोमेशन लिखते समय, आपके पास सिर्फ़ खास डिवाइसों को चुनने का विकल्प नहीं होता. इकाई फ़िल्टर नाम की सुविधा की मदद से, आपका ऑटोमेशन अलग-अलग शर्तों के आधार पर, रनटाइम में डिवाइस चुन सकता है.

उदाहरण के लिए, इकाई फ़िल्टर का इस्तेमाल करके, आपका ऑटोमेशन इन डिवाइसों को टारगेट कर सकता है:

  • किसी खास तरह के सभी डिवाइस
  • किसी खास कमरे में मौजूद सभी डिवाइस
  • किसी खास कमरे में मौजूद, किसी खास तरह के सभी डिवाइस
  • चालू किए गए सभी डिवाइस
  • किसी खास कमरे में चालू किए गए सभी डिवाइस

इकाई फ़िल्टर का इस्तेमाल करने के लिए:

  1. Structure या Room पर, atExecutionTime() को कॉल करें. इससे TypedExpression<TypedEntity<StructureType>> मिलता है.
  2. इस ऑब्जेक्ट पर, getDevicesOfType() को कॉल करें. साथ ही, इसे DeviceType पास करें.

इकाई फ़िल्टर का इस्तेमाल, स्टार्टर, स्टेट रीडर, और कार्रवाइयों में किया जा सकता है.

उदाहरण के लिए, किसी भी चालू/बंद होने वाली लाइट से स्टार्टर के तौर पर ऑटोमेशन ट्रिगर करने के लिए:

// If any light is turned on or off
val starter =
  starter(
    entityExpression = structure.atExecutionTime().getDevicesOfType(OnOffLightDevice),
    trait = OnOff,
  )

स्टेट रीडर में, किसी स्ट्रक्चर में मौजूद सभी लाइटों (खास तौर पर, चालू/बंद होने वाली लाइटों) की OnOff स्थिति कैप्चर करने के लिए:

// Build a Map<Entity, OnOff>
val onOffStateOfAllLights =
  stateReader(
    entityExpression = structure.atExecutionTime().getDevicesOfType(OnOffLightDevice),
    trait = OnOff,
  )

किसी खास कमरे में मौजूद लाइटों की जानकारी पाने और उन्हें किसी शर्त में इस्तेमाल करने के लिए:

val livingRoomLights =
  stateReader(
    entityExpression = livingRoom.atExecutionTime().getDevicesOfType(OnOffLightDevice),
    trait = OnOff,
  )
// Are any of the lights in the living room on?
condition { expression = livingRoomLights.values.any { it.onOff equals true } }

रनटाइम में:

स्थिति नतीजा
कोई भी डिवाइस, स्टार्टर में दी गई शर्तों को पूरा नहीं करता. ऑटोमेशन ट्रिगर नहीं होता.
कोई भी डिवाइस, स्टेट रीडर में दी गई शर्तों को पूरा नहीं करता. ऑटोमेशन शुरू होता है, लेकिन यह शर्त वाले नोड के आधार पर जारी रहेगा.
कोई भी डिवाइस, किसी कार्रवाई में दी गई शर्तों को पूरा नहीं करता. ऑटोमेशन शुरू होता है, लेकिन कार्रवाई से कुछ नहीं होता.

यहां एक ऐसे ऑटोमेशन का उदाहरण दिया गया है जो किसी एक लाइट के बंद होने पर, हॉल की लाइट को छोड़कर बाकी सभी लाइटें बंद कर देता है:

val unused = automation {
  sequential {
    // If any light is turned on or off
    val starter =
      starter(
        entityExpression = structure.atExecutionTime().getDevicesOfType(OnOffLightDevice),
        trait = OnOff,
      )
    condition {
      // Check to see if the triggering light was turned off
      expression = starter.onOff equals false
    }
    // Turn off all lights except the hall light
    action(
      entityExpression =
        structure.atExecutionTime().getDevicesOfType(OnOffLightDevice).filter {
          it notEquals entity(hallwayLight, OnOffLightDevice)
        }
    ) {
      command(OnOff.on())
    }
  }
}

ऑटोमेशन लागू करना

बनाए गए ऑटोमेशन को execute() तरीके का इस्तेमाल करके लागू करें:

createdAutomation.execute()

अगर ऑटोमेशन में मैन्युअल स्टार्टर है, तो उस पॉइंट से ऑटोमेशन शुरू करता है. साथ ही, मैन्युअल स्टार्टर से पहले के सभी नोड को अनदेखा कर देता है.execute() अगर ऑटोमेशन में मैन्युअल स्टार्टर नहीं है, तो पहले स्टार्टर नोड के बाद वाले नोड से ऑटोमेशन लागू होना शुरू होता है.

अगर execute() कार्रवाई पूरी नहीं होती है, तो HomeException दिख सकता है. गड़बड़ी को ठीक करना देखें.

ऑटोमेशन रोकना

ऑटोमेशन को रोकने के लिए, stop() तरीके का इस्तेमाल करें:


createdAutomation.stop()

अगर stop() कार्रवाई पूरी नहीं होती है, तो HomeException दिख सकता है. गड़बड़ी को ठीक करना देखें.

किसी स्ट्रक्चर के लिए, ऑटोमेशन की सूची पाना

ऑटोमेशन, स्ट्रक्चर के लेवल पर तय किए जाते हैं. ऑटोमेशन के Flow को ऐक्सेस करने के लिए, स्ट्रक्चर के automations() पर इकट्ठा करें:


import com.google.home.automation.Automation
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure

...

val structure = homeManager.structures().list().single()
structure.automations().collect {
  println("Available automations:")
  for (automation in it) {
    println(String.format("%S %S", "$automation.id", "$automation.name"))
  }
}

इसके अलावा, इसे किसी लोकल Collection को असाइन करें:

import com.google.home.automation.Automation
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure

...

var myAutomations: Collection<Automation> = emptyList()
myAutomations = structure.automations()

आईडी के हिसाब से ऑटोमेशन पाना

ऑटोमेशन आईडी के हिसाब से ऑटोमेशन पाने के लिए, स्ट्रक्चर पर automations() तरीका कॉल करें. साथ ही, आईडी के हिसाब से मैच करें:

import com.google.home.automation.Automation
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure

...

val structure = homeManager.structures().list().single()
val automation: DraftAutomation = structure.automations().mapNotNull {
  it.firstOrNull
    { automation -> automation.id == Id("automation-id") }
  }.firstOrNull()

जवाब:

// Here's how the automation looks like in the get response.
// Here, it's represented as if calling a println(automation.toString())

Automation(
  name = "automation-name",
  description = "automation-description",
  isActive = true,
  id = Id("automation@automation-id"),
  automationGraph = SequentialFlow(
    nodes = [
      Starter(
        entity="device@test-device",
        type="home.matter.0000.types.0101",
        trait="OnOff@6789..."),
      Action(
        entity="device@test-device",
        type="home.matter.0000.types.0101",
        trait="OnOff@8765...",
        command="on")
    ]))

नाम के हिसाब से ऑटोमेशन पाना

एपीआई कॉल को और बेहतर बनाने के लिए, Kotlin में filter() तरीके का इस्तेमाल किया जा सकता है. नाम के हिसाब से ऑटोमेशन पाने के लिए, स्ट्रक्चर के ऑटोमेशन पाएं और ऑटोमेशन के नाम के हिसाब से फ़िल्टर करें:

import com.google.home.automation.Automation
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure

...

val structure = homeManager.structures().list().single()
val automation: DraftAutomation = structure.automations().filter {
  it.name.equals("Sunset Blinds") }

किसी डिवाइस के लिए सभी ऑटोमेशन पाना

किसी दिए गए डिवाइस के रेफ़रंस वाले सभी ऑटोमेशन पाने के लिए, हर ऑटोमेशन के automationGraph को स्कैन करने के लिए, नेस्टेड फ़िल्टरिंग का इस्तेमाल करें:

import android.util.Log
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure
import com.google.home.automation.Action
import com.google.home.automation.Automation
import com.google.home.automation.Automation.automationGraph
import com.google.home.automation.Node
import com.google.home.automation.ParallelFlow
import com.google.home.automation.SelectFlow
import com.google.home.automation.SequentialFlow
import com.google.home.automation.Starter
import com.google.home.automation.StateReader

...

fun collectDescendants(node: Node): List<Node> {
  val d: MutableList<Node> = mutableListOf(node)

  val children: List<Node> =
    when (node) {
      is SequentialFlow -> node.nodes
      is ParallelFlow -> node.nodes
      is SelectFlow -> node.nodes
      else -> emptyList()
    }
  for (c in children) {
    d += collectDescendants(c)
  }
  return d
}

val myDeviceId = "device@452f78ce8-0143-84a-7e32-1d99ab54c83a"
val structure = homeManager.structures().list().single()
val automations =
  structure.automations().first().filter {
    automation: Automation ->
    collectDescendants(automation.automationGraph!!).any { node: Node ->
      when (node) {
        is Starter -> node.entity.id.id == myDeviceId
        is StateReader -> node.entity.id.id == myDeviceId
        is Action -> node.entity.id.id == myDeviceId
        else -> false
      }
    }
  }

ऑटोमेशन अपडेट करना

किसी ऑटोमेशन का मेटाडेटा अपडेट करने के लिए, उसके update() तरीके को कॉल करें. साथ ही, इसे लैम्डा एक्सप्रेशन पास करें, जो मेटाडेटा सेट करता है:

import com.google.home.automation.Automation
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure

...

val structure = homeManager.structures().list().single()
val automation: DraftAutomation = structure.automations().mapNotNull {
  it.firstOrNull
    { automation -> automation.id == Id("automation-id") }
  }.firstOrNull()
automation.update { this.name = "Flashing lights 2" }

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

import com.google.home.automation.Automation
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure

...

val structure = homeManager.structures().list().single()
val automation: Automation = structure.automations().mapNotNull {
  it.firstOrNull
    { automation -> automation.id == Id("automation-id") }
  }.firstOrNull()
automation.update {
  this.automationGraph = sequential {
    val laundryWasherCompletionEvent =
      starter<_>(laundryWasher, LaundryWasherDevice, OperationCompletionEvent)
    condition {
      expression =
        laundryWasherCompletionEvent.completionErrorCode equals
          // UByte 0x00u means NoError
          0x00u
    }
    action(speaker, SpeakerDevice) { command(AssistantBroadcast.broadcast("laundry is done")) }
    }
  }
}

ऑटोमेशन मिटाना

ऑटोमेशन मिटाने के लिए, स्ट्रक्चर के deleteAutomation() तरीके का इस्तेमाल करें. ऑटोमेशन को उसके आईडी का इस्तेमाल करके मिटाया जाना चाहिए.

import com.google.home.automation.Automation
import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.Structure

...

val structure = homeManager.structures().list().single()
val automation: DraftAutomation = structure.automations().first()
structure.deleteAutomation(automation.id)

अगर मिटाने की प्रोसेस पूरी नहीं होती है, तो HomeException दिख सकता है. गड़बड़ी को ठीक करना देखें.

डिवाइस मिटाने पर, ऑटोमेशन पर पड़ने वाला असर

अगर कोई उपयोगकर्ता, किसी ऑटोमेशन में इस्तेमाल किया गया डिवाइस मिटाता है, तो मिटाया गया डिवाइस, किसी भी स्टार्टर को ट्रिगर नहीं कर सकता. साथ ही, ऑटोमेशन, उससे एट्रिब्यूट नहीं पढ़ पाएगा या उसे कमांड नहीं दे पाएगा. उदाहरण के लिए, अगर कोई उपयोगकर्ता अपने होम से OccupancySensorDevice मिटाता है और किसी ऑटोमेशन में ऐसा स्टार्टर है जो the OccupancySensorDeviceपर निर्भर करता है, तो वह स्टार्टर अब ऑटोमेशन को चालू नहीं कर पाएगा.