Skip to content

Commit

Permalink
Don't follow source: GPS as an entity.
Browse files Browse the repository at this point in the history
Also, call reverse_geocode to get friendly_name updated whenever trigger accepted.
  • Loading branch information
rodpayne committed Jun 1, 2024
1 parent 24dad9f commit 1426b63
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 78 deletions.
2 changes: 1 addition & 1 deletion custom_components/person_location/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion custom_components/person_location/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
31 changes: 14 additions & 17 deletions custom_components/person_location/process_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down Expand Up @@ -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"]
Expand All @@ -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...",
Expand Down
129 changes: 70 additions & 59 deletions custom_components/person_location/reverse_geocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
# "<identity> is in <locality>"; 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"):
# "<identity> is in <locality>"; 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()

Expand Down

0 comments on commit 1426b63

Please sign in to comment.