Only clear restored entity names when the update provides no names

This commit is contained in:
Paul Bottein
2026-07-30 15:17:05 +02:00
parent 1e1ca36d57
commit 5c7a146b76
2 changed files with 24 additions and 8 deletions
@@ -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
@@ -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",
}