مثال على الإجراءات المبرمَجة على أجهزة iOS

لعرض مجموعة من المهام المختلفة التي يمكن أتمتتها باستخدام واجهة برمجة التطبيقات Automation API وربما لإلهام المزيد من الأفكار، إليك بعض الأمثلة على عمليات التشغيل الآلي.

تشغيل جهاز عند تشغيل جهاز آخر أو إيقافه

import GoogleHomeSDK
import GoogleHomeTypes

typealias OnOffTrait = Matter.OnOffTrait

...

let starterNode = starter(device1, OnOffLightDeviceType.self, OnOffTrait.self)
let device2State = stateReader(device2, OnOffLightDeviceType.self, OnOffTrait.self

automation (
  name: "Turn on a device",
  description:
    """
    Turn on a device when another device is turned on or off
    """
) {
 starterNode
 device2State
 condition {
   // Only send the command if device2 is off
   device2State.onOff.equals(false)
 }
 // Turn on device 2
 action(device2, OnOffLightDeviceType.self) {
   OnOffTrait.on()
 }
}

إغلاق الستائر الخارجية تلقائيًا عندما تنخفض درجة الحرارة الخارجية إلى أقل من 15.5 درجة مئوية ويكون الجو مظلمًا

import GoogleHomeSDK
import GoogleHomeTypes

typealias TemperatureMeasurementTrait = Matter.TemperatureMeasurementTrait
typealias TimeTrait = Google.TimeTrait
typealias WindowCoveringTrait = Matter.WindowCoveringTrait

let temperatureMeasurement = stateReader(
  thermostatDevice,
  TemperatureSensorDeviceType.self,
  TemperatureMeasurementTrait.self
)
let time = stateReader(structure, TimeTrait.self)

automation (
  name: "Close window blinds",
  description:
    """
    Automatically close window blinds when the temperature outside drops below 60F and it's dark
    outside.
    """
) {
  select {
    starter(
      eveThermostat,
      TemperatureSensorDeviceType.self,
      TemperatureMeasurementTrait.self
    )
    starter(structure, TimeTrait.ScheduledEvent.self) {
      TimeTrait.ScheduledEvent.solarTime(SolarTime(type: .sunrise, offset: .seconds(0)))
    }
    starter(structure, TimeTrait.ScheduledEvent.self) {
      TimeTrait.ScheduledEvent.solarTime(SolarTime(type: .sunset, offset: .seconds(0)))
    }
  }
  temperatureMeasurement
  time
  let exp1 = temperatureMeasurement.measuredValue.lessThan(1555)  // 15 degrees C ~ 60 degrees F
  let exp2 =
    localTimeBetweenCondition != nil
    ? time.currentTime.between(localTimeBetweenCondition!.0, localTimeBetweenCondition!.1)
    : time.currentTime.between(time.sunriseTime, time.sunsetTime)
  condition {
    exp1.and(exp2.not())
  }
  parallel {
    for windowBlind in windowBlinds {
      action(windowBlind, WindowCoveringDeviceType.self) {
        WindowCoveringTrait.downOrClose()
      }
    }
  }
}

إذا ظل باب المرآب مفتوحًا لأكثر من 10 دقائق، يتم إرسال إشعار إلى هواتف أفراد المنزل وبث تذكير بإغلاق باب المرآب.

import GoogleHomeSDK
import GoogleHomeTypes

typealias BooleanStateTrait = Matter.BooleanStateTrait

automation (
  name: "Broadcast reminder"
  description:
    """
    If garage door left open for more than 10 minutes,
    send a notification to household members phones
    and broadcast a reminder to close the garage door
    """
  )
{
    let garageSensorStarter = starter(
      garageDoorSensor,
      ContactSensorDeviceType.self,
      BooleanStateTrait.self
    )
    garageSensorStarter
    condition(
      for: .seconds(10 * 60)  // 10 minutes.
    ) {
      // stateValue - True means garage door is closed, False means open
      garageSensorStarter.stateValue.equals(false)
    }
    action(structure) {
      Google.AssistantBroadcastTrait.broadcast(msg: "Garage is open")
    }
    action(structure) {
      Google.NotificationTrait.sendNotifications(
        title: "Garage is open", body: nil, optInMemberEmailsArray: [])
    }
  }
}

عند فتح باب المرآب، يتم تشغيل مصابيح الشرفة والممر

import GoogleHomeSDK
import GoogleHomeTypes

typealias OnOffTrait = Matter.OnOffTrait
typealias BooleanStateTrait = Matter.BooleanStateTrait

automation (
  name: "Turn on lights"
  description:
    """
    When the garage door opens, turn on porch and driveway lights
    """
) {
  let garageSensorStarter = starter(
    garageDoorSensor,
    ContactSensorDeviceType.self,
    BooleanStateTrait.self
  )
  garageSensorStarter
  condition {
    // stateValue - True means garage door is closed, False means open
    garageSensorStarter.stateValue.equals(false)
  }
  parallel {
    for light in drivewayLights {
      action(light, OnOffLightDeviceType.self) {
        OnOffTrait.on()
      }
    }
    for light in porchLights {
      action(light, OnOffLightDeviceType.self) {
        OnOffTrait.on()
      }
    }
  }
}

إذا كان التلفزيون مفعّلاً عند اكتمال دورة الفرن، تومض مصابيح غرفة المعيشة ويُعلن مكبّر الصوت الذكي عن اكتمال دورة الفرن.

import GoogleHomeSDK
import GoogleHomeTypes

typealias OnOffTrait = Matter.OnOffTrait
typealias OperationalStateTrait = Matter.OperationalStateTrait

automation (
  name: "Broadcast oven cycle complete"
  description:
    """
    If TV is on when oven cycle completes,
    living room lights blink and smart speaker broadcasts \"Oven Cycle Complete\"
    """
) {
  let ovenCompletedEvent = starter(
    oven,
    OvenDeviceType.self,
    OvenCavityOperationalStateTrait.OperationCompletionEvent.self
  )
  ovenCompletedEvent
  condition {
    ovenCompletedEvent
      .completionErrorCode
      .equals(0x00)  // no error
  }

  let operationalState = stateReader(
    oven,
    OvenDeviceType.self,
    OvenCavityOperationalStateTrait.self
  )
  operationalState
  condition {
    operationalState.phaseList[operationalState.currentPhase.toUInt()].equals("pre-heated")
  }

  let tvOnOff = stateReader(
    tv,
    GoogleTVDeviceType.self,
    OnOffTrait.self
  )
  tvOnOff
  condition {
    tvOnOff.onOff.equals(true)
  }

  parallel {
    for speaker in speakers {
      action(speaker, SpeakerDeviceType.self) {
        Google.AssistantBroadcastTrait.broadcast(msg: "Oven Cycle Complete")
      }
    }
    for light in lights {
      action(light, OnOffLightDeviceType.self) {
        OnOffTrait.toggle()
      }
    }
  }
  delay(for: .seconds(30))
  parallel {
    for light in lights {
      action(light, OnOffLightDeviceType.self) {
        OnOffTrait.toggle()
      }
    }
  }
}

في الساعة 9 مساءً، إذا كان هناك شخص في المنزل، يتم إطفاء التلفزيون، وتنخفض إضاءة مصابيح غرفة نوم الأطفال، ويتم ضبط درجة حرارة جهاز التكييف على درجة معيّنة.

import GoogleHomeSDK
import GoogleHomeTypes
typealias OnOffTrait = Matter.OnOffTrait
typealias ThermostatTrait = Matter.ThermostatTrait

automation(
  name: "Turn AC on",
  description:
    """
    At 9pm, if someone is home, TV turns off, kids' bedroom lights dim,
    GE AC turns to [X#] degrees
    """
) {
  let timeStarter = starter(
    structure,
    Google.TimeTrait.ScheduledEvent.self
  ) {
    Google.TimeTrait.ScheduledEvent.clockTime(scheduledTime)
  }

  let stateReaderNode = stateReader(
    structure,
    Google.AreaPresenceStateTrait.self
  )
  timeStarter
  stateReaderNode
  condition {
    stateReaderNode
      .presenceState
      .equals(Google.AreaPresenceStateTrait.PresenceState.presenceStateOccupied)
  }
  parallel {
    action(tv, GoogleTVDeviceType.self) {
      OnOffTrait.off()
    }

    for light in kidBedroomLights {
      action(light, DimmableLightDeviceType.self) {
        LevelControlTrait.moveToLevel(
          level: dimmedLightLevel,
          transitionTime: 10,
          optionsMask: LevelControlTrait.OptionsBitmap(),
          optionsOverride: LevelControlTrait.OptionsBitmap()
        )
      }
    }
    let fahrenheit = Measurement(value: acTempInFahrenheit, unit: UnitTemperature.fahrenheit)
    let setpoint = Int16(fahrenheit.converted(to: .celsius).value)
    action(geAc, RoomAirConditionerDeviceType.self) {
      update(ThermostatTrait.self) {
        $0.setOccupiedCoolingSetpoint(setpoint * 100)
      }
    }
  }
}

الإعلان عن "انتهت عملية الغسيل" على مكبّرات صوت غرفة النوم عند اكتمال دورة الغسيل في الغسّالة

import GoogleHomeSDK
import GoogleHomeTypes

typealias OperationalStateTrait = Matter.OperationalStateTrait

automation(
  name: "Broadcast laundry is done",
  description:
    """
    Announce \"laundry is done\" on bedroom speakers when the laundry machine cycle completes
    """
) {
  let laundryWasherCompletionEvent = starter(
    laundryWasher,
    LaundryWasherDeviceType.self,
    OperationalStateTrait.OperationCompletionEvent.self
  )
  laundryWasherCompletionEvent
  condition {
    laundryWasherCompletionEvent
      .completionErrorCode
      .equals(0x00)
  }

  action(bedroomSpeaker, SpeakerDeviceType.self) {
    Google.AssistantBroadcastTrait.broadcast(msg: "laundry is done")
  }
}

عند الوصول إلى المنزل: إذا تم فتح قفل Aqara بعد غروب الشمس، يتم تشغيل مصباح السقف وتعديل إعدادات الترموستات 3P إلى 26 درجة مئوية وإعادة تشغيل المقابس الذكية وتشغيل التلفزيون وجهاز صنع القهوة. يقول مكبّر الصوت الذكي "مرحبًا بك في المنزل".

import GoogleHomeSDK
import GoogleHomeTypes

typealias DoorLockTrait = Matter.DoorLockTrait
typealias ThermostatTrait = Matter.ThermostatTrait

automation(
  name: "Door unlock turn on appliances",
  description:
    """
    Home Arrival: If Aqara lock is unlocked and it is after sunset,
    ceiling light turns on,
    update the 3P thermostat setting to 26 degress, smart plugs restart power, TV and coffeemaker
    turn on. Smart speaker says \"Welcome Home!\"
     """
) {
  let doorLockEvent = starter(
    doorLock, DoorLockDeviceType.self,
    DoorLockTrait.LockOperationEvent.self
  )
  let doorIsUnlocked = doorLockEvent.lockOperationType.equals(.unlock)

  let timeCondition =
    localTimeBetweenCondition != nil
    ? time.currentTime.between(localTimeBetweenCondition!.0, localTimeBetweenCondition!.1)
    : time.currentTime.between(time.sunsetTime, time.sunriseTime)

  time
  doorLockEvent
  condition {
    doorIsUnlocked.and(timeCondition)
  }

  parallel {
    for light in allLights {
      action(light, DimmableLightDeviceType.self) {
        OnOffTrait.on()
      }
    }

    action(thermostat, ThermostatDeviceType.self) {
      update(ThermostatTrait.self) {
        $0.setOccupiedCoolingSetpoint(2600)
      }
    }
    action(plug, OnOffPluginUnitDeviceType.self) {
      OnOffTrait.on()
    }
    action(tv, GoogleTVDeviceType.self) {
      OnOffTrait.on()
    }
    action(coffeemaker, CooktopDeviceType.self) {
      OnOffTrait.on()
    }
    action(structure) {
      Google.AssistantBroadcastTrait.broadcast(msg: "Welcome Home!")
    }
  }
}

يمكن للمستخدم إنشاء روتين "وضع الهواء النقي" الذي يشغّل مروحة السقف والمكنسة الكهربائية.

import GoogleHomeSDK
import GoogleHomeTypes

typealias FanControlTrait = Matter.FanControlTrait

automation(
  name: "Fan and vaccum turn on",
  description:
    """
    User can create "Fresh Air Mode" routine which starts the ceiling fan,
    and vacuum
    """
) {
  manualStarter()

  parallel {
    action(fan, FanDeviceType.self) {
      update(FanControlTrait.self) {
        $0.setFanMode(.on)
      }
    }
    action(vacuum, RoboticVacuumCleanerDeviceType.self) {

      // 0 - idle, 1 - cleaning, 2 - mapping
      RvcRunModeTrait.changeToMode(newMode: 1)
    }
  }
}

عندما يشغّل المستخدم مصابيح غرفة النوم بعد الساعة 9 مساءً، يتم ضبط مصباح غرفة النوم على لون دافئ للاسترخاء، ويتم إغلاق الستائر الكهربائية الثلاثية المراحل، ويتم ضبط مكيّف الهواء المجزأ الصغير لتقليل درجة الحرارة بمقدار درجةَين لتبريد الغرفة. يشغِّل Google Nest Audio "أصوات المطر".

import GoogleHomeSDK
import GoogleHomeTypes

typealias OnOffTrait = Matter.OnOffTrait
typealias WindowCoveringTrait = Matter.WindowCoveringTrait

automation(
  name: "Bulbs warm color after 9pm",
  description:
    """
    When user turns on bedroom lights, and the time is after 9p, bedroom bulb adjusts
    to warm color to relax, 3P shades close,mini-split air conditioner adjusts
    down 2 degrees to cool the room. Google Nest Audio plays 'rain sounds'.
    """
) {
  select {
    for light in bedroomLights {
      let lightOnOffState = starter(
        light,
        OnOffLightDeviceType.self,
        OnOffTrait.self
      )
      lightOnOffState
      condition { lightOnOffState.onOff.equals(true) }
    }
  }
  let time = stateReader(structure, Google.TimeTrait.self)
  time
  condition {
    time.currentTime.between(
      betweenConditionTime.0,
      betweenConditionTime.1
    )
  }
  parallel {
    for light in colorControlBedroomLights {
      action(light, ColorTemperatureLightDeviceType.self) {
        ColorControlTrait.moveToColorTemperature(
          colorTemperatureMireds: 200,
          transitionTime: 10,
          optionsMask: 0,
          optionsOverride: 0
        )
      }
    }
    action(windowBlind, WindowCoveringDeviceType.self) {
      WindowCoveringTrait.downOrClose()
    }
    action(thermostatDevice, RoomAirConditionerDeviceType.self) {
      ThermostatTrait.setpointRaiseLower(
        mode: .cool,
        amount: 2
      )
    }
    action(speaker, SpeakerDeviceType.self) {
      Google.AssistantFulfillmentTrait.okGoogle(query: "Play rain sound.")
    }
  }
}

يمكنك تشغيل المصابيح أو إيقاف وضع "درجات حرارة اقتصادية" في الترموستات عند فتح قفل الباب.

import GoogleHomeSDK
import GoogleHomeTypes

typealias DoorLockTrait = Matter.DoorLockTrait
typealias OnOffTrait = Matter.OnOffTrait
typealias ThermostatTrait = Matter.ThermostatTrait

 automation(
  name: "Unlock door, turn on lights or turn off thermostat",
  description:
    """
    Turn on lights or turn off thermostat eco mode when you unlock the door.
    """
) {
  let doorLockEvent = starter(
    doorLock,
    DoorLockDeviceType.self,
    DoorLockTrait.LockOperationEvent.self
  )
  doorLockEvent
  condition {
    doorLockEvent.lockOperationType.equals(.unlock)
  }
  parallel {
    for light in allLights {
      action(light, OnOffLightDeviceType.self) {
        OnOffTrait.on()
      }
      // Assume the thermostat is in eco mode, set to auto.
      action(thermostat, ThermostatDeviceType.self) {
        Google.SimplifiedThermostatTrait.setSystemMode(systemMode: .auto)
       }
    }
  }
}

في الساعة 9 مساءً، عليك قفل الباب.

import GoogleHomeSDK
import GoogleHomeTypes

typealias DoorLockTrait = Matter.DoorLockTrait

automation(
  name: "Lock the door",
  description: "At 9pm lock the door."
) {
  starter(structure, Google.TimeTrait.ScheduledEvent.self) {
    Google.TimeTrait.ScheduledEvent.clockTime(startTime)
  }
  action(doorLock, DoorLockDeviceType.self) {
    DoorLockTrait.lockDoor()
  }
}

إطفاء الأضواء عندما يكون الجميع خارج المنزل

import GoogleHomeSDK
import GoogleHomeTypes

typealias OnOffTrait = Matter.OnOffTrait

automation(
  name: "Turn off light",
  description: "When everyone is away from home, turn off lights."
) {
  let homeAwayState = starter(structure, Google.AreaPresenceStateTrait.self)
  homeAwayState
  condition {
    homeAwayState.presenceState.equals(.presenceStateVacant)
  }

  parallel {
    for light in allLights {
      action(light, OnOffLightDeviceType.self) {
        OnOffTrait.off()
      }
    }
  }
}

إذا تم رصد حركة (من خلال جهاز الاستشعار)، يتم بث صوت المنبّه على مكبّرات الصوت الذكية، وتشغيل المصابيح وإطفاؤها بشكل دوري، وتشغيل التلفزيون لردع المتسلّلين.

import GoogleHomeSDK
import GoogleHomeTypes

typealias OccupancySensingTrait = Matter.OccupancySensingTrait
typealias OnOffTrait = Matter.OnOffTrait

automation(
  name: "Motion sensed, turn lights on and off to deter intruders",
  description:
    """
    If motion (sensor) is detected, broadcast alarm sound on smart speakers,
    turn lights on and off periodically and TV to deter intruders.
    """
) {
  let motionStarter =
    starter(motionSensor, OccupancySensorDeviceType.self, OccupancySensingTrait.self)
  let homeAwayState = stateReader(structure, Google.AreaPresenceStateTrait.self)
  motionStarter
  homeAwayState

  let exp1 = homeAwayState.presenceState.equals(.presenceStateVacant)
  let exp2 = motionStarter.occupancy.equals(.occupied)

  condition {
    exp1.and(exp2)
  }

  action(structure) {
    Google.AssistantBroadcastTrait.broadcast(msg: "ALARM! ALARM! ALARM!")
  }

  turnOnOff(lights: allLights, tv: tv, shouldTurnOn: true)
  delay(for: Duration.seconds(5))
  turnOnOff(lights: allLights, tv: tv, shouldTurnOn: false)
  delay(for: Duration.seconds(5))
  turnOnOff(lights: allLights, tv: tv, shouldTurnOn: true)
}

private func turnOnOff(
  lights: Set<HomeDevice>,
  tv: HomeDevice,
  shouldTurnOn: Bool
) -> ParallelFlow {
  parallel {
    for light in lights {
      action(light, OnOffLightDeviceType.self) {
        shouldTurnOn ? OnOffTrait.on() : OnOffTrait.off()
      }
    }
    action(tv, GoogleTVDeviceType.self) {
      shouldTurnOn ? OnOffTrait.on() : OnOffTrait.off()
    }
  }
}

مصباح في غرفة النوم إذا كان الوقت بعد الساعة 10 مساءً وأطفأ المستخدم المصباح، يتم إطفاء جميع مصابيح الغرفة وإغلاق الستائر وقفل الباب الأمامي (قفل غير مادي).

import GoogleHomeSDK
import GoogleHomeTypes

typealias WindowCoveringTrait = Matter.WindowCoveringTrait
typealias OnOffTrait = Matter.OnOffTrait
typealias DoorLockTrait = Matter.DoorLockTrait

automation(
  name: "After 10pm, turn all lights off, close shades, lock front door",
  description:
    """
    User has Lamp in their bedroom. If it is after 10pm and the user turns off the lamp,
    then all room lights turn off, shades close, front door locks (non-matter lock).
    """
) {
  let lampOnOffState = starter(Lamp, DimmableLightDeviceType.self, OnOffTrait.self)
  let time = stateReader(structure, Google.TimeTrait.self)
  lampOnOffState
  time
  let expr1 = lampOnOffState.onOff.equals(false)
  let expr2 = time.currentTime.between(
    localTimeBetweenCondition.0,
    localTimeBetweenCondition.1
  )

  condition {
    expr1.and(expr2)
  }

  parallel {
    for light in allLights {
      action(light, DimmableLightDeviceType.self) {
        OnOffTrait.off()
      }
    }
    action(windowBlind, WindowCoveringDeviceType.self) {
      WindowCoveringTrait.downOrClose()
    }
    action(doorLock, DoorLockDeviceType.self) {
      DoorLockTrait.lockDoor()
    }
  }
}

إذا قال المستخدم "Ok Google، لا يمكنني النوم"، يتم تشغيل أصوات أمواج المحيط، وتفعيل المروحة، وتفعيل "مقبس الطاقة الداخلي"، وخفض الستائر.

import GoogleHomeSDK
import GoogleHomeTypes

typealias FanControlTrait = Matter.FanControlTrait
typealias OnOffTrait = Matter.OnOffTrait
typealias WindowCoveringTrait = Matter.WindowCoveringTrait

automation(
  name: "Turn sleep sound, fan, indoor plug on",
  description:
    """
    If user says "Hey Google, I can't sleep",
    play ocean wave sounds, turn on the Fan,
    Indoor Plug turns on, and 3P shades roll down
    """
) {
  starter(structure, Google.VoiceStarterTrait.OkGoogleEvent.self) {
    Google.VoiceStarterTrait.OkGoogleEvent.query("I can't sleep")
  }
  parallel {
    action(speaker, SpeakerDeviceType.self) {
      Google.AssistantFulfillmentTrait.okGoogle(query: "Play ocean wave sounds")
    }
    action(fan, FanDeviceType.self) {
      update(FanControlTrait.self) {
        $0.setFanMode(.on)
      }
    }
    action(plug, OnOffPluginUnitDeviceType.self) {
      OnOffTrait.on()
    }
    action(shades, WindowCoveringDeviceType.self) {
      WindowCoveringTrait.downOrClose()
    }
  }
}

إذا رصدت أدوات الاستشعار أنّ غرفة المعيشة فارغة، يتم إيقاف المروحة وضبط مكيّف الهواء على الوضع "الاقتصادي" وتشغيل المكنسة الكهربائية الروبوتية (مرة واحدة بحد أقصى في اليوم).

import GoogleHomeSDK
import GoogleHomeTypes

typealias OccupancySensingTrait = Matter.OccupancySensingTrait
typealias FanControlTrait = Matter.FanControlTrait

{

  let time = stateReader(structure, Google.TimeTrait.self)
  let timeCondition: TypedExpression<Bool>

  if let localTimeBetweenCondition {
    timeCondition = time.currentTime.between(
      localTimeBetweenCondition.0,
      localTimeBetweenCondition.1
    )
  } else {
    timeCondition = time.currentTime.between(time.sunriseTime, time.sunsetTime)
  }
  return automation(
    name: "No motion detected, turn off fan and turn on vacuum",
    description:
      """
      If presence/occupancy sensor detects that the living room is empty, fan turns off,
      AC goes into Eco mode and robot vacuum starts (max once per day)
      """
  ) {


{
    let occupancySensorStarter = starter(
      occupancySensor,
      OccupancySensorDeviceType.self,
      OccupancySensingTrait.self
    )

    occupancySensorStarter
    time
    condition {
      occupancySensorStarter.occupancy.notEquals(.occupied)
        .and(timeCondition)
    }
     suppress(for: .seconds(43200))  // 12 hours
    parallel {
      action(fan, FanDeviceType.self) {
        update(FanControlTrait.self) {
          $0.setFanMode(.off)
        }
      }
      action(thermostat, ThermostatDeviceType.self) {
        Google.SimplifiedThermostatTrait.setSystemMode(systemMode: .eco)
      }
      action(vacuum, RoboticVacuumCleanerDeviceType.self) {
        RvcRunModeTrait.changeToMode(newMode: 1)
      }
    }
  }
}

إذا كان الجميع خارج المنزل وفتح أحد أفراد الأسرة بوابة أو نافذة، سيتم بث رسالة "إنذار، إنذار، إنذار" على جميع مكبّرات الصوت أو الشاشات الذكية المتصلة بتطبيق Google Home.

import GoogleHomeSDK
import GoogleHomeTypes

typealias DoorLockTrait = Matter.DoorLockTrait
typealias BooleanStateTrait = Matter.BooleanStateTrait

automation(
  name: "Broadcast intruder alert when door is open if no one is home",
  description:
    """
    If everyone is away and the door or window sensor opens, all Google Home-connected
    smart speaker or displays broadcast 'Intruder Alert, Intruder Alert, Intruder Alert'
    """
) {
  sequential {
    let areaPresenceState = starter(structure, Google.AreaPresenceStateTrait.self)
    areaPresenceState
    condition {
      areaPresenceState.presenceState.equals(.presenceStateVacant)
    }
    let doorLockState = stateReader(
      doorLock,
      DoorLockDeviceType.self,
      DoorLockTrait.self
    )
    // In a Contact Sensor device type, FALSE=open or no contact, TRUE=closed or contact.
    let contactSensorState = stateReader(
      contactSensor,
      ContactSensorDeviceType.self,
      BooleanStateTrait.self
    )
    doorLockState
    contactSensorState
    condition {
      let exp1 = doorLockState.lockState.equals(.unlocked)
      let exp2 = contactSensorState.stateValue.equals(false)
      return exp1.or(exp2)
    }
    action(structure) {
      Google.AssistantBroadcastTrait.broadcast(
        msg: "Intruder Alert, Intruder Alert, Intruder Alert"
      )
    }
  }
}
}

عندما يشغّل المستخدمون الوسائط على جهاز Chromecast في المساء، يتم تفعيل ميزة "إقران الشاشة" وخفض الستائر الذكية (عمليات التشغيل الآلي)

import GoogleHomeSDK
import GoogleHomeTypes

typealias WindowCoveringTrait = Matter.WindowCoveringTrait

automation(
  name: "Lower blinds when media is played",
  description:
    """
    When users plays media on their Chromecast in the evening, Screen Mirror
    activates and smart blinds lower (Automations)
    """
) {
   select {
    sequential {
      playbackStarter
      time
      let isPlaying = playbackStarter.currentState.equals(.playing)
      condition {
        isPlaying.and(timeCondition)
      }
    }
    starter(structure, Google.VoiceStarterTrait.OkGoogleEvent.self) {
      Google.VoiceStarterTrait.OkGoogleEvent.query("resume my movie night")
    }
  }
  action(nanoleaf4dLight, OnOffLightDeviceType.self) {
    Google.ExtendedModeSelectTrait.changeModeSettings(
      updateModeSettings: [
        Google.ExtendedModeSelectTrait.ModeSetting(
          modeNameKey: "Light Mode",
          modeValueKey: "Screen Mirror"
        )
      ]
    )
  }

  parallel {
    for windowBlind in windowBlinds {
      action(windowBlind, WindowCoveringDeviceType.self) {
        WindowCoveringTrait.downOrClose()
      }
    }
  }
}

عندما يوقف المستخدم بث المحتوى على Chromecast مؤقتًا، يتم إيقاف ميزة "إضفاء مظهر شاشة تلفزيون" مؤقتًا وتبقى الستائر الذكية منخفضة. يمكن للمستخدم استئناف استخدام Chromecast باستخدام عبارة "Ok G" أو من خلال استئناف استخدام Chromecast يدويًا باستخدام جهاز تحكّم عن بُعد أو هاتفه.

import GoogleHomeSDK
import GoogleHomeTypes

typealias WindowCoveringTrait = Matter.WindowCoveringTrait

{
  let mediaPlayback = starter(tv, GoogleTVDeviceType.self, MediaPlaybackTrait.self)
  let time = stateReader(structure, Google.TimeTrait.self)
  let timeCondition: TypedExpression<Bool>
  if let localTimeBetweenCondition {
    timeCondition = time.currentTime.between(
      localTimeBetweenCondition.0,
      localTimeBetweenCondition.1
    )
  } else {
    timeCondition = time.currentTime.between(time.sunsetTime, time.sunriseTime)
  }
automation(
  name: "When Chromecast pauses, pause screen mirror",
  description:
    """
    When the user pauses Chromecast, Screen Mirror pauses and smart blinds stay lowered.
    The user can resume Chromecast using "Ok G" or by resuming Chromecast manually using
    a remote or their phone.
    """
) {
  mediaPlayback
  time
  let isPaused = mediaPlayback.currentState.equals(.paused)
  condition {
    isPaused.and(timeCondition)
  }

  action(nanoleaf4dLight, OnOffLightDeviceType.self) {
    Google.ExtendedModeSelectTrait.changeModeSettings(
      updateModeSettings: [
        Google.ExtendedModeSelectTrait.ModeSetting(
          modeNameKey: "Light Mode",
          modeValueKey: "Screen Mirror Pause"
        )
      ]
    )
  }

  parallel {
    for windowBlind in windowBlinds {
      action(windowBlind, WindowCoveringDeviceType.self) {
        WindowCoveringTrait.downOrClose()
        }
      }
    }
  }
}

عندما يوقف المستخدم تشغيل الوسائط على جهاز Chromecast أو ينهيه، يوقف الجهاز ميزة "نسخ الشاشة" ويعود إلى حالته الأصلية قبل بدء ميزة "نسخ الشاشة" (عمليات التشغيل الآلي)

import GoogleHomeSDK
import GoogleHomeTypes

typealias WindowCoveringTrait = Matter.WindowCoveringTrait

{
  let playbackState = starter(tv, GoogleTVDeviceType.self, MediaPlaybackTrait.self)
  let time = stateReader(structure, Google.TimeTrait.self)
  let timeCondition: TypedExpression<Bool>
  if let localTimeBetweenCondition {
    timeCondition = time.currentTime.between(
      localTimeBetweenCondition.0,
      localTimeBetweenCondition.1
    )
  } else {
    timeCondition = time.currentTime.between(time.sunsetTime, time.sunriseTime)
  }
automation(
  name: "When media stops, screen mirroring stops",
  description:
    """
    When the user stops/ends media play on the Chromecast, the device stops screen mirroring
    and goes back to the original state that it was on before screen mirror (Automations)
    """
) {
  playbackState
  time
  let isNotPlaying = playbackState.currentState.equals(.notPlaying)

  condition {
    isNotPlaying.and(timeCondition)
  }

  action(nanoleaf4dLight, OnOffLightDeviceType.self) {
    Google.ExtendedModeSelectTrait.changeModeSettings(
      updateModeSettings: [
        Google.ExtendedModeSelectTrait.ModeSetting()
      ]
    )
  }

  parallel {
    for windowBlind in windowBlinds {
      action(windowBlind, WindowCoveringDeviceType.self) {
        WindowCoveringTrait.upOrOpen()
        }
      }
    }
  }
}

إذا أوقف المستخدم التلفزيون الذكي في غرفة النوم وكان الوقت بين الساعة 10 مساءً و6 صباحًا، تتغيّر الأضواء إلى اللون الأبيض الدافئ وتبدأ في الخفوت ببطء بمرور الوقت، وينخفض مستوى الإضاءة في شريط الإضاءة واللوحات السداسية.

import GoogleHomeSDK
import GoogleHomeTypes

typealias OnOffTrait = Matter.OnOffTrait
typealias LevelControlTrait = Matter.LevelControlTrait
typealias ColorControlTrait = Matter.ColorControlTrait

{
  var matterLights = Set<HomeDevice>()
  var smartHomeLights = Set<HomeDevice>()

  for light in bedroomColorTemperatureLights {
    if let device = await light.types.get(ColorTemperatureLightDeviceType.self) {
      if !device.traits.contains(Google.LightEffectsTrait.self),
        device.traits.contains(Matter.LevelControlTrait.self),
        device.traits[Matter.LevelControlTrait.self]?.supportsMoveWithOnOffCommand ?? false
      {
        matterLights.insert(light)
      } else if device.traits.contains(Google.LightEffectsTrait.self),
        device.traits[Google.LightEffectsTrait.self]?.supportsSleepEffectSetCommand ?? false
      {
        smartHomeLights.insert(light)
      }
    }
  }
automation(
  structureID: structure.id,
  name: "Change lights when Smart TV is off",
  description:
    """
    If user turns bedroom Smart TV off and time is between 10pm - 6am,
    lights change to Warm White and begin to slowly fade off over time,
    light strip and hexagon panels dim.
    """
) {
{
  let tvOnOffState = starter(
    tv,
    GoogleTVDeviceType.self,
    OnOffTrait.self
  )
  let time = stateReader(
    structure,
    Google.TimeTrait.self
  )
  tvOnOffState
  time

  let tvIsOn = tvOnOffState.onOff.equals(false)
  let timeCondition = time.currentTime.between(
    localTimeBetweenCondition.0,
    localTimeBetweenCondition.1
  )

  condition {
    tvIsOn.and(timeCondition)
  }

  parallel {
    for light in matterLights {
      action(light, ColorTemperatureLightDeviceType.self) {
        LevelControlTrait.moveWithOnOff(
          moveMode: .down,
          rate: 1,
          optionsMask: LevelControlTrait.OptionsBitmap.coupleColorTempToLevel,
          optionsOverride: LevelControlTrait.OptionsBitmap.coupleColorTempToLevel
        )
      }
    }

    for light in smartHomeLights {
      action(light, ColorTemperatureLightDeviceType.self) {
        Google.LightEffectsTrait.sleepEffectSet(durationSeconds: 30)
      }
    }

    for lightStrip in bedroomLightStripsAndHexagonPanels {
      action(lightStrip, DimmableLightDeviceType.self) {
        LevelControlTrait.moveWithOnOff(
          moveMode: .down,
          rate: 10,
          optionsMask: LevelControlTrait.OptionsBitmap(),
          optionsOverride: LevelControlTrait.OptionsBitmap()
          )
        }
      }
    }
  }
}