ตัวอย่างการทำงานอัตโนมัติ

ต่อไปนี้คือตัวอย่างการทำงานอัตโนมัติบางส่วนที่แสดงงานต่างๆ ที่ดำเนินการโดยอัตโนมัติได้โดยใช้ Automation API และอาจช่วยสร้างแรงบันดาลใจให้คุณได้

เปิดอุปกรณ์เมื่ออุปกรณ์อื่นเปิดหรือปิด

import com.google.home.automation.automation
import com.google.home.automation.equals
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.onOff
import com.google.home.matter.standard.OnOffLightDevice

...

automation {
  sequential {
    val starterNode = starter<_>(device1, OnOffLightDevice, OnOff)
    val device2State = stateReader<_>(device2, OnOffLightDevice, OnOff)
    condition {
      // Only send the command if device2 is off
      expression = device2State.onOff equals false
    }
   // turn on device 2
   action(device2, OnOffLightDevice) { command(OnOff.on()) }
   }
}

ปิดมู่ลี่หากแสงสว่างจ้าเกินไป

import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.automation.and
import com.google.home.automation.automation
import com.google.home.automation.equals
import com.google.home.automation.greaterThan
import com.google.home.matter.standard.IlluminanceMeasurement
import com.google.home.matter.standard.IlluminanceMeasurement.Companion.measuredValue
import com.google.home.matter.standard.LightSensorDevice
import com.google.home.matter.standard.WindowCovering
import com.google.home.matter.standard.WindowCovering.Companion.currentPositionLift
import com.google.home.matter.standard.WindowCoveringDevice

...

automation {
  sequential {
    val starterNode = starter<_>(device1, LightSensorDevice, IlluminanceMeasurement)
    val blindsPosition = stateReader<_>(blinds, WindowCoveringDevice, WindowCovering)

    condition {
      // only send the command if
      // the blinds are open
      (blindsPosition.currentPositionLift greaterThan 0u) and
        // it's brighter than a certain threshold value
        (starterNode.measuredValue greaterThan 10u)
    }
    // close the blind
    action(blinds, WindowCoveringDevice) { command(WindowCovering.downOrClose()) }
  }
}

เปิดพัดลมเมื่ออุณหภูมิสูงกว่าเกณฑ์

import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
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.And
import com.google.home.automation.Automation
import com.google.home.automation.Condition
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.automation.StateReader
import com.google.home.matter.standard.FanDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffTrait.Attributes.onOff
import com.google.home.matter.standard.TemperatureMeasurement
import com.google.home.matter.standard.TemperatureMeasurement.measuredValue
import com.google.home.matter.standard.TemperatureSensorDevice

...

automation {
  sequential {
    val starterNode = starter<_>(thermometer, TemperatureSensorDevice, TemperatureMeasurement)

    val fanStateReaderNode = stateReader<_>(fan, FanDevice, OnOff)
    condition {
      // only send the command if the temperature is above 78F
      val expr1 = starterNode.measuredValue greaterThan 78
      // the fan is off
      val expr2 = fanStateReaderNode.onOff equals false
      expression = expr1 and expr2
    }
    // turn on the fan
    action(fan, FanDevice) { command(OnOff.on()) }
  }
}

เปิดไฟและส่งเสียงแจ้งเตือนเมื่อตรวจพบการเคลื่อนไหว

import com.google.home.Home
import com.google.home.HomeDevice
import com.google.home.HomeManager
import com.google.home.Id
import com.google.home.automation.and
import com.google.home.automation.automation
import com.google.home.automation.condition
import com.google.home.automation.equals
import com.google.home.automation.greaterThan
import com.google.home.automation.starter
import com.google.home.automation.stateReader
import com.google.home.google.AssistantBroadcast
import com.google.home.google.MotionDetection
import com.google.home.google.MotionDetection.Companion.motionDetectionEventInProgress
import com.google.home.matter.standard.OccupancySensorDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.onOff
import com.google.home.matter.standard.OnOffLightDevice
import com.google.home.matter.standard.SpeakerDevice
import java.time.Duration

...

automation {
  sequential {
    val starterNode = starter<_>(sensor, OccupancySensorDevice, MotionDetection)
    // only proceed if there is currently motion taking place
    condition { starterNode.motionDetectionEventInProgress equals true }
    // ignore the starter for one minute after it was last triggered
    suppressFor(Duration.ofMinutes(1))
    // make three announcements three seconds apart
    action(speaker, SpeakerDevice) {
      command(AssistantBroadcast.broadcast("Intruder detected!"))
    }
    delayFor(Duration.ofSeconds(3))
    action(speaker, SpeakerDevice) {
      command(AssistantBroadcast.broadcast("Intruder detected!"))
    }
    delayFor(Duration.ofSeconds(3))
    action(speaker, SpeakerDevice) {
      command(AssistantBroadcast.broadcast("Intruder detected!"))
    }
    // flash lights every two seconds
    action(light, OnOffLightDevice) { command(OnOff.toggle()) }
    delayFor(Duration.ofSeconds(2))
    action(light, OnOffLightDevice) { command(OnOff.toggle()) }
    delayFor(Duration.ofSeconds(2))
    action(light, OnOffLightDevice) { command(OnOff.toggle()) }
    delayFor(Duration.ofSeconds(2))
    action(light, OnOffLightDevice) { command(OnOff.toggle()) }
    delayFor(Duration.ofSeconds(2))
    action(light, OnOffLightDevice) { command(OnOff.toggle()) }
    delayFor(Duration.ofSeconds(2))
    action(light, OnOffLightDevice) { command(OnOff.toggle()) }
  }
}

เปิดมู่ลี่ 15 นาทีก่อนพระอาทิตย์ขึ้น

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.and
import com.google.home.automation.automation
import com.google.home.automation.condition
import com.google.home.automation.equals
import com.google.home.automation.greaterThan
import com.google.home.automation.parallel
import com.google.home.automation.starter
import com.google.home.automation.stateReader
import com.google.home.google.Time
import com.google.home.google.Time.Companion.sunriseTime
import com.google.home.google.TimeTrait.SolarTimeStruct
import com.google.home.google.TimeTrait.SolarTimeType
import com.google.home.matter.standard.WindowCovering
import com.google.home.matter.standard.WindowCovering.Companion.upOrOpen
import com.google.home.matter.standard.WindowCoveringDevice
import java.time.Duration

...

automation {
  sequential {
    // 15 minutes before sunrise
    val unused =
      starter<_>(structure, Time.ScheduledTimeEvent) {
        parameter(Time.ScheduledTimeEvent.solarTime(
          SolarTimeStruct(SolarTimeType.Sunrise,
                          java.time.Duration.ofMinutes(-15))))
      }
    }
    sequential {
      manualStarter()
    }
    // simultaneously open the five blinds
    parallel {
        action(blind1, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
        action(blind2, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
        action(blind3, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
        action(blind4, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
        action(blind5, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
    }
}

เปิดมู่ลี่เวลา 07:00 น. ยกเว้นในกรณีที่แสงสว่างจ้าและ/หรืออากาศร้อนเกินไป

โปรดทราบว่าตัวอย่างนี้ใช้ Time.ScheduledTimeEvent ซึ่งกำหนดให้โครงสร้างได้รับการกำหนดที่อยู่โดยใช้ Google Home app (GHA) เปลี่ยนที่อยู่บ้านใน Google อธิบายวิธีที่ผู้ใช้ป้อนที่อยู่ของโครงสร้างได้

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.greaterThan
import com.google.home.automation.lessThanOrEquals
import com.google.home.automation.notEquals
import com.google.home.automation.or
import com.google.home.automation.parallel
import com.google.home.automation.select
import com.google.home.automation.sequential
import com.google.home.automation.size
import com.google.home.automation.starter
import com.google.home.automation.stateReader
import com.google.home.google.Time
import com.google.home.matter.standard.IlluminanceMeasurement
import com.google.home.matter.standard.IlluminanceMeasurement.Companion.measuredValue
import com.google.home.matter.standard.LightSensorDevice
import com.google.home.matter.standard.TemperatureMeasurement
import com.google.home.matter.standard.TemperatureMeasurement.Companion.measuredValue
import com.google.home.matter.standard.TemperatureSensorDevice
import com.google.home.matter.standard.WindowCovering
import com.google.home.matter.standard.WindowCovering.Companion.upOrOpen
import com.google.home.matter.standard.WindowCoveringDevice
import java.time.Duration
import java.time.LocalTime

...

automation {
  sequential {
    select {
      sequential {
        // run the automation daily at 7am
        val earlyMorning =
          starter<_>(structure, Time.ScheduledTimeEvent) {
            parameter(Time.ScheduledTimeEvent.clockTime(LocalTime.of(7, 0, 0, 0)))
          }
        val temperature = starter<_>(device, TemperatureSensorDevice, TemperatureMeasurement)
        val brightness = starter<_>(device1, LightSensorDevice, IlluminanceMeasurement)
        // don't run more often than every 15 minutes
        suppressFor(Duration.ofMinutes(15))
        condition {
          // temperature is below 32C/90F
          val expr1 = (temperature.measuredValue lessThanOrEquals 32)

          // too bright
          val expr2 = (brightness.measuredValue greaterThan 60u)

          expression = expr1 or expr2
        }
      }
      sequential { manualStarter() }
    }
    // simultaneously open the five blinds
    parallel {
      action(blind1, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
      action(blind2, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
      action(blind3, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
      action(blind4, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
      action(blind5, WindowCoveringDevice) { command(WindowCovering.upOrOpen()) }
    }
  }
}

หากไฟเปิดอยู่ ให้ปิดพัดลมแล้วเปลี่ยนการตั้งค่าความเร็ว

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.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.FanControl
import com.google.home.matter.standard.FanControl.Companion.setFanMode
import com.google.home.matter.standard.FanControl.Companion.setPercentSetting
import com.google.home.matter.standard.FanControlTrait
import com.google.home.matter.standard.FanControlTrait.FanModeEnum
import com.google.home.matter.standard.FanDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.onOff

...

automation {
  name = "Turn off the fan when the light is turned on."
  description = "If light is on, turn off the fan and change the speed setting."
  isActive = true
  sequential {
    // When the light's OnOff state changes...
    val starterNode = starter<_>(device, DimmableLightDevice, OnOff)
    // ...and if the light is turned on...
    condition() { expression = starterNode.onOff equals true }
    // ...turn off the fan and change the speed.
    action(device, FanDevice) {
      update(FanControl) {
        setFanMode(FanModeEnum.Off)
        setPercentSetting(20u)
      }
    }
  }
}

เปิดไฟหากมีคนอยู่ในพื้นที่

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.google.AreaPresenceState
import com.google.home.google.AreaPresenceState.Companion.presenceState
import com.google.home.google.AreaPresenceStateTrait.PresenceState
import com.google.home.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.OnOff

...

automation {
  description = "If the area is occupied, turn the light on."
  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
    }
    // ...turn the light on.
    action(device, DimmableLightDevice) { command(OnOff.on()) }
  }
}

ปิดไฟหากไม่มีคนอยู่บ้าน

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.and
import com.google.home.automation.automation
import com.google.home.automation.equals
import com.google.home.automation.stateReader
import com.google.home.google.AreaAttendanceState
import com.google.home.google.AreaAttendanceState.Companion.attendanceState
import com.google.home.google.AreaAttendanceStateTrait.AttendanceState
import com.google.home.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.onOff

...

automation {
  sequential {
    val starterNode = starter<_>(device, DimmableLightDevice, OnOff)
    val stateReaderNode = stateReader<_>(structure, AreaAttendanceState)

    condition() {
      val expr1 = starterNode.onOff equals true
      val expr2 =
        stateReaderNode.attendanceState equals AttendanceState.AttendanceStateNoHouseholdMembers
      // If the light is turned on and no one from the household is present
      expression = expr1 and expr2
    }
    // turn the light off
    action(device, DimmableLightDevice) { command(OnOff.off()) }
  }
}

ส่งการแจ้งเตือนในเวลาที่ระบุ

โปรดทราบว่าตัวอย่างนี้ใช้ Time.ScheduledTimeEvent ซึ่งกำหนดให้โครงสร้างได้รับการกำหนดที่อยู่โดยใช้ Google Home app (GHA) เปลี่ยนที่อยู่บ้านใน Google อธิบายวิธีที่ผู้ใช้ป้อนที่อยู่ของโครงสร้างได้

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.starter
import com.google.home.google.AreaAttendanceState
import com.google.home.google.Notification
import com.google.home.google.Time
import java.time.LocalTime

...

automation {
  sequential {
    val email1 = "your.email@gmail.com"
    // If the time is 1:01:01:001
    val unused =
      starter<_>(structure, Time.ScheduledTimeEvent) {
        parameter(Time.ScheduledTimeEvent.clockTime(LocalTime.of(1, 1, 1, 1)))
      }
    action(structure) {
      // send a notification to the email list
      command(Notification.sendNotifications("title", { body = "body" }))
    }
  }
}

ประกาศข้อความในเวลาที่ระบุหากไฟเปิดอยู่

โปรดทราบว่าตัวอย่างนี้ใช้ Time.ScheduledTimeEvent ซึ่งกำหนดให้โครงสร้างได้รับการกำหนดที่อยู่โดยใช้ Google Home app (GHA) เปลี่ยนที่อยู่บ้านใน Google อธิบายวิธีที่ผู้ใช้ป้อนที่อยู่ของโครงสร้างได้

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.starter
import com.google.home.google.AssistantBroadcast
import com.google.home.google.Time
import com.google.home.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.onOff
import java.time.LocalTime

...

automation {
  sequential {
    // If the time is 12:00...
    val unused =
      starter<_>(structure, Time.ScheduledTimeEvent) {
        parameter(Time.ScheduledTimeEvent.clockTime(LocalTime.of(12, 0, 0, 0)))
      }

    val stateReaderNode = stateReader<_>(device, DimmableLightDevice, OnOff)
    // ...and the light is on...
    condition() { expression = stateReaderNode.onOff equals true }
    // ...broadcast a message over any devices that support Google Assistant.
    action(structure) { command(AssistantBroadcast.broadcast("It's midday.")) }
  }
}

เปิดเพลงในเวลาที่ระบุเมื่อไฟเปิดอยู่

โปรดทราบว่าตัวอย่างนี้ใช้ Time.ScheduledTimeEvent ซึ่งกำหนดให้โครงสร้างได้รับการกำหนดที่อยู่โดยใช้ Google Home app (GHA) เปลี่ยนที่อยู่บ้านใน Google อธิบายวิธีที่ผู้ใช้ป้อนที่อยู่ของโครงสร้างได้

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.starter
import com.google.home.google.AssistantFulfillment
import com.google.home.google.Time
import com.google.home.matter.standard.DimmableLightDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.onOff
import com.google.home.matter.standard.SpeakerDevice
import java.time.LocalTime
...

automation {
  sequential {
    // When the time is 3:15pm...
    val unused =
      starter<_>(structure, Time.ScheduledTimeEvent) {
        parameter(Time.ScheduledTimeEvent.clockTime(LocalTime.of(15, 15, 0, 0)))
      }
    val stateReaderNode = stateReader<_>(device, DimmableLightDevice, OnOff)
    // ...and the light's OnOff state is on...
    condition() { expression = stateReaderNode.onOff equals true }
    // ...tell Google Assistant to play music
    action(device, SpeakerDevice) { command(AssistantFulfillment.okGoogle("Play some music")) }
  }
}

การทำงานอัตโนมัตินี้ต้องใช้อุปกรณ์เตาอบที่ใช้ลักษณะRunCycle แบบระบบคลาวด์ต่อระบบคลาวด์หรือคลัสเตอร์Matterสถานะการทํางานของช่องเตาอบ ดูข้อมูลเพิ่มเติมได้ในความสามารถในการทำงานร่วมกัน

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 com.google.home.matter.standard.OvenCavityOperationalState
import com.google.home.matter.standard.OvenCavityOperationalState.Companion.currentPhase
import com.google.home.matter.standard.OvenCavityOperationalState.Companion.phaseList
import com.google.home.matter.standard.OvenCavityOperationalStateTrait.OperationalStateStruct
import com.google.home.matter.standard.OvenDevice
import com.google.home.matter.standard.SpeakerDevice
import java.time.Duration

...

automation {
  name = "Oven is up-to-temperature"
  description =
    "When an oven has finished pre-heating, living room lights blink and smart speaker makes an announcement"
  isActive = true

  sequential {
    val starterNode =
      starter<_>(oven, OvenDevice, OvenCavityOperationalState /* Or OperationalState */)
    condition {
      expression = starterNode.phaseList[operationalState.currentPhase.toUInt()] equals "pre-heated"
    }
    action(speaker, SpeakerDevice) {
      command(AssistantBroadcast.broadcast("The oven has now reached the chosen temperature."))
    }
    // "blink" the light by toggling them twice with a five-second pause,
    // then return to the original state.
    action(light, OnOffLightDevice) { command(OnOff.toggle()) }
    delayFor(Duration.ofSeconds(5))
    action(light, OnOffLightDevice) { command(OnOff.toggle()) }
  }
}

ใช้ Google Assistant เพื่อสร้างเงื่อนไขสำหรับการนอนหลับ

ตัวอย่างนี้ทริกเกอร์โดย Google Assistant และใช้ Assistant ในการดําเนินการเพื่อเล่นเสียงคลื่นทะเล

คําสั่ง okGoogle เป็นคําสั่งระดับโครงสร้างและสามารถใช้เพื่อทําให้อุปกรณ์ใดก็ได้ในโครงสร้างทํางานอัตโนมัติ อย่างไรก็ตาม แอป Home APIs อาจไม่มีสิทธิ์เข้าถึงอุปกรณ์บางเครื่อง ดูสิทธิ์ OkGoogle เพื่อดูวิธีบังคับใช้สิทธิ์ในหลายๆ กรณี

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.AssistantFulfillment
import com.google.home.google.VoiceStarter
import com.google.home.matter.standard.FanControl
import com.google.home.matter.standard.FanControl.Companion.setFanMode
import com.google.home.matter.standard.FanControlTrait.FanModeEnum
import com.google.home.matter.standard.FanDevice
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.toggle
import com.google.home.matter.standard.OnOffPluginUnitDevice
import com.google.home.matter.standard.SpeakerDevice
import com.google.home.matter.standard.WindowCovering
import com.google.home.matter.standard.WindowCoveringDevice

automation {
  name = "Lull to sleep"
  description =
    "If user says \"Hey Google, I can't sleep\", play ocean wave sounds, turn on the fan, turn on the plug, and roll down the blinds"
  isActive = true
  sequential {
    val unused =
      starter<_>(structure, VoiceStarter.OkGoogleEvent) {
        parameter(VoiceStarter.OkGoogleEvent.query("I can't sleep"))
      }

    parallel {
      // tell the speaker to play ocean wave sounds
      action(speaker, SpeakerDevice) {
        command(AssistantFulfillment.okGoogle("Play ocean wave sounds"))
      }
      // turn on the fan
      action(fan, FanDevice) { update(FanControl) { setFanMode(FanModeEnum.On) } }
      // turn on the outlet
      action(plug, OnOffPluginUnitDevice) { command(OnOff.on()) }
      // close the blinds
      action(blinds, WindowCoveringDevice) { command(WindowCovering.downOrClose()) }
    }
  }
}

เปิดหรือปิด Chromecast เมื่อไฟเปิด

import com.google.home.HomeDevice
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.starter
import com.google.home.google.ExtendedApplicationLauncher
import com.google.home.google.ExtendedChannel
import com.google.home.google.ExtendedLevelControl
import com.google.home.google.ExtendedMediaInput
import com.google.home.google.ExtendedMediaPlayback
import com.google.home.google.GoogleTVDevice
import com.google.home.google.MediaActivityState
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.on
import com.google.home.matter.standard.OnOff.Companion.onOff
import com.google.home.matter.standard.OnOffLightDevice

automation {
  name = "Light Activated Chromecast"
  description = "When light turns on, turn on/off the Chromecast."
  isActive = true
  sequential {
    select {
      // Create a starter node for each light device selected
      sequential {
        val starterNode = starter<_>(lightDevice, OnOffLightDevice, OnOff)
        // ...and if the light is turned on...
        condition { expression = starterNode.onOff equals true }
      }
      manualStarter()
    }
    parallel {
      // ...turn the Chromecast on or off.
      action(chromecastDevice, GoogleTVDevice) { command(OnOff.on()) }
    }
  }
}

หยุด Chromecast และออกอากาศข้อความเมื่อไฟเปิด

import com.google.home.HomeDevice
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.starter
import com.google.home.google.AssistantBroadcast
import com.google.home.google.ExtendedApplicationLauncher
import com.google.home.google.ExtendedChannel
import com.google.home.google.ExtendedLevelControl
import com.google.home.google.ExtendedMediaInput
import com.google.home.google.ExtendedMediaPlayback
import com.google.home.google.GoogleTVDevice
import com.google.home.matter.standard.MediaPlayback
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.onOff
import com.google.home.matter.standard.OnOffLightDevice

automation {
  name = "Light Activated Chromecast Media Control"
  description = "When the light turns on, stop the media on Chromecast."
  isActive = true
  sequential {
    select {
      // Create a starter node for each light device selected
      sequential {
        val starterNode = starter<_>(lightDevice, OnOffLightDevice, OnOff)
        condition {
          // ...and the light is on...
          expression = starterNode.onOff equals true
        }
      }
      manualStarter()
    }
    val message = "Light turned on, stopping the media."
    parallel {
      // ...stop the media on the Chromecast...
      action(chromecastDevice, GoogleTVDevice) { command(MediaPlayback.stop()) }
      // ...broadcast message over any devices that support Google Assistant.
      action(speakerDevice, SpeakerDevice) { command(AssistantBroadcast.broadcast(message)) }
    }
  }
}

เปิดไฟเมื่อ Chromecast เปิด

import com.google.home.HomeDevice
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.starter
import com.google.home.google.GoogleTVDevice
import com.google.home.matter.standard.MediaPlayback
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOff.Companion.onOff
import com.google.home.matter.standard.OnOffLightDevice

automation {
  name = "Chromecast Activated Lights"
  description = "When chromecast turns on, turn on a set of lights."
  isActive = true
  sequential {
    select {
      // Create a starter node for each Chromecast device selected...
      sequential {
        val starterNode = starter<_>(chromecastDevice, GoogleTVDevice, OnOff)
        condition {
          // ...and when the Chromecast is turned on...
          expression = starterNode.onOff equals true
        }
      }
      manualStarter()
    }
    parallel {
      // ...turn the lights on.
      action(lightDevice, OnOffLightDevice) { command(OnOff.on()) }
    }
  }
}