基本 Chime 裝置類型可使用標準 Matter 型特徵 com.google.home.matter.standard.Chime 或 Google 自訂特徵 com.google.home.google.Chime 實作,後者除了支援 Matter 標準中指定的各項功能,也支援外部鈴聲。鈴聲裝置也可以實作 Identify 特徵,提供透過視覺或聲音識別鈴聲裝置的方法。
使用任何功能或嘗試更新屬性之前,請務必先檢查裝置是否支援屬性和指令。詳情請參閱「透過Android控制裝置」。
| Home API 裝置類型 | 特徵 | Kotlin 範例應用程式 | 用途 |
|---|---|---|---|
|
電鈴
這類裝置可播放一系列預先安裝的音效,通常會搭配門鈴、語音門鈴或視訊門鈴使用。 |
必要特徵 matter Chime |
門鈴 |
取得裝置的基本資訊
BasicInformation 特徵包括裝置的供應商名稱、供應商 ID、產品 ID、產品名稱 (包括型號資訊) 和軟體版本等資訊:
// Get device basic information. All general information traits are on the RootNodeDevice type. device.type(RootNodeDevice).first().standardTraits.basicInformation?.let { basicInformation -> println("vendorName ${basicInformation.vendorName}") println("vendorId ${basicInformation.vendorId}") println("productId ${basicInformation.productId}") println("productName ${basicInformation.productName}") println("softwareVersion ${basicInformation.softwareVersion}") }
支援多部分
電鈴可以由多種裝置類型組成,包括電鈴和選用的喇叭。此外,部分鈴聲會加入 Thread Border Router (TBR),為確保支援範圍最廣,控制電鈴的 Home API 應用程式應編寫為可配合多部分電鈴裝置。詳情請參閱「多部分裝置」。
應用程式中應以單一動態磚代表每個多部分鈴聲裝置。Chime 裝置應為主要裝置,而子音箱應為 Chime 底下的零件。
電鈴裝置的主要設定應會顯示喇叭音量。
OnOff 對應
如果鈴聲同時具有 Chime 和 Speaker 裝置類型,控制應用程式中的 OnOff 按鈕應對應至 Chime 的 Enabled 屬性。
根據Matter裝置媒體庫規格,將鈴聲特徵的 Enabled 屬性設為 False,除了停用鈴聲外,還可能像全域靜音切換開關一樣,抑制所有可聽見和可見的指標。
自動化支援
如先前所述,由於 ChimeDevice 可能有多個零件,因此建議開發人員將所有鈴聲視為多零件裝置,使用鈴聲的自動化作業也是如此。
鈴聲可用於自動化啟動條件和動作。
自動化動作中使用的父項 Chime 裝置下的任何元件裝置,都必須使用 AutomationPartPath 參照。
下列自動化動作示範如何建立自動化動作,其中包含實作 Chime 特徵和 Speaker 的多部分鈴聲。前門開啟時觸發自動化動作:
import com.google.home.automation.automation import com.google.home.automation.greaterThan import com.google.home.google.GoogleDoorDevice import com.google.home.google.OpenClose import com.google.home.matter.standard.ChimeDevice import com.google.home.matter.standard.Chime // ... // Fetch devices val devices = homeManager.devices() // Fetch devices using the multipart device model. val multipartDevices = homeManager.devices(enableMultipartDevices = true) val doorDevice = devices.first() { it.has(GoogleDoorDevice) && it.has(OpenClose) } // Obtain a reference to the chime device. val chime = multipartDevices.first() { it.has(ChimeDevice) && it.has(Chime) } // Extract the AutomationPartPath for the specific Chime part val chimePartPath = chime.automationPart(chime.part(ChimeDevice).first())!! // Build the automation val chimeAutomation = automation { sequential { val doorOpenClose = stateReader<_>(doorDevice, GoogleDoorDevice, OpenClose) // Starter: When the door state changes val starterNode = starter<_>(doorDevice, GoogleDoorDevice, OpenClose) condition { expression = doorOpenClose.openPercent greaterThan(0) } // Action: Play a chime sound action(chimePartPath) { command(Chime.playChimeSound()) } } }
如要進一步瞭解使用多部裝置的自動化動作,請參閱「多部裝置自動化動作」。
檢查裝置的連線狀態
裝置的連線狀態實際上是在裝置類型層級檢查,因為部分裝置支援多種裝置類型。傳回的狀態是該裝置上所有特徵的連線狀態組合。
在 Android 範例應用程式中實作val lightConnectivity = dimmableLightDevice.metadata.sourceConnectivity.connectivityState
如果沒有網路連線,混合裝置類型可能會出現 PARTIALLY_ONLINE 狀態。Matter標準特徵仍可能因本地路由而處於線上狀態,但雲端特徵會離線。
取得裝置的 IP 位址
如要找出裝置的 IP 位址,請使用 GeneralDiagnostics 特徵的 networkInterfaces 屬性。位址會以位元組陣列的形式傳回,您可以將其格式化為標準 IPv4 或 IPv6 字串:
val ipAddresses =
trait.networkInterfaces?.flatMap { networkInterface ->
(networkInterface.ipv4Addresses + networkInterface.ipv6Addresses).mapNotNull { bytes ->
try {
java.net.InetAddress.getByAddress(bytes).hostAddress
} catch (e: java.net.UnknownHostException) {
null
}
}
} ?: emptyList()