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

Google Maps location sharing device tracker. #12301

Merged
merged 7 commits into from
Mar 30, 2018

Conversation

michaelarnauts
Copy link
Contributor

@michaelarnauts michaelarnauts commented Feb 11, 2018

Description:

This component uses Google Maps location sharing to track device location. This allows to track android devices without battery impact or having to install another app.

Uses https://github.com/costastf/locationsharinglib to fetch the locations of shared accounts.

Documentation is available at: home-assistant/home-assistant.io#4646

Example entry for configuration.yaml (if applicable):

device_tracker:
  - platform: google_maps
    username: username@gmail.com
    password: password

Checklist:

  • The code change is tested and works locally.

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

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

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@michaelarnauts michaelarnauts changed the title Google Maps location sharing device tracker. WIP: Google Maps location sharing device tracker. Feb 11, 2018
})


def setup_scanner(hass, config: dict, see, discovery_info=None):
Copy link
Member

Choose a reason for hiding this comment

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

There's a special ConfigType in helpers/typing that should probably be used instead of dict.

try:
# Check if we already have cached credentials
if os.path.isfile(CREDENTIALS_FILE):
_LOGGER.info('Authenticating with credentials file.')
Copy link
Member

Choose a reason for hiding this comment

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

I'm no expert, but this seems more like a debug log entry to me.

@michaelarnauts michaelarnauts changed the title WIP: Google Maps location sharing device tracker. Google Maps location sharing device tracker. Feb 12, 2018

REQUIREMENTS = ['locationsharinglib==0.3.0']

CONF_IGNORED_DEVICES = 'ignored_devices'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just noticed these two are not used. I will remove them.


def _update_info(self, now=None):
for person in self.service.get_all_people():
dev_id = 'google_maps_{0}'.format(slugify(person.id))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it somehow possible to change these identifiers in for example the entity registry? (cc @balloob)

Copy link
Member

Choose a reason for hiding this comment

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

Out of scope 😉

I think that we should be able to use this ID as a unique ID inside the device tracker.

@michaelarnauts
Copy link
Contributor Author

Does this needs changing anything?

@Esiravegna
Copy link

THing is, if we can use the ID as an internal tracker (check the MAC address for the router based tracking), we could name the device in a sensible way (my_phone, wife_phone, etc) as opposed to the current names required in know_devices.yaml, that are hard to remember.

@dshokouhi
Copy link
Member

won't the entity_registry let us rename the entities created with a unique id? really looking forward to this integration getting added :)

@fanaticDavid
Copy link
Contributor

@Esiravegna Slightly off-topic, but even without the entity registry you can already influence the entity_id of the devices in the known_devices.yaml file:

001710xxxxxx:               => changing this to abc will change the entity_id from device_tracker.001710xxxxxx to device_tracker.abc
  hide_if_away: false
  icon:
  mac: 00:17:10:xx:xx:xx
  name: 001710xxxxxx        => this is used as the device's friendly_name in the UI
  picture:
  track: false
  vendor: Casa Systems Inc.

Changes are applied after restarting Home Assistant.

@Esiravegna
Copy link

@fanaticDavid Thanks I'm aware of that. Thing is, somewhere in the structure the id generated by this component must reside, otherwise the tracking will not be there. I don't think is the MAC here, and if it is, problem is that the router presence detector also uses them, and thus we won't be able to use both methods.

TL;DR: I'd like to see a way to rename the device, keeping the google maps id and also use the other presence detectors, hence muy point.


REQUIREMENTS = ['locationsharinglib==0.3.0']

CREDENTIALS_FILE = 'google_maps_location_sharing.conf'
Copy link
Member

Choose a reason for hiding this comment

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

Please add a . in front of the name

hass.config.path(CREDENTIALS_FILE))
self._update_info()

track_utc_time_change(
Copy link
Member

Choose a reason for hiding this comment

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

It seems you just want to call track_time_interval(hass, action, interval)

attributes=attrs
)

return True
Copy link
Member

Choose a reason for hiding this comment

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

Nothing is checking this return value.

from locationsharinglib import Service
from locationsharinglib.locationsharinglibexceptions import InvalidUser

self.hass = hass
Copy link
Member

Choose a reason for hiding this comment

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

Nothing is using the instance attribute outside this method. Please remove the attribute and just use hass locally in this method.

self.success_init = True

except InvalidUser:
_LOGGER.error('You have specified invalid login credentials.')
Copy link
Member

Choose a reason for hiding this comment

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

Remove period at the end.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have updated locationsharinglib to 0.4.0 fixing a bug with single quotes of the json payload and exposing gps accuracy as an attribute. The dependency should be updated to this version. I also suggest to change the .conf extension to the more semantically correct .cookies

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GPS accuracy is not fetched from the correct field. I get an accuracy of 1500 when google maps locates me spot on with no blue accuract circle. I've not included this information.

@michaelarnauts
Copy link
Contributor Author

@MartinHjelmare i've addressed your remarks.
@andrey-git do you need to review this?

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Just a minor comment for clarity.

def setup_scanner(hass, config: ConfigType, see, discovery_info=None):
"""Set up the scanner."""
scanner = GoogleMapsScanner(hass, config, see)
return scanner if scanner.success_init else None
Copy link
Member

Choose a reason for hiding this comment

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

setup_scanner doesn't want the scanner instance, it just wants a boolean, where true means successful platform setup.

This will work, but it's less clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A lot of device_tracker platforms seem to do exactly this. I don't think this is blocking the merge of this component? Anyway, I will make the change...

homeassistant/components/device_tracker/actiontec.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/aruba.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/asuswrt.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/bbox.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/bt_home_hub_5.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/cisco_ios.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/fritz.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/google_maps.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/hitron_coda.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/keenetic_ndms2.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/luci.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/mikrotik.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/netgear.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/nmap_tracker.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/sky_hub.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/snmp.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/swisscom.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/tado.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/thomson.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/ubus.py:    return scanner if scanner.success_init else None
homeassistant/components/device_tracker/xiaomi.py:    return scanner if scanner.success_init else None

Copy link
Member

@MartinHjelmare MartinHjelmare Mar 30, 2018

Choose a reason for hiding this comment

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

get_scanner should return the scanner instance but not setup_scanner. Platforms that use that latter should not behave like platforms that use the former.

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Thanks!

@MartinHjelmare MartinHjelmare merged commit 0f24fea into dev Mar 30, 2018
@Danielhiversen Danielhiversen deleted the google_maps_device_tracker branch April 4, 2018 12:15
@disrupted
Copy link

this is awesome ! I was only just tweeting the other day how much I would love a Google location history device tracker and here it is already, gotta love this community ❤️

@proximus-983
Copy link

I have this error in /config/.google_maps_location_sharing.cookies
'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
and no device appears in known_devices.yaml

@home-assistant home-assistant locked and limited conversation to collaborators Apr 9, 2018
@MartinHjelmare
Copy link
Member

Please open an issue if you suspect a bug. If you need help please use our help channels:
https://home-assistant.io/help/#communication-channels

Merged PRs should not be used for support or bug reports. Thanks!

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.