From 2cb58b818955a3291e25da62f1b06c2b8f2ce248 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 8 Nov 2022 09:20:03 -0600 Subject: [PATCH] Fix off by one in HomeKit iid allocator (#81793) --- homeassistant/components/homekit/iidmanager.py | 8 ++++---- tests/components/homekit/test_diagnostics.py | 2 -- tests/components/homekit/test_iidmanager.py | 6 ++++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/homekit/iidmanager.py b/homeassistant/components/homekit/iidmanager.py index 3805748225a..8bac268800b 100644 --- a/homeassistant/components/homekit/iidmanager.py +++ b/homeassistant/components/homekit/iidmanager.py @@ -109,12 +109,12 @@ class AccessoryIIDStorage: # AID must be a string since JSON keys cannot be int aid_str = str(aid) accessory_allocation = self.allocations.setdefault(aid_str, {}) - accessory_allocated_iids = self.allocated_iids.setdefault(aid_str, []) + accessory_allocated_iids = self.allocated_iids.setdefault(aid_str, [1]) if service_hap_type == ACCESSORY_INFORMATION_SERVICE and char_uuid is None: - allocated_iid = 1 - elif allocation_key in accessory_allocation: + return 1 + if allocation_key in accessory_allocation: return accessory_allocation[allocation_key] - elif accessory_allocated_iids: + if accessory_allocated_iids: allocated_iid = accessory_allocated_iids[-1] + 1 else: allocated_iid = 2 diff --git a/tests/components/homekit/test_diagnostics.py b/tests/components/homekit/test_diagnostics.py index be98c3bacdd..d114c462e2f 100644 --- a/tests/components/homekit/test_diagnostics.py +++ b/tests/components/homekit/test_diagnostics.py @@ -51,7 +51,6 @@ async def test_config_entry_running(hass, hass_client, hk_driver, mock_async_zer "3E__23_": 5, "3E__30_": 6, "3E__52_": 7, - "3E___": 1, "A2__37_": 9, "A2___": 8, } @@ -278,7 +277,6 @@ async def test_config_entry_accessory( "3E__23_": 5, "3E__30_": 6, "3E__52_": 7, - "3E___": 1, "43__25_": 11, "43___": 10, "A2__37_": 9, diff --git a/tests/components/homekit/test_iidmanager.py b/tests/components/homekit/test_iidmanager.py index 3e4a19c9045..c16cbf01d7d 100644 --- a/tests/components/homekit/test_iidmanager.py +++ b/tests/components/homekit/test_iidmanager.py @@ -152,23 +152,29 @@ async def test_iid_generation_and_restore_v2(hass, iid_storage, hass_storage): 1, "000000AA-0000-1000-8000-0026BB765291", None, None, None ) assert not_accessory_info_service_iid == 2 + assert iid_storage.allocated_iids == {"1": [1, 2]} not_accessory_info_service_iid_2 = iid_storage.get_or_allocate_iid( 1, "000000BB-0000-1000-8000-0026BB765291", None, None, None ) assert not_accessory_info_service_iid_2 == 3 + assert iid_storage.allocated_iids == {"1": [1, 2, 3]} not_accessory_info_service_iid_2 = iid_storage.get_or_allocate_iid( 1, "000000BB-0000-1000-8000-0026BB765291", None, None, None ) assert not_accessory_info_service_iid_2 == 3 + assert iid_storage.allocated_iids == {"1": [1, 2, 3]} accessory_info_service_iid = iid_storage.get_or_allocate_iid( 1, "0000003E-0000-1000-8000-0026BB765291", None, None, None ) assert accessory_info_service_iid == 1 + assert iid_storage.allocated_iids == {"1": [1, 2, 3]} accessory_info_service_iid = iid_storage.get_or_allocate_iid( 1, "0000003E-0000-1000-8000-0026BB765291", None, None, None ) assert accessory_info_service_iid == 1 + assert iid_storage.allocated_iids == {"1": [1, 2, 3]} accessory_info_service_iid = iid_storage.get_or_allocate_iid( 2, "0000003E-0000-1000-8000-0026BB765291", None, None, None ) assert accessory_info_service_iid == 1 + assert iid_storage.allocated_iids == {"1": [1, 2, 3], "2": [1]}