Google Maps location sharing

This is great! I have everything working as intended. The best thing is that my wife doesn’t need to install some other piece of software on her phone. She was already sharing her location with me in Google Maps, so this was an easy sell :slight_smile:

My only complaint is that the device_tracker entities show up as “device_tracker.google_maps_01234567890123456789” and you can’t change it to some more intelligible value; the ID format name just shows up again on restart. It would be cool if it could key off an ID attribute or something and you could name the entity “device_tracker.steves_phone” or something more readable.

Great work!

1 Like

Hi Steve, glad you like it! You can customize the tracker following this https://home-assistant.io/docs/configuration/customizing-devices/. Keep in mind that there is an issue with some people, including me where the GPS signal pops in and out of the zone in HA so take the information accuracy with a grain of salt.

The library is in public pypi so a simple pip install locationsharinglib will probably do.

Looks like it installed itself at some point… maybe it just didn’t work the first time as the log is now showing this:

2018-03-03 09:57:13 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform google_maps
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/components/device_tracker/__init__.py", line 169, in async_setup_platform
    disc_info)
  File "/usr/lib/python3.6/asyncio/futures.py", line 332, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.6/asyncio/tasks.py", line 250, in _wakeup
    future.result()
  File "/usr/lib/python3.6/asyncio/futures.py", line 245, in result
    raise self._exception
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/device_tracker/google_maps.py", line 33, in setup_scanner
    scanner = GoogleMapsScanner(hass, config, see)
  File "/config/custom_components/device_tracker/google_maps.py", line 53, in __init__
    self._update_info()
  File "/config/custom_components/device_tracker/google_maps.py", line 65, in _update_info
    for person in self.service.get_all_people():
  File "/config/deps/lib/python3.6/site-packages/locationsharinglib/locationsharinglib.py", line 287, in get_all_people
    return self._get_data()
  File "/config/deps/lib/python3.6/site-packages/cachetools/__init__.py", line 46, in wrapper
    v = func(*args, **kwargs)
  File "/config/deps/lib/python3.6/site-packages/locationsharinglib/locationsharinglib.py", line 278, in _get_data
    output = json.loads(response.text.split("'")[1])
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Unterminated string starting at: line 4 column 19 (char 262)

Should the google_maps_location_sharing.conf have legible JSON in it? When I try and open it up I can see some semblance of JSON inside but it looks to be encoded strangely or something. I ran the generation of the conf file from a windows python terminal if that makes a difference.

The contents of that file is not json and the conf suffix is a bit unfortunate. That file holds the cookies of the connection pickled(python thing) so it is not supposed to be human readable. Please read a few comments up on how to initially create that file from your workstation in order to answer the security prompts that Google sends you. Once you have a complete connection move the cookie file to your ha installation. Good luck!

I have followed those instructions and getting the error I posted above in my home assistant log, I only poked into the conf file as I saw it was a JSON decode error.

I actually just tried the following snippet from the library docs in the same console I am using to generate the conf and got the same JSONDecodeError from above so it must surely be something wrong before the home asssistant steps.

username="myusername"
password="mypassword"
from locationsharinglib import Service
service = Service(username,password)
for person in service.get_all_people():
    print(person)

Just did some basic debugging through the library and I think I have found the problem, it’s not immediately obvious how to fix it but I will see if I can figure something out.

In line 278:

output = json.loads(response.text.split("'")[1])

You are splitting by the ’ character, and I live in a place called St. John’s. Note the single quote in the city name! So the split is breaking the JSON in the middle of an object and the decode obviously fails on an invalid json string.

Could very well be. Make your local copy of the library into and if it fixes it I will incorporate it into the library.
output = json.loads(response.text.split("'", 1)[1])

That worked locally and in home assistant! Location being tracked now, thanks for the help!

I will fix the library. Thanks for reporting!

I think you misunderstood me. I know I can change the display name of the entity. I’ve done that. I want to be able to change the actual entity ID and have the numeric ID be an attribute of the tracker instead.

In that case I am guessing @michaelarnauts is accepting MRs for the component part :slight_smile:

It seems that the accuracy problem is service related. After two days of intensive usage, the issue appears when reaching a zone, and the GPS accuracy probably is bad enough for the location to flicker. I installed the last liblocationsharing version, to no avail. Have you guys found the accuracy field ?

Nah, i actually guessed a field and I was wrong. Sadly I am very busy to monitor this to figure out which one it is. If anyone has more time locationsharinglib is accepting MRs happily! :slight_smile:

If it is

data[1][3]

It seems reasonable to me. Check these logs:

[None, -64.2038145, -31.4409447], 1520435741480, 22,
[None, [None, -64.1830157, -31.4198915], 1520435055011, 21, '
[None, [None, -64.2057561, -31.4429099], 1520434932926, 118,
[None, [None, -64.1830241, -31.4199133], 1520431523874, 21,

The 21x figures corresponds to my phone, the others to my wife who was at home, and then on the move (118), to finally to 23 when she reached her destination, back to 80something on the move again, and then to 23 when stopped.

In meters, it seems a very reasonable accuracy.

if you don’t mind monitoring for a while longer and things correlate after a while I will implement this. How does that sound? Actually if people could help with this making sure that this value makes sense that would be awesome!

Sure, I’ll take a look a couple of days and I’ll go back to you.
My take on the usage is that the presence plugin should dismiss any states changes if the accuracy is below certain figure, pretty much as owntracks do: If I set my home to, let’s say, a 30 meters circle area around my house, and the accuracy is 113, there’s little use for an event triggered to be actually used to determine if I’m home or not, as the gps actually cannot tell.

That makes absolute sense. It is very annoying that I get popping in and out states every once in a while… Keep me informed on the correlation.

Oh, BTW: On my end also there is the issue of conflicts with the mikrotik/network based presence detection. If your ‘consider_home’ parameter is too high (and the default is like 180 seconds I believe) what happens here is that your GPS reports that you’re away, but your router is forced to wait three minutes before actually letting you go. So I’m setting this to something like 15 seconds and see if the in/out messages drops.

Got this working this morning, thank you so much!!