From c9f3a5660c96e057edb368a66399c3417df2a9c3 Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Sun, 30 Jun 2024 23:56:49 +0000 Subject: [PATCH 01/14] testing entity migration --- custom_components/playstation_network/sensor.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/custom_components/playstation_network/sensor.py b/custom_components/playstation_network/sensor.py index 3d3775e..f35527d 100644 --- a/custom_components/playstation_network/sensor.py +++ b/custom_components/playstation_network/sensor.py @@ -146,8 +146,8 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]: name="Trophy Level", icon="mdi:trophy", entity_registry_enabled_default=True, - has_entity_name=True, - unique_id="trophies", + has_entity_name=False, + unique_id="psn_trophies", value_fn=lambda data: data.get("trophy_summary").trophy_level, attributes_fn=get_trophy_attr, ), @@ -158,8 +158,8 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]: icon="mdi:account-circle-outline", options=["Online", "Offline", "Playing"], entity_registry_enabled_default=True, - has_entity_name=True, - unique_id="status", + has_entity_name=False, + unique_id="psn_status", value_fn=get_status, attributes_fn=get_status_attr, ), @@ -185,6 +185,7 @@ def __init__(self, coordinator, description: PsnSensorEntityDescription) -> None """Initialize PSN Sensor.""" super().__init__(coordinator) self._attr_unique_id = f"{coordinator.data.get("username")}_{description.unique_id}" + self._attr_unique_id = f"psn_{description.unique_id}" self._attr_name = f"{description.name}" self.entity_description = description self._state = 0 From c18029b66fcdd40a26092de73ac6d82e429ff453 Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Mon, 1 Jul 2024 00:13:04 +0000 Subject: [PATCH 02/14] more testing --- custom_components/playstation_network/sensor.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/custom_components/playstation_network/sensor.py b/custom_components/playstation_network/sensor.py index f35527d..3260072 100644 --- a/custom_components/playstation_network/sensor.py +++ b/custom_components/playstation_network/sensor.py @@ -146,8 +146,8 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]: name="Trophy Level", icon="mdi:trophy", entity_registry_enabled_default=True, - has_entity_name=False, - unique_id="psn_trophies", + has_entity_name=True, + unique_id="trophies", value_fn=lambda data: data.get("trophy_summary").trophy_level, attributes_fn=get_trophy_attr, ), @@ -158,8 +158,8 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]: icon="mdi:account-circle-outline", options=["Online", "Offline", "Playing"], entity_registry_enabled_default=True, - has_entity_name=False, - unique_id="psn_status", + has_entity_name=True, + unique_id="status", value_fn=get_status, attributes_fn=get_status_attr, ), @@ -184,8 +184,9 @@ class PsnSensor(PSNEntity, SensorEntity): def __init__(self, coordinator, description: PsnSensorEntityDescription) -> None: """Initialize PSN Sensor.""" super().__init__(coordinator) - self._attr_unique_id = f"{coordinator.data.get("username")}_{description.unique_id}" - self._attr_unique_id = f"psn_{description.unique_id}" + self.entity_id = f"{DOMAIN}.{coordinator.data.get("username")}_{description.unique_id}" + #self._attr_unique_id = f"{coordinator.data.get("username")}_{description.unique_id}" + # self._attr_unique_id = f"psn_{description.unique_id}" self._attr_name = f"{description.name}" self.entity_description = description self._state = 0 From 2472ce9d4c57193567bc12a101d2486a2333626f Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Mon, 1 Jul 2024 00:14:19 +0000 Subject: [PATCH 03/14] testing --- custom_components/playstation_network/media_player.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/playstation_network/media_player.py b/custom_components/playstation_network/media_player.py index e8faf76..f0d9bad 100644 --- a/custom_components/playstation_network/media_player.py +++ b/custom_components/playstation_network/media_player.py @@ -76,8 +76,8 @@ def state(self): return MediaPlayerState.OFF @property - def unique_id(self): - return f"{self.data.get('username')}_{self.data.get('platform').get('platform')}_console" + def entity_id(self): + return f"{DOMAIN}.{self.data.get('username')}_{self.data.get('platform').get('platform')}_console" @property def name(self): From 955d5476af1ca8f8bd7aa2cf62e3598e859a88df Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Mon, 1 Jul 2024 00:22:15 +0000 Subject: [PATCH 04/14] testing --- custom_components/playstation_network/media_player.py | 7 ++++--- custom_components/playstation_network/sensor.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/custom_components/playstation_network/media_player.py b/custom_components/playstation_network/media_player.py index f0d9bad..2e58d78 100644 --- a/custom_components/playstation_network/media_player.py +++ b/custom_components/playstation_network/media_player.py @@ -50,6 +50,7 @@ def __init__(self, coordinator) -> None: super().__init__(coordinator) self.data = self.coordinator.data self._attr_has_entity_name = True + self.entity_id = f"media_player.{self.data.get('username').lower()}_{self.data.get('platform').get('platform').lower()}_console" @property def icon(self): @@ -75,9 +76,9 @@ def state(self): case _: return MediaPlayerState.OFF - @property - def entity_id(self): - return f"{DOMAIN}.{self.data.get('username')}_{self.data.get('platform').get('platform')}_console" + # @property + # def entity_id(self): + # return f"media_player.{self.data.get('username').lower()}_{self.data.get('platform').get('platform').lower()}_console" @property def name(self): diff --git a/custom_components/playstation_network/sensor.py b/custom_components/playstation_network/sensor.py index 3260072..e46b3f2 100644 --- a/custom_components/playstation_network/sensor.py +++ b/custom_components/playstation_network/sensor.py @@ -184,7 +184,7 @@ class PsnSensor(PSNEntity, SensorEntity): def __init__(self, coordinator, description: PsnSensorEntityDescription) -> None: """Initialize PSN Sensor.""" super().__init__(coordinator) - self.entity_id = f"{DOMAIN}.{coordinator.data.get("username")}_{description.unique_id}" + self.entity_id = f"sensor.{coordinator.data.get("username").lower()}_{description.unique_id}" #self._attr_unique_id = f"{coordinator.data.get("username")}_{description.unique_id}" # self._attr_unique_id = f"psn_{description.unique_id}" self._attr_name = f"{description.name}" From 18984808d4231a042c5468810bb2551e4cf5f61b Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Mon, 1 Jul 2024 18:53:17 +0000 Subject: [PATCH 05/14] testing --- .../playstation_network/__init__.py | 26 +++++++++++++++++-- .../playstation_network/media_player.py | 7 +++-- .../playstation_network/sensor.py | 4 +-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/custom_components/playstation_network/__init__.py b/custom_components/playstation_network/__init__.py index c5175bb..7db251f 100644 --- a/custom_components/playstation_network/__init__.py +++ b/custom_components/playstation_network/__init__.py @@ -4,12 +4,13 @@ import logging -import homeassistant.helpers.device_registry as dr from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_NAME, Platform -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady +from homeassistant.helpers import device_registry as dr from homeassistant.helpers import discovery +from homeassistant.helpers import entity_registry as er from psnawp_api.core.psnawp_exceptions import PSNAWPAuthenticationError from psnawp_api.psnawp import PSNAWP @@ -27,6 +28,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up PSN from a config entry.""" + await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry) + try: npsso = entry.data.get("npsso") psn = PSNAWP(npsso) @@ -95,3 +98,22 @@ def _migrate_device_identifiers( "migrate identifier '%s' to '%s'", device.identifiers, new_identifier ) dev_reg.async_update_device(device.id, new_identifiers=new_identifier) + + +@callback +def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None: + """Migrate PSN entity entries. + + - Migrates old unique ID's from old sensors and media players to the new unique ID's + """ + if entry.domain == Platform.SENSOR and entry.unique_id.endswith( + "-relative_humidity" + ): + return { + "new_unique_id": entry.unique_id.replace("-relative_humidity", "-humidity") + } + if entry.domain == Platform.MEDIA_PLAYER and entry.unique_id.endswith("-plug"): + return {"new_unique_id": entry.unique_id.replace("-plug", "-relay")} + + # No migration needed + return None diff --git a/custom_components/playstation_network/media_player.py b/custom_components/playstation_network/media_player.py index 2e58d78..e8faf76 100644 --- a/custom_components/playstation_network/media_player.py +++ b/custom_components/playstation_network/media_player.py @@ -50,7 +50,6 @@ def __init__(self, coordinator) -> None: super().__init__(coordinator) self.data = self.coordinator.data self._attr_has_entity_name = True - self.entity_id = f"media_player.{self.data.get('username').lower()}_{self.data.get('platform').get('platform').lower()}_console" @property def icon(self): @@ -76,9 +75,9 @@ def state(self): case _: return MediaPlayerState.OFF - # @property - # def entity_id(self): - # return f"media_player.{self.data.get('username').lower()}_{self.data.get('platform').get('platform').lower()}_console" + @property + def unique_id(self): + return f"{self.data.get('username')}_{self.data.get('platform').get('platform')}_console" @property def name(self): diff --git a/custom_components/playstation_network/sensor.py b/custom_components/playstation_network/sensor.py index e46b3f2..3d3775e 100644 --- a/custom_components/playstation_network/sensor.py +++ b/custom_components/playstation_network/sensor.py @@ -184,9 +184,7 @@ class PsnSensor(PSNEntity, SensorEntity): def __init__(self, coordinator, description: PsnSensorEntityDescription) -> None: """Initialize PSN Sensor.""" super().__init__(coordinator) - self.entity_id = f"sensor.{coordinator.data.get("username").lower()}_{description.unique_id}" - #self._attr_unique_id = f"{coordinator.data.get("username")}_{description.unique_id}" - # self._attr_unique_id = f"psn_{description.unique_id}" + self._attr_unique_id = f"{coordinator.data.get("username")}_{description.unique_id}" self._attr_name = f"{description.name}" self.entity_description = description self._state = 0 From 9af96f015e5671a5ce2a5a930805ca2fa6f27a6e Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Mon, 1 Jul 2024 19:41:59 +0000 Subject: [PATCH 06/14] testing --- .../playstation_network/__init__.py | 28 +++++++++++++++---- .../playstation_network/media_player.py | 2 +- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/custom_components/playstation_network/__init__.py b/custom_components/playstation_network/__init__.py index 7db251f..9421886 100644 --- a/custom_components/playstation_network/__init__.py +++ b/custom_components/playstation_network/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import Any from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_NAME, Platform @@ -106,14 +107,29 @@ def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None - Migrates old unique ID's from old sensors and media players to the new unique ID's """ - if entry.domain == Platform.SENSOR and entry.unique_id.endswith( - "-relative_humidity" - ): + coordinator = entry.entry_id[PSN_COORDINATOR] + if entry.domain == Platform.SENSOR and entry.unique_id.endswith("psn_psn_status"): + new = f"{coordinator.data.get("username").lower()}_psn_status" return { - "new_unique_id": entry.unique_id.replace("-relative_humidity", "-humidity") + "new_unique_id": entry.unique_id.replace( + "psn_psn_status", new + ) + } + + if entry.domain == Platform.SENSOR and entry.unique_id.endswith("psn_psn_trophies"): + new = f"{coordinator.data.get("username").lower()}_trophy_level" + return { + "new_unique_id": entry.unique_id.replace( + "psn_psn_trophy_level", new + ) + } + if entry.domain == Platform.MEDIA_PLAYER and entry.unique_id.endswith("_console"): + new = f"{coordinator.data.get('username')}_{coordinator.data.get('platform').get('platform').lower()}_console" + return { + "new_unique_id": entry.unique_id.replace( + "PS5_console", new + ) } - if entry.domain == Platform.MEDIA_PLAYER and entry.unique_id.endswith("-plug"): - return {"new_unique_id": entry.unique_id.replace("-plug", "-relay")} # No migration needed return None diff --git a/custom_components/playstation_network/media_player.py b/custom_components/playstation_network/media_player.py index e8faf76..1057cfa 100644 --- a/custom_components/playstation_network/media_player.py +++ b/custom_components/playstation_network/media_player.py @@ -77,7 +77,7 @@ def state(self): @property def unique_id(self): - return f"{self.data.get('username')}_{self.data.get('platform').get('platform')}_console" + return f"{self.data.get('username')}_{self.data.get('platform').get('platform').lower()}_console" @property def name(self): From db2783886b98d035b614f26d1cf1b85da1ba9532 Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Mon, 1 Jul 2024 19:59:33 +0000 Subject: [PATCH 07/14] testing --- custom_components/playstation_network/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/playstation_network/__init__.py b/custom_components/playstation_network/__init__.py index 9421886..f090e63 100644 --- a/custom_components/playstation_network/__init__.py +++ b/custom_components/playstation_network/__init__.py @@ -29,7 +29,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up PSN from a config entry.""" - await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry) + try: npsso = entry.data.get("npsso") @@ -52,6 +52,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: PSN_COORDINATOR: coordinator, PSN_API: psn, } + await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry) if entry.unique_id is None: hass.config_entries.async_update_entry(entry, unique_id=user.online_id) await coordinator.async_config_entry_first_refresh() From 6bdd05472efaebbbad42caac08fd130c85b9a3b0 Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Tue, 2 Jul 2024 11:09:29 +0000 Subject: [PATCH 08/14] testing --- .../playstation_network/__init__.py | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/custom_components/playstation_network/__init__.py b/custom_components/playstation_network/__init__.py index f090e63..2d01c50 100644 --- a/custom_components/playstation_network/__init__.py +++ b/custom_components/playstation_network/__init__.py @@ -52,13 +52,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: PSN_COORDINATOR: coordinator, PSN_API: psn, } - await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry) + if entry.unique_id is None: hass.config_entries.async_update_entry(entry, unique_id=user.online_id) await coordinator.async_config_entry_first_refresh() - _migrate_device_identifiers(hass, entry.entry_id, coordinator) - await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) - entry.async_on_unload(entry.add_update_listener(update_listener)) hass.async_create_task( discovery.async_load_platform( @@ -70,6 +67,42 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) ) + @callback + def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None: + """Migrate PSN entity entries. + + - Migrates old unique ID's from old sensors and media players to the new unique ID's + """ + if entry.domain == Platform.SENSOR and entry.unique_id.endswith("psn_psn_status"): + new = f"{coordinator.data.get("username").lower()}_psn_status" + return { + "new_unique_id": entry.unique_id.replace( + "psn_psn_status", new + ) + } + + if entry.domain == Platform.SENSOR and entry.unique_id.endswith("psn_psn_trophies"): + new = f"{coordinator.data.get("username").lower()}_trophy_level" + return { + "new_unique_id": entry.unique_id.replace( + "psn_psn_trophy_level", new + ) + } + if entry.domain == Platform.MEDIA_PLAYER and entry.unique_id.endswith("_console"): + new = f"{coordinator.data.get('username')}_{coordinator.data.get('platform').get('platform').lower()}_console" + return { + "new_unique_id": entry.unique_id.replace( + "PS5_console", new + ) + } + + # No migration needed + return None + + await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry) + _migrate_device_identifiers(hass, entry.entry_id, coordinator) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + entry.async_on_unload(entry.add_update_listener(update_listener)) return True @@ -100,37 +133,3 @@ def _migrate_device_identifiers( "migrate identifier '%s' to '%s'", device.identifiers, new_identifier ) dev_reg.async_update_device(device.id, new_identifiers=new_identifier) - - -@callback -def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None: - """Migrate PSN entity entries. - - - Migrates old unique ID's from old sensors and media players to the new unique ID's - """ - coordinator = entry.entry_id[PSN_COORDINATOR] - if entry.domain == Platform.SENSOR and entry.unique_id.endswith("psn_psn_status"): - new = f"{coordinator.data.get("username").lower()}_psn_status" - return { - "new_unique_id": entry.unique_id.replace( - "psn_psn_status", new - ) - } - - if entry.domain == Platform.SENSOR and entry.unique_id.endswith("psn_psn_trophies"): - new = f"{coordinator.data.get("username").lower()}_trophy_level" - return { - "new_unique_id": entry.unique_id.replace( - "psn_psn_trophy_level", new - ) - } - if entry.domain == Platform.MEDIA_PLAYER and entry.unique_id.endswith("_console"): - new = f"{coordinator.data.get('username')}_{coordinator.data.get('platform').get('platform').lower()}_console" - return { - "new_unique_id": entry.unique_id.replace( - "PS5_console", new - ) - } - - # No migration needed - return None From 202d9ffb217004af93629178bf7b32e5686a8599 Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Tue, 2 Jul 2024 11:22:27 +0000 Subject: [PATCH 09/14] testing --- custom_components/playstation_network/__init__.py | 8 ++++---- custom_components/playstation_network/sensor.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/custom_components/playstation_network/__init__.py b/custom_components/playstation_network/__init__.py index 2d01c50..625fd88 100644 --- a/custom_components/playstation_network/__init__.py +++ b/custom_components/playstation_network/__init__.py @@ -82,14 +82,14 @@ def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None } if entry.domain == Platform.SENSOR and entry.unique_id.endswith("psn_psn_trophies"): - new = f"{coordinator.data.get("username").lower()}_trophy_level" + new = f"{coordinator.data.get("username").lower()}_psn_trophy_level" return { "new_unique_id": entry.unique_id.replace( - "psn_psn_trophy_level", new + "psn_psn_trophies", new ) } - if entry.domain == Platform.MEDIA_PLAYER and entry.unique_id.endswith("_console"): - new = f"{coordinator.data.get('username')}_{coordinator.data.get('platform').get('platform').lower()}_console" + if entry.domain == Platform.MEDIA_PLAYER and entry.unique_id is "PS5_console": + new = f"{coordinator.data.get('username').lower()}_{coordinator.data.get('platform').get('platform').lower()}_console" return { "new_unique_id": entry.unique_id.replace( "PS5_console", new diff --git a/custom_components/playstation_network/sensor.py b/custom_components/playstation_network/sensor.py index 3d3775e..d9d1ef0 100644 --- a/custom_components/playstation_network/sensor.py +++ b/custom_components/playstation_network/sensor.py @@ -147,7 +147,7 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]: icon="mdi:trophy", entity_registry_enabled_default=True, has_entity_name=True, - unique_id="trophies", + unique_id="psn_trophy_level", value_fn=lambda data: data.get("trophy_summary").trophy_level, attributes_fn=get_trophy_attr, ), @@ -159,7 +159,7 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]: options=["Online", "Offline", "Playing"], entity_registry_enabled_default=True, has_entity_name=True, - unique_id="status", + unique_id="psn_status", value_fn=get_status, attributes_fn=get_status_attr, ), @@ -184,7 +184,7 @@ class PsnSensor(PSNEntity, SensorEntity): def __init__(self, coordinator, description: PsnSensorEntityDescription) -> None: """Initialize PSN Sensor.""" super().__init__(coordinator) - self._attr_unique_id = f"{coordinator.data.get("username")}_{description.unique_id}" + self._attr_unique_id = f"{coordinator.data.get("username").lower()}_{description.unique_id}" self._attr_name = f"{description.name}" self.entity_description = description self._state = 0 From 22fd4fea25464a18bdde8667e598a4825561baaa Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Tue, 2 Jul 2024 11:37:17 +0000 Subject: [PATCH 10/14] testing --- custom_components/playstation_network/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/playstation_network/__init__.py b/custom_components/playstation_network/__init__.py index 625fd88..ffb2225 100644 --- a/custom_components/playstation_network/__init__.py +++ b/custom_components/playstation_network/__init__.py @@ -88,7 +88,7 @@ def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None "psn_psn_trophies", new ) } - if entry.domain == Platform.MEDIA_PLAYER and entry.unique_id is "PS5_console": + if entry.domain == Platform.MEDIA_PLAYER and entry.unique_id == "PS5_console": new = f"{coordinator.data.get('username').lower()}_{coordinator.data.get('platform').get('platform').lower()}_console" return { "new_unique_id": entry.unique_id.replace( From a893bf7c83c1dd915958372e1cf17d2cf91cb4ef Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Tue, 2 Jul 2024 11:43:24 +0000 Subject: [PATCH 11/14] testing --- custom_components/playstation_network/media_player.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/playstation_network/media_player.py b/custom_components/playstation_network/media_player.py index 1057cfa..a0941f7 100644 --- a/custom_components/playstation_network/media_player.py +++ b/custom_components/playstation_network/media_player.py @@ -77,7 +77,7 @@ def state(self): @property def unique_id(self): - return f"{self.data.get('username')}_{self.data.get('platform').get('platform').lower()}_console" + return f"{self.data.get('username').lower()}_{self.data.get('platform').get('platform').lower()}_console" @property def name(self): From 49bcca6009c59b0cc07cc40a69e8aa8e525dd251 Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Tue, 2 Jul 2024 14:15:47 +0000 Subject: [PATCH 12/14] migrate entries and device support --- .../playstation_network/__init__.py | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/custom_components/playstation_network/__init__.py b/custom_components/playstation_network/__init__.py index ffb2225..2f936ef 100644 --- a/custom_components/playstation_network/__init__.py +++ b/custom_components/playstation_network/__init__.py @@ -29,8 +29,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up PSN from a config entry.""" - - try: npsso = entry.data.get("npsso") psn = PSNAWP(npsso) @@ -73,7 +71,7 @@ def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None - Migrates old unique ID's from old sensors and media players to the new unique ID's """ - if entry.domain == Platform.SENSOR and entry.unique_id.endswith("psn_psn_status"): + if entry.domain == Platform.SENSOR and entry.unique_id == "psn_psn_status": new = f"{coordinator.data.get("username").lower()}_psn_status" return { "new_unique_id": entry.unique_id.replace( @@ -81,7 +79,7 @@ def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None ) } - if entry.domain == Platform.SENSOR and entry.unique_id.endswith("psn_psn_trophies"): + if entry.domain == Platform.SENSOR and entry.unique_id == "psn_psn_trophies": new = f"{coordinator.data.get("username").lower()}_psn_trophy_level" return { "new_unique_id": entry.unique_id.replace( @@ -99,8 +97,15 @@ def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None # No migration needed return None - await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry) - _migrate_device_identifiers(hass, entry.entry_id, coordinator) + # Migrate unique ID -- Make the ID actually Unique. + # Migrate Device Name -- Make the device name match the psn username + # We can remove this logic after a reasonable period of time has passed. + if entry.version == 1: + await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry) + _migrate_device_identifiers(hass, entry.entry_id, coordinator) + hass.config_entries.async_update_entry(entry, version=2) + + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) entry.async_on_unload(entry.add_update_listener(update_listener)) return True @@ -118,6 +123,9 @@ async def update_listener(hass: HomeAssistant, entry: ConfigEntry): """Update Listener.""" await hass.config_entries.async_reload(entry.entry_id) +async def async_migrate_entry(hass: HomeAssistant, self): + """Migrate Entry Support""" + return True def _migrate_device_identifiers( hass: HomeAssistant, entry_id: str, coordinator From 307f58c55d0ae2bf8c198fe9540303ed5e4fc512 Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Tue, 2 Jul 2024 14:16:11 +0000 Subject: [PATCH 13/14] skip add if there is no associcated console --- custom_components/playstation_network/media_player.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/custom_components/playstation_network/media_player.py b/custom_components/playstation_network/media_player.py index a0941f7..24c9ea0 100644 --- a/custom_components/playstation_network/media_player.py +++ b/custom_components/playstation_network/media_player.py @@ -36,7 +36,15 @@ async def async_setup_entry( ) -> None: """Entity Setup""" coordinator = hass.data[DOMAIN][config_entry.entry_id][PSN_COORDINATOR] - await coordinator.async_config_entry_first_refresh() + + if coordinator.data.get("platform").get("platform") is None: + username = coordinator.data.get("username") + _LOGGER.warning( + "No console found associated with account: %s. -- Skipping creation of media player", + username, + ) + return + async_add_entities([MediaPlayer(coordinator)]) From 38f960d846ad7be1719fd9d0244f51d56f55a6c9 Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Tue, 2 Jul 2024 14:16:33 +0000 Subject: [PATCH 14/14] do not request coordinator refresh --- custom_components/playstation_network/sensor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/custom_components/playstation_network/sensor.py b/custom_components/playstation_network/sensor.py index d9d1ef0..17bfc96 100644 --- a/custom_components/playstation_network/sensor.py +++ b/custom_components/playstation_network/sensor.py @@ -169,7 +169,6 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]: async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entities): """Add sensors for passed config_entry in HA.""" coordinator = hass.data[DOMAIN][config_entry.entry_id][PSN_COORDINATOR] - await coordinator.async_config_entry_first_refresh() async_add_entities( PsnSensor(coordinator, description) for description in PSN_SENSOR