Fix ps4 not able to use different PSN accounts (#22799)
* Remove skipping of creds step. * Check for device added per account * typo * lint * Pylint * Fix test * Fix test * Typo * Add auto location * blank space * Add new identifier handling + fix select source * Add cred_timeout error * add credential timeout error * Fix Tests * patch decorator * Update test_config_flow.py * add test * Revert * Rename vars * fix tests * Add attr location * Bump 0.6.0 * Bump 0.6.0 * Bump 0.6.0 * Update handling exception * Update remove method * Update tests * Refactoring * Pylint * revert * chmod * 0.6.1 * 0.6.1 * 0.6.1 * Remove func * Add migration * Version 3 * Remove redefinition * Add format unique id * Add format unique id * pylint * pylint * 0.7.1 * 0.7.1 * 0.7.1 * Changes with media_art call * Add library exception * 0.7.2 * 0.7.2 * 0.7.2 * Version and entry_version update * Revert list comprehension * Corrected exception handling * Update media_player.py * Update media_player.py * white space
This commit is contained in:
1 parent
d505f1c5f2
commit
68d3e624e6
9 files changed
+157
-47
No files matched your search
@@ -1,7 +1,9 @@
|
||||
"""Support for PlayStation 4 consoles."""
|
||||
import logging
|
||||
|
||||
from homeassistant.const import CONF_REGION
|
||||
from homeassistant.core import split_entity_id
|
||||
from homeassistant.const import CONF_REGION, CONF_TOKEN
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.util import location
|
||||
|
||||
from .config_flow import PlayStation4FlowHandler # noqa: pylint: disable=unused-import
|
||||
@@ -37,9 +39,14 @@ async def async_migrate_entry(hass, entry):
|
||||
data = entry.data
|
||||
version = entry.version
|
||||
|
||||
reason = {1: "Region codes have changed"} # From 0.89
|
||||
_LOGGER.debug("Migrating PS4 entry from Version %s", version)
|
||||
|
||||
# Migrate Version 1 -> Version 2
|
||||
reason = {
|
||||
1: "Region codes have changed",
|
||||
2: "Format for Unique ID for entity registry has changed"
|
||||
}
|
||||
|
||||
# Migrate Version 1 -> Version 2: New region codes.
|
||||
if version == 1:
|
||||
loc = await hass.async_add_executor_job(location.detect_location_info)
|
||||
if loc:
|
||||
@@ -47,11 +54,41 @@ async def async_migrate_entry(hass, entry):
|
||||
if country in COUNTRIES:
|
||||
for device in data['devices']:
|
||||
device[CONF_REGION] = country
|
||||
entry.version = 2
|
||||
version = entry.version = 2
|
||||
config_entries.async_update_entry(entry, data=data)
|
||||
_LOGGER.info(
|
||||
"PlayStation 4 Config Updated: \
|
||||
Region changed to: %s", country)
|
||||
|
||||
# Migrate Version 2 -> Version 3: Update identifier format.
|
||||
if version == 2:
|
||||
# Prevent changing entity_id. Updates entity registry.
|
||||
registry = await entity_registry.async_get_registry(hass)
|
||||
|
||||
for entity_id, e_entry in registry.entities.items():
|
||||
if e_entry.config_entry_id == entry.entry_id:
|
||||
unique_id = e_entry.unique_id
|
||||
|
||||
# Remove old entity entry.
|
||||
registry.async_remove(entity_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# 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,
|
||||
suggested_object_id=new_id,
|
||||
config_entry_id=e_entry.config_entry_id,
|
||||
device_id=e_entry.device_id
|
||||
)
|
||||
entry.version = 3
|
||||
_LOGGER.info(
|
||||
"PlayStation 4 identifier for entity: %s \
|
||||
has changed", entity_id)
|
||||
config_entries.async_update_entry(entry)
|
||||
return True
|
||||
|
||||
msg = """{} for the PlayStation 4 Integration.
|
||||
@@ -64,3 +101,9 @@ async def async_migrate_entry(hass, entry):
|
||||
notification_id='config_entry_migration'
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
def format_unique_id(creds, mac_address):
|
||||
"""Use last 4 Chars of credential as suffix. Unique ID per PSN user."""
|
||||
suffix = creds[-4:]
|
||||
return "{}_{}".format(mac_address, suffix)
|
||||
Reference in new issue
Block a user