Avoid linear search of the entity registry in ps4 (#109723)

This commit is contained in:
J. Nick Koston 2024-02-05 16:25:12 -06:00 committed by GitHub
parent 440212ddce
commit 6fce8a5403
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 29 deletions

View File

@ -120,33 +120,35 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Prevent changing entity_id. Updates entity registry. # Prevent changing entity_id. Updates entity registry.
registry = er.async_get(hass) registry = er.async_get(hass)
for entity_id, e_entry in registry.entities.items(): for e_entry in registry.entities.get_entries_for_config_entry_id(
if e_entry.config_entry_id == entry.entry_id: entry.entry_id
unique_id = e_entry.unique_id ):
unique_id = e_entry.unique_id
entity_id = e_entry.entity_id
# Remove old entity entry. # Remove old entity entry.
registry.async_remove(entity_id) registry.async_remove(entity_id)
# Format old unique_id. # Format old unique_id.
unique_id = format_unique_id(entry.data[CONF_TOKEN], unique_id) unique_id = format_unique_id(entry.data[CONF_TOKEN], unique_id)
# Create new entry with old entity_id. # Create new entry with old entity_id.
new_id = split_entity_id(entity_id)[1] new_id = split_entity_id(entity_id)[1]
registry.async_get_or_create( registry.async_get_or_create(
"media_player", "media_player",
DOMAIN, DOMAIN,
unique_id, unique_id,
suggested_object_id=new_id, suggested_object_id=new_id,
config_entry=entry, config_entry=entry,
device_id=e_entry.device_id, device_id=e_entry.device_id,
) )
entry.version = 3 entry.version = 3
_LOGGER.info( _LOGGER.info(
"PlayStation 4 identifier for entity: %s has changed", "PlayStation 4 identifier for entity: %s has changed",
entity_id, entity_id,
) )
config_entries.async_update_entry(entry) config_entries.async_update_entry(entry)
return True return True
msg = f"""{reason[version]} for the PlayStation 4 Integration. msg = f"""{reason[version]} for the PlayStation 4 Integration.
Please remove the PS4 Integration and re-configure Please remove the PS4 Integration and re-configure

View File

@ -344,11 +344,13 @@ class PS4Device(MediaPlayerEntity):
_LOGGER.info("Assuming status from registry") _LOGGER.info("Assuming status from registry")
e_registry = er.async_get(self.hass) e_registry = er.async_get(self.hass)
d_registry = dr.async_get(self.hass) d_registry = dr.async_get(self.hass)
for entity_id, entry in e_registry.entities.items():
if entry.config_entry_id == self._entry_id: for entry in e_registry.entities.get_entries_for_config_entry_id(
self._attr_unique_id = entry.unique_id self._entry_id
self.entity_id = entity_id ):
break self._attr_unique_id = entry.unique_id
self.entity_id = entry.entity_id
break
for device in d_registry.devices.values(): for device in d_registry.devices.values():
if self._entry_id in device.config_entries: if self._entry_id in device.config_entries:
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(