Skip to content

Commit

Permalink
Initial coding for CONF_SHOW_ZONE_WHEN_AWAY.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodpayne committed Jun 4, 2024
1 parent 9b34cfe commit 0173410
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ When a person is detected as moving between `Home` and `Away`, instead of going

![Person State Diagram](docs/images/PersonHomeState.png)

*Inspired by <https://philhawthorne.com/making-home-assistants-presence-detection-not-so-binary/>*
*Inspired by <https://philhawthorne.com/making-home-assistants-presence-detection-not-so-binary/>*

If `CONF_SHOW_ZONE_WHEN_AWAY`, then `<Zone>` is reported instead of `Away`.

### **Reverse geocode the location and make distance calculations**
The custom integration supplies a service to reverse geocode the location (making it human readable) using `Open Street Map`, `MapQuest`, or `Google Maps` and calculate the distance from home (miles and minutes) using `WazeRouteCalculator`.
Expand Down
2 changes: 1 addition & 1 deletion custom_components/person_location/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _handle_device_tracker_state_change(entity_id, old_state, new_state):
_LOGGER.debug("[_handle_device_tracker_state_change]" + " === Start ===")

_LOGGER.debug("[_handle_device_tracker_state_change]" + " (%s) " % (entity_id))
if hasattr('old_state', 'state'):
if hasattr(old_state, 'state'):
fromState = old_state.state
else:
fromState = 'unknown'
Expand Down
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.06.03"
VERSION = "2024.06.04"

# 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.06.03"
"version": "2024.06.04"
}
49 changes: 36 additions & 13 deletions custom_components/person_location/process_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
CONF_HOURS_EXTENDED_AWAY,
CONF_MINUTES_JUST_ARRIVED,
CONF_MINUTES_JUST_LEFT,
CONF_SHOW_ZONE_WHEN_AWAY,
DOMAIN,
PERSON_LOCATION_ENTITY,
TARGET_LOCK,
Expand Down Expand Up @@ -105,13 +106,27 @@ def handle_delayed_state_change(
target.attributes[ATTR_COMPASS_BEARING] = 0
target.attributes[ATTR_DIRECTION] = "home"
elif to_state == "Away":
change_state_later(
target.entity_id,
"Away",
"Extended Away",
(pli.configuration[CONF_HOURS_EXTENDED_AWAY] * 60),
)
pass
if pli.configuration[CONF_SHOW_ZONE_WHEN_AWAY]:
reportedZone = target.attributes["zone"]
zoneStateObject = pli.hass.states.get("zone." + reportedZone)
if (zoneStateObject is None
or reportedZone.lower().endswith("stationary")):
_LOGGER.debug(f"Skipping use of zone {reportedZone} for Away state")
pass
else:
zoneAttributesObject \
= zoneStateObject.attributes.copy()
if "friendly_name" in zoneAttributesObject:
target.state = zoneAttributesObject["friendly_name"]
if pli.configuration[
CONF_HOURS_EXTENDED_AWAY] != 0:
change_state_later(
target.entity_id,
target.state,
"Extended Away",
(pli.configuration[CONF_HOURS_EXTENDED_AWAY] * 60),
)
pass
elif to_state == "Extended Away":
pass

Expand Down Expand Up @@ -150,11 +165,6 @@ def utc2local_naive(utc_dt):
if str(local)[-6] == "-" or str(local)[-6] == "+":
local = str(local)[:-6] # remove offset to make it offset-naive
local = datetime.strptime(local, "%Y-%m-%d %H:%M:%S.%f")
_LOGGER.debug(
"[utc2local_naive] utc = %s; local = %s",
utc_dt,
local,
)
return local

def handle_process_trigger(call):
Expand All @@ -171,6 +181,7 @@ def handle_process_trigger(call):
Attributes:
- selected attributes from the triggered device tracker
- state: "Just Arrived", "Home", "Just Left", "Away", or "Extended Away"
If CONF_SHOW_ZONE_WHEN_AWAY, then <Zone> is reported instead of "Away".
- person_name: <personName>
- source: entity_id of the device tracker that triggered the automation
- reported_state: the state reported by device tracker = "Home", "Away", or <zone>
Expand Down Expand Up @@ -555,11 +566,23 @@ def handle_process_trigger(call):
call_rest_command_service(
trigger.personName, newTargetState
)
if newTargetState == "Away" and pli.configuration[CONF_SHOW_ZONE_WHEN_AWAY]:
# Get the state from the zone friendly_name:
if (zoneStateObject is None
or reportedZone.lower().endswith("stationary")):
# Skip stray zone names:
pass
else:
zoneAttributesObject \
= zoneStateObject.attributes.copy()
if "friendly_name" in zoneAttributesObject:
newTargetState = zoneAttributesObject["friendly_name"]

target.state = newTargetState

if ha_just_started:
target.attributes[ATTR_BREAD_CRUMBS] = newTargetState

target.state = newTargetState
target.attributes["version"] = f"{DOMAIN} {VERSION}"

target.set_state()
Expand Down

0 comments on commit 0173410

Please sign in to comment.