基本响铃设备类型可以使用基于标准 Matter 的 trait com.google.home.matter.standard.Chime 或 Google 自己的自定义版本 trait com.google.home.google.Chime 来实现,后者除了支持 Matter 标准中指定的功能外,还支持外部电铃。电铃设备还可以实现 Identify trait,该 trait 提供了一种通过视觉或声音识别电铃设备的方法。
在尝试使用任何功能或更新属性之前,请务必先检查设备是否支持相应属性和命令。如需了解详情,请参阅在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}") }
多部分支持
门铃可能由多种设备类型组成,包括 Chime 和可选的 Speaker。此外,某些铃声还包含 Thread Border Router (TBR)。为了确保最广泛的支持,控制电铃的 Home API 应用应编写为可适应多部分电铃设备。如需了解详情,请参阅多部分设备。
每个多部件电铃设备都应在应用中以单个功能块表示。Chime 设备应是主设备,而子音箱应是 Chime 下的部分。
扬声器音量应显示为 Chime 设备的主要设置。
OnOff 映射
对于同时具有 Chime 和 Speaker 设备类型的门铃,控制应用中的 OnOff 按钮应映射到门铃的 Enabled 属性。
根据 Matter 设备库规范,将 Chime 特征的 Enabled 属性设置为 False 除了会停用响铃之外,还可充当全局静音开关,抑制所有可听见和可见的指示器。
自动化支持
如前所述,由于 ChimeDevice 可能包含多个部分,因此建议开发者将所有门铃都视为多部分设备,对于使用门铃的自动化操作,此建议仍然适用。
铃声可用于自动化操作启动方式和操作。
自动化中使用的父 Chime 设备下的任何组件设备都必须使用 AutomationPartPath 进行引用。
以下自动化操作演示了如何创建具有多部分铃声的自动化操作,该铃声实现了 Chime 特征以及扬声器。当打开前门时,自动化操作会被触发:
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()