Fix PS4 v2->v3 migration (#179489)

This commit is contained in:
Erik Montnemery
2026-08-18 16:01:30 +02:00
committed by GitHub
parent 0140b9039b
commit aeb5fe4b5d
2 changed files with 23 additions and 31 deletions
+12 -25
View File
@@ -16,7 +16,7 @@ from homeassistant.components.media_player import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_LOCKED, CONF_REGION, CONF_TOKEN, Platform
from homeassistant.core import HomeAssistant, split_entity_id
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@@ -101,39 +101,26 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
country,
)
# Migrate Version 2 -> Version 3: Update identifier format.
# Migrate Version 2 -> Version 3: Update unique_id format.
if version == 2:
# Prevent changing entity_id. Updates entity registry.
# Update the unique_id.
registry = er.async_get(hass)
for e_entry in registry.entities.get_entries_for_config_entry_id(
entry.entry_id
):
unique_id = e_entry.unique_id
entity_id = e_entry.entity_id
# Remove old entity entry.
registry.async_remove(entity_id)
# Format old unique_id.
unique_id = format_unique_id(entry.data[CONF_TOKEN], unique_id)
# Create new entry with old entity_id.
new_id = split_entity_id(entity_id)[1]
registry.async_get_or_create(
"media_player",
DOMAIN,
unique_id,
config_entry=entry,
device_id=e_entry.device_id,
object_id_base=new_id,
registry.async_update_entity(
e_entry.entity_id,
new_unique_id=format_unique_id(
entry.data[CONF_TOKEN], e_entry.unique_id
),
)
_LOGGER.debug(
"PlayStation 4 identifier for entity: %s has changed",
entity_id,
"PlayStation 4 unique_id for entity %s has been updated",
e_entry.entity_id,
)
config_entries.async_update_entry(entry, version=3)
return True
config_entries.async_update_entry(entry, version=3)
return True
msg = f"""{reason[version]} for the PlayStation 4 Integration.
Please remove the PS4 Integration and re-configure
+11 -6
View File
@@ -142,11 +142,16 @@ async def test_config_flow_entry_migrate(
manager = hass.config_entries
mock_entry = MOCK_ENTRY_VERSION_1
mock_entry.add_to_manager(manager)
# The integration registers the PS4 device with a name (the console host name),
# so the entity id is derived from the device name.
mock_device_entry = device_registry.async_get_or_create(
config_entry_id=mock_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
identifiers={(DOMAIN, MOCK_UNIQUE_ID)},
manufacturer="Sony Interactive Entertainment Inc.",
model="PlayStation 4",
name="My PS4",
)
mock_entity_id = f"media_player.ps4_{MOCK_UNIQUE_ID}"
mock_e_entry = entity_registry.async_get_or_create(
"media_player",
"ps4",
@@ -154,8 +159,9 @@ async def test_config_flow_entry_migrate(
config_entry=mock_entry,
device_id=mock_device_entry.id,
)
mock_entity_id = mock_e_entry.entity_id
assert len(entity_registry.entities) == 1
assert mock_e_entry.entity_id == mock_entity_id
assert mock_entity_id == "media_player.my_ps4"
assert mock_e_entry.unique_id == MOCK_UNIQUE_ID
with (
@@ -173,10 +179,9 @@ async def test_config_flow_entry_migrate(
await hass.async_block_till_done()
assert len(entity_registry.entities) == 1
for entity in entity_registry.entities.values():
mock_entity = entity
# Test that entity_id remains the same.
# The migration must keep the entity_id unchanged.
mock_entity = entity_registry.async_get(mock_entity_id)
assert mock_entity is not None
assert mock_entity.entity_id == mock_entity_id
assert mock_entity.device_id == mock_device_entry.id