非HomeKit环境下如何通过单条Siri语音指令直接设置空调/温控器温度(附AppIntent代码咨询)
非HomeKit环境下如何通过单条Siri语音指令直接设置空调/温控器温度(附AppIntent代码咨询)
看起来你已经搭建好了基础的Intent框架,核心问题是让Siri能从单条语音指令里直接识别并提取温度值,跳过二次询问的步骤。我结合你的现有代码,帮你梳理问题点和具体的优化方案:
先分析现有代码的潜在卡点
- Temperature参数的默认值可能干扰解析:你给
temperature设置了默认值24.0,虽然方便快速测试,但会让Siri有时优先使用默认值,而不是主动从语音中提取用户指定的温度。 - 唤醒短语不够贴合目标指令:现有
suggestedInvocationPhrases里的示例没有完全匹配你想要的"Set Thermostat temperature 25 degree"结构,Siri可能无法精准理解指令逻辑。 - 参数关联的明确性可以强化:虽然
parameterSummary已经关联了设备和温度,但可以进一步明确指令结构,帮助Siri更清晰地识别需要提取的参数。
针对性修改方案
1. 调整Temperature参数配置
去掉temperature的默认值,让Siri优先从语音中提取温度;只有当用户没说温度时,才会触发询问,完全符合你“单条指令完成”的需求:
@Parameter( title: "Temperature (°C)", description: "The target temperature to set for the device" ) var temperature: Double
2. 优化唤醒短语,匹配目标指令
更新suggestedInvocationPhrases,加入和你需求完全一致的示例指令,帮助Siri学习正确的指令结构:
static var suggestedInvocationPhrases = [ "Set thermostat temperature to \(\.$temperature) degrees", "Set \(\.$device) temperature to \(\.$temperature)°C", "Adjust \(\.$device) to \(\.$temperature) degrees", "Set temperature for \(\.$device) to \(\.$temperature)", "Set Thermostat temperature to 25 degree" // 完全匹配你想要的指令示例 ]
这里的动态占位符\(\.$device)和\(\.$temperature)会让Siri理解:这条指令需要同时指定设备和温度值。
3. 强化参数摘要的明确性
微调parameterSummary,让Siri更清晰地识别指令逻辑:
static var parameterSummary: some ParameterSummary { Summary("Set the temperature of \(\.$device) to \(\.$temperature)°C") }
4. 确保SmartDevice类型的兼容性
要让Siri正确识别你的SmartDevice,这个类型需要满足两个条件:
- 实现
Identifiable协议(你代码里已经用了device.id,应该已经满足) - 暴露易识别的名称字段:确保
SmartDevice的name属性是普通字符串或LocalizedStringResource,且在设备列表中唯一、易被语音识别。
测试与验证
修改完成后,按以下步骤验证:
- 在iOS设置中,进入「Siri与搜索」→ 找到你的App → 确保「允许Siri访问」和「显示Siri建议」已开启。
- 用完整语音指令测试:比如
Hey Siri, Set Thermostat temperature to 25 degrees或者Hey Siri, Set [你的设备名称] to 26°C。 - 如果Siri仍无法正确提取温度,尝试重启App,或在「快捷指令」App中查看你的Intent是否能被正确调用。
最终修改后的完整代码示例
@available(iOS 16.0, *) public struct SetTemp: AppIntent, ProvidesDialog { public var value: Never? public init() { IntentContext.currentIntentType = .setTemperature } public static var title: LocalizedStringResource = "Set Temperature" @Parameter(title: "Temperature") var device: SmartDevice @Parameter( title: "Temperature (°C)", description: "The target temperature to set for the device" ) var temperature: Double public static var openAppWhenRun: Bool = false public static var parameterSummary: some ParameterSummary { Summary("Set the temperature of \(\.$device) to \(\.$temperature)°C") } static var suggestedInvocationPhrases = [ "Set thermostat temperature to \(\.$temperature) degrees", "Set \(\.$device) temperature to \(\.$temperature)°C", "Adjust \(\.$device) to \(\.$temperature) degrees", "Set temperature for \(\.$device) to \(\.$temperature)", "Set Thermostat temperature to 25 degree" ] public func perform() async throws -> some IntentResult & ProvidesDialog { let tempStr = String(Int(temperature)) // 按需取整或保留小数 let resultStr = try await sendSwitchCommand( categoryType: .setTemperature, deviceName: device.name, deviceID: device.id, state: tempStr, locationId: device.locId, categoryId: device.categoryId ) return .result(dialog: IntentDialog(stringLiteral: resultStr)) } }
这样修改后,Siri应该能直接从单条语音指令中提取设备名称和温度值,一步完成温度设置,不需要额外的询问步骤。
内容来源于stack exchange
相关产品推荐
相关产品推荐

