键值 API 为设备、房间和自动化对象提供了新的自定义数据管理 API。自定义数据的生命周期严格限定为与其关联的对象的生命周期。例如,与 Room 对象关联的自定义数据(例如“Lucy 的房间”)将随 Room 对象一起自动删除、移动或转移(跨结构)。
Swift API 更改以 CustomAppData 控制器为中心,并为自定义应用数据所属的对象提供扩展,包括标准的创建、读取、更新和删除 (CRUD) 功能。
设备示例
guard let device = home.devices.first(where: { $0.name == deviceName }) else { return }
var response = try await device.customAppData[key]
print("Before: \(response)")
try await device.customAppData.update(key: key, value: value)
response = try await device.customAppData[key]
print("After: \(response)")
try await device.customAppData.delete(key)
Room 示例
guard let room = home.rooms.first(where: { $0.name == roomName }) else { return }
try await room.customAppData.update(key: "nickname", value: "Lucy's Room")
let response = try await room.customAppData["nickname"]
try await room.customAppData.delete("nickname")
自动化示例
guard let automation = home.automations.first(where: { $0.name == automationName }) else { return }
var response = try await automation.customAppData[key]
print("Before: \(response)")
try await automation.customAppData.update(key: key, value: value)
response = try await automation.customAppData[key]
print("After: \(response)")
try await automation.customAppData.delete(key)
限制
- 键必须是包含有效 UTF-8 字符的字符串,长度上限为 128 个字符。键可以是空字符串。
- 该值必须是包含有效 UTF-8 字符的字符串,长度上限为 1024 个字符。该值可以为空字符串。
- 每个对象最多可以有 10 行键值对。