From 9b34cfe1fff40cf2de9078265a64ff4c22d6a2c5 Mon Sep 17 00:00:00 2001 From: RodPayne Date: Mon, 3 Jun 2024 21:13:37 -0600 Subject: [PATCH] Refresh friendly_name in all location sensors after template update. #17 --- custom_components/person_location/__init__.py | 29 ++++++++++++++++++- custom_components/person_location/const.py | 2 +- .../person_location/manifest.json | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/custom_components/person_location/__init__.py b/custom_components/person_location/__init__.py index 43a000a..fdce86a 100644 --- a/custom_components/person_location/__init__.py +++ b/custom_components/person_location/__init__.py @@ -26,6 +26,7 @@ DATA_ASYNC_SETUP_ENTRY, DATA_CONFIG_ENTRY, DATA_CONFIGURATION, + DATA_ENTITY_INFO, DATA_UNDO_STATE_LISTENER, DATA_UNDO_UPDATE_LISTENER, DEFAULT_FRIENDLY_NAME_TEMPLATE, @@ -88,6 +89,9 @@ async def _async_setup_entry(hass, entry): entry.options, ) + friendly_name_template_changed = ( + entry.options["friendly_name_template"] != pli.configuration["friendly_name_template"] + ) pli.configuration.update(entry.data) pli.configuration.update(entry.options) @@ -95,6 +99,30 @@ async def _async_setup_entry(hass, entry): await hass.async_add_executor_job(_listen_for_configured_entities) + if friendly_name_template_changed: + + # Update the friendly_name for all enties that have been geocoded: + + entity_info = hass.data[DOMAIN][DATA_ENTITY_INFO] + + for sensor in entity_info: + + if ( + "geocode_count" in entity_info[sensor] + and entity_info[sensor]["geocode_count"] != 0 + ): + + _LOGGER.debug(f"sensor to be updated = {sensor}") + service_data = { + "entity_id": sensor, + "friendly_name_template": pli.configuration[CONF_FRIENDLY_NAME_TEMPLATE], + "force_update": False, + } + await pli.hass.services.async_call( + DOMAIN, "reverse_geocode", service_data, False + ) + + _LOGGER.debug("[_async_setup_entry] === Return ===") return True @@ -224,7 +252,6 @@ async def async_options_update_listener(hass, entry): async def async_unload_entry(hass, entry): """Unload a config entry.""" - _LOGGER.debug("===== async_unload_entry") if DATA_UNDO_UPDATE_LISTENER in hass.data[DOMAIN]: hass.data[DOMAIN][DATA_UNDO_UPDATE_LISTENER]() diff --git a/custom_components/person_location/const.py b/custom_components/person_location/const.py index 082ce55..a266d47 100644 --- a/custom_components/person_location/const.py +++ b/custom_components/person_location/const.py @@ -29,7 +29,7 @@ API_STATE_OBJECT = DOMAIN + "." + DOMAIN + "_integration" INTEGRATION_NAME = "Person Location" ISSUE_URL = "https://github.com/rodpayne/home-assistant_person_location/issues" -VERSION = "2024.06.01" +VERSION = "2024.06.03" # Constants: METERS_PER_KM = 1000 diff --git a/custom_components/person_location/manifest.json b/custom_components/person_location/manifest.json index 91cbc57..bed15af 100644 --- a/custom_components/person_location/manifest.json +++ b/custom_components/person_location/manifest.json @@ -20,5 +20,5 @@ "iot_class": "calculated", "issue_tracker": "https://github.com/rodpayne/home-assistant_person_location/issues", "requirements": [], - "version": "2024.06.01" + "version": "2024.06.03" } \ No newline at end of file