基本 Chime 设备类型可以使用基于标准 Matter 的 Matter.ChimeTrait 或 Google 自定义的特征版本 Google.ChimeTrait 来实现,后者除了支持 Matter 标准中指定的功能外,还支持外部响铃。电铃设备还可以实现 Matter.IdentifyTrait,该接口提供了一种通过视觉或声音识别电铃设备的方法。
在尝试使用任何功能或更新属性之前,请务必先检查设备是否支持相应属性和命令。如需了解详情,请参阅在iOS上控制设备。
| Home API 设备类型 | 特征 | Swift 示例应用 | 使用场景 |
|---|---|---|---|
|
电铃
一种可以播放一系列预安装声音的设备,通常与门铃、音频门铃或可视门铃搭配使用。 |
必需的特征 matter ChimeTrait |
门铃 |
获取设备的基本信息
BasicInformation 特征包含设备的相关信息,例如供应商名称、供应商 ID、产品 ID、产品名称(包括型号信息)和软件版本:
let vendorName = basicInfoTrait.attributes.vendorName! let vendorID = basicInfoTrait.attributes.vendorID! let productID = basicInfoTrait.attributes.productID! let productName = basicInfoTrait.attributes.productName! let softwareVersion = basicInfoTrait.attributes.softwareVersion!
多部分支持
门铃可能由多种设备类型组成,包括 Chime 和可选的 Speaker。此外,某些铃声还包含 Thread Border Router (TBR)。为了确保最广泛的支持,控制电铃的 Home API 应用应编写为可适应多部分电铃设备。如需了解详情,请参阅多部分设备。
每个多部件电铃设备都应在应用中以单个功能块表示。Chime 设备应是主设备,而子音箱应是 Chime 下的部分。
扬声器音量应显示为 Chime 设备的主要设置。
OnOff 映射
对于同时具有 Chime 和 Speaker 设备类型的门铃,控制应用中的 OnOff 按钮应映射到门铃的 Enabled 属性。
根据 Matter 设备库规范,将 Chime 特征的 Enabled 属性设置为 False 除了会停用响铃之外,还可充当全局静音开关,抑制所有可听见和可见的指示器。
自动化支持
如前所述,由于 ChimeDeviceType 可能包含多个部分,因此建议开发者将所有铃声都视为多部分设备,对于使用铃声的自动化操作,这一点仍然适用。
铃声可用于自动化操作启动方式和操作。
自动化中使用的父 Chime 设备下的任何组件设备都必须使用 AutomationPartPath 进行引用。
以下自动化操作演示了如何创建具有多部分铃声的自动化操作,该铃声实现了 Chime 特征以及扬声器。当打开前门时,自动化操作会被触发:
import GoogleHomeSDK import GoogleHomeTypes typealias ChimeTrait = Matter.ChimeTrait typealias OpenCloseTrait = Google.OpenCloseTrait // Fetch devices using the multipart device model. let multipartDevices = try await home.devices(enableMultipartDevices: true).list() guard let chimeParentDevice = multipartDevices.first(where: { $0.types.contains(ChimeDeviceType.self) }) else { return } guard let doorDevice = multipartDevices.first(where: { $0.types.contains(GoogleDoorDeviceType.self) }) else { return } // Extract the AutomationPartPath for the specific Chime part guard let chimeType = await chimeParentDevice.types.get(ChimeDeviceType.self), let chimePartPath = chimeParentDevice.automationPart(chimeType) else { return } let starterNode = starter(doorDevice, GoogleDoorDeviceType.self, OpenCloseTrait.self) let doorOpenClose = stateReader(doorDevice, GoogleDoorDeviceType.self, OpenCloseTrait.self) // Build the automation let chimeAutomation = automation( name: "Multipart Chime Demo", description: "Activate the chime when the door is opened" ) { starterNode condition { starterNode.openPercent.unwrapOptional().greaterThan(0) } // Action: Play a chime sound action(chimePartPath) { ChimeTrait.playChimeSound() } }
如需详细了解如何使用多部件设备实现自动化,请参阅多部件设备自动化。
检查设备的网络连接
设备的连接性实际上是在设备类型级别进行检查的,因为某些设备支持多种设备类型。返回的状态是相应设备上所有特征的连接状态的组合。
let lightConnectivity = dimmableLightDeviceType.metadata.sourceConnectivity .connectivityState
如果设备类型混杂且未连接到互联网,则可能会出现 partiallyOnline 状态。由于本地路由,Matter 标准特征可能仍处于在线状态,但基于云的特征将处于离线状态。
获取设备的 IP 地址
如需查找设备的 IP 地址,请使用 GeneralDiagnosticsTrait 的 networkInterfaces 属性。
地址以 Data 对象的形式返回,您可以使用 Network 框架将其格式化为标准 IPv4 或 IPv6 字符串:
func getIpAddresses(trait: Matter.GeneralDiagnosticsTrait) -> [String] {
let interfaces = trait.attributes.networkInterfaces ?? []
var ipAddresses: [String] = []
for interface in interfaces {
for data in interface.iPv4Addresses {
if let ipv4 = IPv4Address(data) {
ipAddresses.append(String(describing: ipv4))
}
}
for data in interface.iPv6Addresses {
if let ipv6 = IPv6Address(data) {
ipAddresses.append(String(describing: ipv6))
}
}
}
return ipAddresses
}