Tự động hoá nhiều thiết bị trên Android

Một quy trình tự động hoá có thể tham chiếu đến các thiết bị nhiều phần tương tự như một quy trình tự động hoá không sử dụng thiết bị nhiều phần.

Trước tiên, hãy lấy(các) bộ phận của linh kiện như bình thường. Hãy xem phần Thiết bị nhiều phần để biết cách làm việc với thiết bị nhiều phần.

Sau đó, đối với mỗi bộ phận bạn muốn sử dụng trong quy trình tự động hoá, hãy tạo một AutomationPartPath, cho phép bạn tham chiếu bộ phận đó trong các điều kiện, hành động và trình khởi động quy trình tự động hoá.

// Obtain a reference to the device:
val multipartDevices = structure.devices(enableMultipartDevices = true).first().toList()

val lights = multipartDevices.filter { it.has(OnOffLightDevice) && it.has(OnOff) }

val light = lights.first()

// get the AutomationPartPath for the device
val lightPartPath = light.automationPart(light.part(OnOffLightDevice).first())

Một điều khác cần lưu ý là các hành động tự động hoá thường lấy thiết bị và loại thiết bị làm tham số. Tuy nhiên, một hành động tự động hoá tham chiếu đến một thiết bị thành phần (vẫn sử dụng AutomationPartPath) chỉ cần AutomationPartPath, vì AutomationPartPath đã chứa thông tin tham chiếu đến loại thiết bị ngoài thông tin tham chiếu đến thiết bị thành phần.

Ví dụ: trong Automation API, loại thiết bị Refrigerator có thể được coi là thiết bị nhiều phần. Nó bao gồm một RefrigeratorDevice gốc có thể chứa nhiều tủ phụ, chẳng hạn như tủ đông hoặc tủ tiêu chuẩn thuộc loại TemperatureControlledCabinetDevice.

Để xây dựng quy trình tự động hoá tủ lạnh, bạn chủ yếu tương tác với hai đặc điểm tiêu chuẩn của Matter:

  • RefrigeratorAlarm: Hiển thị cảnh báo trạng thái cửa thông qua thuộc tính doorOpen của trường state.
  • RefrigeratorAndTemperatureControlledCabinetMode: Cho phép chế độ đọc và ra lệnh. Ví dụ: chạy các lệnh như changeToMode để chuyển sang các chế độ như LowEnergy, RapidCool hoặc LowNoise.

Chương trình tự động hoá mẫu này sẽ kích hoạt khi cửa tủ lạnh mở. Nếu cửa mở quá 2 phút, quy trình tự động hoá sẽ phát thông báo bằng giọng nói trên loa thông minh, nhấp nháy đèn nhà bếp và gửi thông báo đẩy. Xin lưu ý rằng quy trình tự động hoá này chỉ tác động đến ngăn tủ lạnh của thiết bị, bỏ qua ngăn đông lạnh (nếu có):

import com.google.home.automation.action
import com.google.home.automation.automation
import com.google.home.automation.condition
import com.google.home.automation.equals
import com.google.home.automation.starter
import com.google.home.google.AssistantBroadcast
import com.google.home.google.Notification
import com.google.home.matter.standard.OnOff
import com.google.home.matter.standard.OnOffLightDevice
import com.google.home.matter.standard.RefrigeratorAlarm
import com.google.home.matter.standard.RefrigeratorAlarm.Companion.state
import com.google.home.matter.standard.RefrigeratorDevice
import com.google.home.matter.standard.SpeakerDevice
import java.time.Duration

val structure: Structure = home.structures().first()

// Fetch devices using the multipart device model.
var multipartDevices = homeManager.devices(enableMultipartDevices = true)

// Obtain a reference to the refrigerator device.
val refrigeratorDevice = multipartDevices.first {
  it.has(TemperatureControlledCabinetDevice) &&
  it.has(RefrigeratorAndTemperatureControlledCabinetMode) &&
  it.has(RefrigeratorAlarm)
}

// Get all temperature-controlled cabinet parts of the refrigerator
val cabinets = refrigeratorDevice.parts(TemperatureControlledCabinetDevice)

// Find the cabinet part with the 'refrigerator' semantic tag
val refrigeratorCabinet = cabinets.firstOrNull {
  it.metadata.tags.contains(SemanticTag.Refrigerator.refrigerator)
}

val cabinetPartPath = refrigeratorCabinet.automationPath(TemperatureControlledCabinetDevice)

val speaker = home.devices().list().first { device -> device.has(SpeakerDevice) }

val refrigeratorDoorAlert = automation {
  name = "Refrigerator Door Open Alert"
  description = "Warn when the refrigerator door has been open for over 2 min"
  isActive = true

  sequential {
    // 1. Starter: Monitor the refrigerator door alarm trait
    val alarmStarter = starter<_>(cabinetPartPath, RefrigeratorAlarm)

    // 2. Condition: Ensure the 'doorOpen' alarm remains active for 2 continuous min
    condition {
      expression = alarmStarter.state.doorOpen equals true
      forDuration(Duration.ofMinutes(2))
    }

    // 3. Actions: Execute parallel reactions
    parallel {
      // Broadcast warning to household speakers
      action(speaker, SpeakerDevice) {
        command(AssistantBroadcast.broadcast("The refrigerator door has been left open!"))
      }

      // Push a notification alerts to home members' mobile devices
      action(structure) {
        command(Notification.sendNotifications(
          "Fridge Alert",
          { body = "The refrigerator door has been open for over 2 min" }
        ))
      }
    }
  }
}

Ví dụ sau đây chuyển tủ lạnh sang chế độ Năng lượng thấp khi phát hiện không có ai ở nhà.

import com.google.home.automation.action
import com.google.home.automation.automation
import com.google.home.automation.condition
import com.google.home.automation.equals
import com.google.home.automation.starter
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.RefrigeratorAndTemperatureControlledCabinetMode
import com.google.home.matter.standard.RefrigeratorAndTemperatureControlledCabinetMode.Companion.changeToMode
import com.google.home.matter.standard.RefrigeratorDevice
import com.google.home.matter.standard.TemperatureControlledCabinetDevice

val structure: Structure = home.structures().first()

// Fetch devices using the multipart device model.
var multipartDevices = homeManager.devices(enableMultipartDevices = true)

// Obtain a reference to the refrigerator device.
val refrigeratorDevice = multipartDevices.first {
  it.has(TemperatureControlledCabinetDevice) &&
  it.has(RefrigeratorAndTemperatureControlledCabinetMode) &&
  it.has(RefrigeratorAlarm)
}

val refrigeratorPartPath = refrigeratorDevice.automationPart(refrigeratorDevice.part(RefrigeratorDevice).first())

// Get all temperature-controlled cabinet parts of the refrigerator
val cabinets = refrigeratorDevice.parts(TemperatureControlledCabinetDevice )

// Find the cabinet part with the 'refrigerator' semantic tag
val refrigeratorCabinet = cabinets.firstOrNull {
  it.metadata.tags.contains(SemanticTag.Refrigerator.refrigerator)
}

val refrigeratorEcoMode = automation {
  name = "Refrigerator Eco Mode"
  description = "Automatically changes refrigerator to low energy mode when house is vacant."
  isActive = true

  sequential {
    // 1. Starter: Monitor household presence changes
    val presenceStarter = starter<_>(structure, AreaPresenceState)

    // 2. Condition: Verify presence state transitions to vacant
    condition {
      expression = presenceStarter.presenceState equals PresenceState.PresenceStateVacant
    }

    // 3. Action: Set refrigerator cabinet Mode to 'Low Energy' (e.g. Mode 1u)
    action(refrigeratorPartPath {
      command(RefrigeratorAndTemperatureControlledCabinetMode.changeToMode(1u))
    }
  }
}