diff --git a/custom_components/person_location/const.py b/custom_components/person_location/const.py index b1abddb..082ce55 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.05.31" +VERSION = "2024.06.01" # Constants: METERS_PER_KM = 1000 diff --git a/custom_components/person_location/manifest.json b/custom_components/person_location/manifest.json index 2e5fae4..91cbc57 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.05.31" + "version": "2024.06.01" } \ No newline at end of file diff --git a/custom_components/person_location/process_trigger.py b/custom_components/person_location/process_trigger.py index 3a7a0e0..9e3caeb 100644 --- a/custom_components/person_location/process_trigger.py +++ b/custom_components/person_location/process_trigger.py @@ -237,9 +237,8 @@ def handle_process_trigger(call): else: triggerSourceType = "other" # person entities do not indicate the source type, dig deeper: - if "source" in trigger.attributes: - triggerSource = trigger.attributes["source"] - triggerSourceObject = pli.hass.states.get(triggerSource) + if "source" in trigger.attributes and '.' in trigger.attributes["source"]: + triggerSourceObject = pli.hass.states.get(trigger.attributes["source"]) if triggerSourceObject is not None: if ATTR_SOURCE_TYPE in triggerSourceObject.attributes: triggerSourceType = triggerSourceObject.attributes[ @@ -567,7 +566,7 @@ def handle_process_trigger(call): # Call service to "reverse geocode" the location. # For devices at Home, this will be forced to run - # once at startup or on arrival. + # at startup or on arrival. force_update = (newTargetState in ["Home", "Just Arrived"] @@ -576,19 +575,17 @@ def handle_process_trigger(call): "extended away", "just left"] ) - if ( - newTargetState != "Home" - or pli.attributes["startup"] - or force_update - ): - service_data = { - "entity_id": target.entity_id, - "friendly_name_template": pli.configuration[CONF_FRIENDLY_NAME_TEMPLATE], - "force_update": force_update, - } - pli.hass.services.call( - DOMAIN, "reverse_geocode", service_data, False - ) + if pli.attributes["startup"]: + force_update = True + + service_data = { + "entity_id": target.entity_id, + "friendly_name_template": pli.configuration[CONF_FRIENDLY_NAME_TEMPLATE], + "force_update": force_update, + } + pli.hass.services.call( + DOMAIN, "reverse_geocode", service_data, False + ) _LOGGER.debug( "(%s) TARGET_LOCK release...", diff --git a/custom_components/person_location/reverse_geocode.py b/custom_components/person_location/reverse_geocode.py index b2b9739..142f1b2 100644 --- a/custom_components/person_location/reverse_geocode.py +++ b/custom_components/person_location/reverse_geocode.py @@ -924,68 +924,79 @@ def handle_reverse_geocode(call): new_longitude, ) - if template != "NONE": + if template != "NONE": - # Determine friendly_name_location component of friendly_name: + # Determine friendly_name_location component of friendly_name: - if target.attributes["reported_state"] in [ - "Away", - STATE_ON, - STATE_NOT_HOME, - ]: - friendly_name_location = "is Away" - else: - friendly_name_location = f"is at {target.attributes["reported_state"]}" - - reportedZone = target.attributes["zone"] - zoneStateObject = pli.hass.states.get("zone." + reportedZone) - if (zoneStateObject is None - or reportedZone.lower().endswith("stationary")): - # Eliminate stray zone names: - friendly_name_location = "is Away" - else: - zoneAttributesObject \ - = zoneStateObject.attributes.copy() - if "friendly_name" in zoneAttributesObject: - friendly_name_location = "is at " \ - + zoneAttributesObject["friendly_name"] - - _LOGGER.debug( - "(%s) friendly_name_location = %s", - target.entity_id, - friendly_name_location, - ) + if target.attributes["reported_state"] in [ + "Away", + STATE_ON, + STATE_NOT_HOME, + ]: + friendly_name_location = "is Away" + else: + friendly_name_location = f"is at {target.attributes["reported_state"]}" + + reportedZone = target.attributes["zone"] + zoneStateObject = pli.hass.states.get("zone." + reportedZone) + if (zoneStateObject is None + or reportedZone.lower().endswith("stationary")): + # Eliminate stray zone names: + friendly_name_location = "is Away" + else: + zoneAttributesObject \ + = zoneStateObject.attributes.copy() + if "friendly_name" in zoneAttributesObject: + friendly_name_location = "is at " \ + + zoneAttributesObject["friendly_name"] + + _LOGGER.debug( + "(%s) friendly_name_location = %s", + target.entity_id, + friendly_name_location, + ) - if (friendly_name_location == "is Away"): - # " is in "; add new locality - friendly_name_location = f"is in {target.attributes['locality']}" - - # Format new friendly_name using the supplied template: - - if "source" in target.attributes: - sourceObject = pli.hass.states.get(target.attributes["source"]) - if sourceObject is not None and "source" in sourceObject.attributes: - # Find the source for a person entity: - sourceObject = pli.hass.states.get(sourceObject.attributes["source"]) - else: - sourceObject = None - - friendly_name_variables = { - "friendly_name_location": friendly_name_location, - "person_name": target.attributes['person_name'], - "source": { "attributes": sourceObject.attributes }, - "target": { "attributes": target.attributes }, - } - _LOGGER.debug(f"friendly_name_variables = {friendly_name_variables}") - - try: - target.attributes["friendly_name"] \ - = Template(pli.configuration[CONF_FRIENDLY_NAME_TEMPLATE]) \ - .render(**friendly_name_variables) \ - .replace('()','') \ - .replace(' ',' ') - except TemplateError as err: - _LOGGER.error("Error parsing friendly_name_template: %s", err) + if (friendly_name_location == "is Away"): + # " is in "; add new locality + friendly_name_location = f"is in {target.attributes['locality']}" + + # Format new friendly_name using the supplied template: + + if "source" in target.attributes and '.' in target.attributes["source"]: + sourceEntity = target.attributes["source"] + sourceObject = pli.hass.states.get(sourceEntity) + if sourceObject is not None and "source" in sourceObject.attributes \ + and '.' in target.attributes["source"]: + # Find the source for a person entity: + sourceEntity = sourceObject.attributes["source"] + sourceObject = pli.hass.states.get(sourceEntity) + else: + sourceObject = target + + friendly_name_variables = { + "friendly_name_location": friendly_name_location, + "person_name": target.attributes['person_name'], + "source": { + "entity_id": sourceEntity, + "state": sourceObject.state, + "attributes": sourceObject.attributes, + }, + "target": { + "entity_id": target.entity_id, + "state": target.state, + "attributes": target.attributes, + }, + } + _LOGGER.debug(f"friendly_name_variables = {friendly_name_variables}") + + try: + target.attributes["friendly_name"] \ + = Template(pli.configuration[CONF_FRIENDLY_NAME_TEMPLATE]) \ + .render(**friendly_name_variables) \ + .replace('()','') \ + .replace(' ',' ') + except TemplateError as err: + _LOGGER.error("Error parsing friendly_name_template: %s", err) target.set_state()