Automatyzacje urządzeń wieloczęściowych na Androidzie

Automatyzacja może odwoływać się do urządzeń wieloczęściowych podobnie jak automatyzacja, która nie korzysta z takich urządzeń.

Najpierw uzyskaj komponenty w zwykły sposób. Informacje o tym, jak pracować z urządzeniami wieloczęściowymi, znajdziesz w artykule Urządzenia wieloczęściowe.

Następnie dla każdej części, której chcesz użyć w automatyzacji, utwórz AutomationPartPath, który umożliwi Ci odwoływanie się do tej części w starterach, warunkach i działaniach automatyzacji.

// 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())

Pamiętaj też, że działania automatyzacji zwykle przyjmują jako parametry urządzenie i jego typ. Jednak działanie automatyzacji, które odwołuje się do urządzenia będącego częścią składową – ponownie za pomocą AutomationPartPath – potrzebuje tylko AutomationPartPath, ponieważ oprócz odniesienia do urządzenia będącego częścią składową zawiera ono też odniesienie do typu urządzenia.AutomationPartPath

Na przykład w Automation API typ urządzenia Refrigerator może być traktowany jako urządzenie wieloczęściowe. Składa się ono z głównego urządzenia RefrigeratorDevice, które może zawierać wiele podrzędnych części składowych, takich jak zamrażarki lub standardowe szafki typu TemperatureControlledCabinetDevice.

Aby tworzyć automatyzacje lodówki, musisz przede wszystkim korzystać z 2 standardowych Matter cech:

  • RefrigeratorAlarm: udostępnia alarmy stanu drzwi za pomocą atrybutu doorOpen pola state.
  • RefrigeratorAndTemperatureControlledCabinetMode: umożliwia odczytywanie i sterowanie trybami. Na przykład uruchamianie poleceń takich jak changeToMode, aby przełączać się między trybami LowEnergy, RapidCool lub LowNoise.

Ta przykładowa automatyzacja jest uruchamiana, gdy otworzą się drzwi lodówki. Jeśli drzwi pozostaną otwarte przez ponad 2 minuty, automatyzacja wyśle powiadomienie głosowe na głośniki inteligentne, włączy światła w kuchni i wyśle powiadomienie push. Pamiętaj, że ta automatyzacja wpływa tylko na komorę lodówki, ignorując komorę zamrażarki (jeśli istnieje):

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

Poniższy przykład przełącza lodówkę w tryb niskiego zużycia energii, gdy wykryje, że nikogo nie ma w domu.

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