键值 API 为设备、房间和自动化对象提供了新的自定义数据管理 API。自定义数据的生命周期严格限定于与其关联的对象的生命周期。例如,与房间对象关联的自定义数据(如“Lucy 的房间”)将随房间对象自动删除、移动或转移(跨结构)。
Kotlin API 更改以
hasCustomAppData
接口为中心,并为自定义应用
数据所属的对象提供扩展,包括标准的创建、读取、更新和删除
(CRUD) 函数。
设备示例
val device = homeClient.devicesWithLogging().findDeviceByName(deviceName)
var response = device.getValue(key)
println("Before: $response")
device.updateValue(key, value)
response = device.getValue(key)
println("After: $response")
device.deleteValue(key)
房间示例
val room = homeClient.rooms().findRoomByName(roomName)
room.updateValue("nickname", "Lucy's Room")
val response = room.getValue("nickname")
room.deleteValue("nickname")
自动化示例
val automation = homeClient.automations().findAutomationByName(automationName)
var response = automation.getValue(key)
println("Before: $response")
automation.updateValue(key, value)
response = automation.getValue(key)
println("After: $response")
automation.deleteValue(key)
限制
- 键必须是包含有效 UTF-8 字符的字符串,且长度上限为 128。键可以是空字符串。
- 值必须是包含有效 UTF-8 字符的字符串,且长度上限为 1024。值可以是空字符串。
- 每个对象最多可以有 10 行键值对。