diff --git a/homeassistant/components/bluetooth/passive_update_processor.py b/homeassistant/components/bluetooth/passive_update_processor.py index f389bf56d762..c90eeefb32cb 100644 --- a/homeassistant/components/bluetooth/passive_update_processor.py +++ b/homeassistant/components/bluetooth/passive_update_processor.py @@ -165,15 +165,17 @@ class PassiveBluetoothDataUpdate[_T]: if current.get(key, UNDEFINED) != data: changed_entity_keys.add(key) current[key] = data # type: ignore[assignment] - # A key present in the update without a name means the integration - # does not name that entity; clear any stale name restored from storage. - for key in new_data.entity_data.keys() | new_data.entity_descriptions.keys(): - if ( - key not in new_data.entity_names - and self.entity_names.get(key) is not None + # An update without any names means the integration does not name its + # entities; clear stale names restored from storage for the updated keys. + # Updates that provide names for only some keys are left untouched since + # the missing names may be provided by other partial updates. + if not new_data.entity_names: + for key in ( + new_data.entity_data.keys() | new_data.entity_descriptions.keys() ): - changed_entity_keys.add(key) - self.entity_names[key] = None + if self.entity_names.get(key) is not None: + changed_entity_keys.add(key) + self.entity_names[key] = None # If the device changed we don't need to return the changed # entity keys as all entities will be updated return None if device_change else changed_entity_keys diff --git a/tests/components/bluetooth/test_passive_update_processor.py b/tests/components/bluetooth/test_passive_update_processor.py index 6305a690618b..f3ea25201b4b 100644 --- a/tests/components/bluetooth/test_passive_update_processor.py +++ b/tests/components/bluetooth/test_passive_update_processor.py @@ -2029,3 +2029,17 @@ def test_update_clears_names_missing_from_the_update() -> None: temperature_key: "Custom name", pressure_key: "Pressure", } + + update_with_partial_names = PassiveBluetoothDataUpdate( + devices={None: DeviceInfo(name="Test Device")}, + entity_descriptions={temperature_key: temperature_description}, + entity_names={pressure_key: "Pressure"}, + entity_data={temperature_key: 15.5, pressure_key: 1234}, + ) + + # An update naming only some keys does not clear the other names + assert data.update(update_with_partial_names) == set() + assert data.entity_names == { + temperature_key: "Custom name", + pressure_key: "Pressure", + }