Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add battery_charging & last_seen attributes to GPSLogger #108118

Closed
wants to merge 6 commits into from

Conversation

pnbruckner
Copy link
Contributor

@pnbruckner pnbruckner commented Jan 15, 2024

Proposed change

The GPSLogger app can provide battery charging status and an update timestamp. Allow configuring these in the Android app and, if present in the received webhook packet, add them as attributes to the GPSLogger tracker entities.

Note that it is possible for packets from the Android app to be received out of order due to network delays, etc. If this happens, the device_tracker entity will move "backwards" (because it will be updated with old GPS data.) This can be prevented by utilizing the last_seen timestamp. If a packet is received with a last_seen value that is before the previously received packet's last_seen value, skip the newly received packet (and log a debug message to document this has happened.) This change implements that safeguard (which the Google Maps integration also implements, and the Life360 integration did before it was removed.)

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request: TBD

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

ATTR_ACTIVITY: attr.get(ATTR_ACTIVITY),
ATTR_ALTITUDE: attr.get(ATTR_ALTITUDE),
ATTR_BATTERY_CHARGING: attr.get(ATTR_BATTERY_CHARGING),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Battery charging should be a binary sensor entity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't care so much about this piece of data, but why make this special when all other miscellaneous data are attributes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's our current standard to put all measurements that are relevant on their own as separate sensor entities.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, does that mean I need to pull out all the others, too, and make them separate sensor entities? At a minimum, shouldn't that also apply to activity, altitude, battery_level, direction & speed? And can I assume, since you did not reply to my comments about last_seen, it's ok to leave it as an attribute on the device_tracker entity?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing attributes don't need to be moved at this time, since that would be a breaking change.

We don't allow new additions of attributes that should be sensors.

Last seen is not allowed as an attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then there's no point.

ATTR_DIRECTION: attr.get(ATTR_DIRECTION),
ATTR_LAST_SEEN: last_seen,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last seen should be a timestamp sensor entity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is much easier to compare multiple device_tracker entities for a single physical device (e.g., when using multiple presence detection integrations) when last_seen is an attribute, not a separate entity. And there is already precendence for a timestamp like this to be an attribute. E.g., last_seen in (the now defunct) Life360, Google Maps' last_seen, Tile's last_timestamp, and possibly even asuswrt's last_time_reachable. It is crucial for my custom composite integration that many, many people use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I understand splitting out miscellaneous data into separate sensors as opposed to the original tendency to keep adding attributes to an existing entity. I have no problem with that in general.

However, in this case, I would argue that this is a perfect example of what an attribute should be used for. It is adding information about the main state (latitude, longitude & accuracy) that makes using that data more accurate. I.e., it describes when the data actually changed (as opposed to the last_updated field, that just indicates when the data was received by HA, which can be, and often is, very different.)

@home-assistant home-assistant bot marked this pull request as draft January 15, 2024 20:05
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@MartinHjelmare MartinHjelmare changed the title Add battery_charging & last_seet attributes to GPSLogger Add battery_charging & last_seen attributes to GPSLogger Jan 15, 2024
Accuracy should be 0 instead of None.

Handle case when restored without last_seen attribute.
@pnbruckner pnbruckner marked this pull request as ready for review January 16, 2024 15:18
@MartinHjelmare MartinHjelmare marked this pull request as draft January 16, 2024 15:25
@pnbruckner pnbruckner marked this pull request as ready for review January 16, 2024 16:07
@MartinHjelmare MartinHjelmare marked this pull request as draft January 16, 2024 23:29
@pnbruckner pnbruckner closed this Jan 17, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jan 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants