From 655367e8c9f549e76ecd6c620f4aa4a54f5d48b4 Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Wed, 18 Dec 2024 22:25:39 +0000 Subject: [PATCH] CAbout Me as top level sensor --- .../playstation_network/__init__.py | 20 ++++++++++++++++++- .../playstation_network/manifest.json | 2 +- .../playstation_network/sensor.py | 20 +++++++++---------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/custom_components/playstation_network/__init__.py b/custom_components/playstation_network/__init__.py index 2426dca..ea64f5a 100644 --- a/custom_components/playstation_network/__init__.py +++ b/custom_components/playstation_network/__init__.py @@ -15,7 +15,7 @@ from psnawp_api.core.psnawp_exceptions import PSNAWPAuthenticationError from psnawp_api.psnawp import PSNAWP -from .const import DOMAIN, PSN_API, PSN_COORDINATOR +from .const import DOMAIN, PSN_API, PSN_COORDINATOR, CONF_EXPOSE_ATTRIBUTES_AS_ENTITIES from .coordinator import PsnCoordinator PLATFORMS: list[Platform] = [ @@ -95,6 +95,10 @@ def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None _migrate_device_identifiers(hass, entry.entry_id, coordinator) hass.config_entries.async_update_entry(entry, version=2) + if entry.version < 3: + _remove_option_sensor(hass, entry, coordinator) + hass.config_entries.async_update_entry(entry, version=3) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) entry.async_on_unload(entry.add_update_listener(update_listener)) return True @@ -132,3 +136,17 @@ def _migrate_device_identifiers( "migrate identifier '%s' to '%s'", device.identifiers, new_identifier ) dev_reg.async_update_device(device.id, new_identifiers=new_identifier) + + +def _remove_option_sensor( + hass: HomeAssistant, entry: ConfigEntry, coordinator: PsnCoordinator +) -> None: + if entry.options.get(CONF_EXPOSE_ATTRIBUTES_AS_ENTITIES) is True: + entity_registry = er.async_get(hass) + entity_id = entity_registry.async_get_entity_id( + "sensor", + DOMAIN, + f"{coordinator.data.get("username").lower()}_psn_about_me_attr", + ) + if entity_id: + entity_registry.async_remove(entity_id) diff --git a/custom_components/playstation_network/manifest.json b/custom_components/playstation_network/manifest.json index b85cc16..5f85359 100644 --- a/custom_components/playstation_network/manifest.json +++ b/custom_components/playstation_network/manifest.json @@ -11,5 +11,5 @@ "issue_tracker": "https://github.com/JackJPowell/hass-psn/issues", "requirements": ["PSNAWP-HA==2.2.2"], "ssdp": [], - "version": "0.7.0" + "version": "0.7.1" } diff --git a/custom_components/playstation_network/sensor.py b/custom_components/playstation_network/sensor.py index 29b103e..bdfe938 100644 --- a/custom_components/playstation_network/sensor.py +++ b/custom_components/playstation_network/sensor.py @@ -185,6 +185,16 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]: unique_id="has_playstation_plus", value_fn=get_ps_plus_status, ), + PsnSensorEntityDescription( + key="about_me", + native_unit_of_measurement=None, + name="About Me", + icon="mdi:comment-text-outline", + entity_registry_enabled_default=True, + has_entity_name=True, + unique_id="about_me", + value_fn=lambda data: data.get("profile").get("aboutMe"), + ), ) PSN_ADDITIONAL_SENSOR: tuple[PsnSensorEntityDescription, ...] = ( @@ -198,16 +208,6 @@ def get_trophy_attr(coordinator_data: any) -> dict[str, str]: unique_id="psn_title_name_attr", value_fn=lambda data: data.get("name"), ), - PsnSensorEntityDescription( - key="about_me", - native_unit_of_measurement=None, - name="About Me", - icon="mdi:information-outline", - entity_registry_enabled_default=True, - has_entity_name=True, - unique_id="psn_about_me_attr", - value_fn=lambda data: data.get("about_me"), - ), PsnSensorEntityDescription( key="platform", native_unit_of_measurement=None,