Skip to content

Commit

Permalink
Merge pull request #27 from JackJPowell/about-me-sensor
Browse files Browse the repository at this point in the history
About Me as top level sensor
  • Loading branch information
JackJPowell authored Dec 18, 2024
2 parents 68719b6 + 655367e commit 9315f37
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
20 changes: 19 additions & 1 deletion custom_components/playstation_network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion custom_components/playstation_network/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
20 changes: 10 additions & 10 deletions custom_components/playstation_network/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...] = (
Expand All @@ -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,
Expand Down

0 comments on commit 9315f37

Please sign in to comment.