基本门铃设备类型可以使用基于标准 Matter 的 trait com.google.home.matter.standard.Chime 或 Google 自己的自定义版本 trait com.google.home.google.Chime 来实现,除了 Matter 标准中指定的功能外,该 trait 还支持外部门铃。门铃设备还可以实现 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 下的部分。
音箱音量应显示为 Chime 设备的主要设置。
OnOff 映射
对于同时具有“响铃”和“音箱”设备类型的响铃,控制应用中的 OnOff 按钮应映射到响铃的 Enabled 属性。
根据 Matter 设备库规范,将 Chime 特性的 Enabled 属性设置为 False 除了停用 Chime 之外,还可能充当全局静音开关,抑制所有声音和视觉指示器。
自动化支持
如前所述,由于 ChimeDevice 可能包含多个部分,因此我们建议开发者将所有铃声设备都视为多部分设备,在使用铃声设备的自动化操作中也是如此。
门铃可用于自动化操作启动方式和操作。
自动化中使用的父级门铃设备下的任何组件设备都必须使用 AutomationPartPath 进行引用。
以下自动化操作演示了如何创建包含多部分提示音的自动化操作,该提示音实现了 Chime trait 以及 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()