鍵值 API

Key Value 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)

房間範例

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 列鍵/值組合。