Google Fit Support

I see HA supports FitBit … Any chance of adding Goole Fit?

Python API is available:
https://developers.google.com/api-client-library/python/apis/fitness/v1

I’ve found a Python script that was trying to get the steps, forked it and made it show the amount of steps that fit.google.com is showing:
https://gist.github.com/danielperna84/a02c307e123036973845e85b326cc940

This may help someone to make a sensor for the steps.

There already is a component that does the Google authentication and the binary sensor for the calendar. I guess it would make sense to modify the existing component to have a list of options for which APIs authentication should be requested for. Currently only the calendar API is requested.

2 Likes

This Google Developers Console which is mentioned in this script? Which service is that exactly, I can’t find it under that name? (is if free?)?

I think you can use the instructions for the Google Calendar sensor as a rough guide. But instead of using the calendar API you search for the fitness API.

Thanks I got further :).

Now after i paste the verification code and press enter i get this:

Traceback (most recent call last):
File “getfit.py”, line 68, in
dataset = retrieve_data()
File “getfit.py”, line 46, in retrieve_data
credentials = flow.step2_exchange(code)
File “/home/msev/.virtualenvs/coding/local/lib/python2.7/site-packages/oauth2client/_helpers.py”, line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File “/home/msev/.virtualenvs/coding/local/lib/python2.7/site-packages/oauth2client/client.py”, line 2089, in step2_exchange
raise FlowExchangeError(error_msg)
oauth2client.client.FlowExchangeError: invalid_clientUnauthorized

Did you register for the Fit API here?
https://developers.google.com/fit/rest/v1/get-started

This is the Google Developers Console:
https://console.developers.google.com/apis/dashboard

This is how to Enable and Create the Authorization:
https://console.developers.google.com/apis/api/fitness.googleapis.com/

I see in the Gist you commented saying you used the first link.

Yes I got the two api keys and inserted them into the code…Then it opens some auth page and it wants me to paste some verification number inside the code and it doesn’t work…when i paste it.

Usage of my script should actualyl be pretty straight forward. You start the script, it prompts you to visit an URL to receive the authorization token (it does that every time you run the script), you go to that URL, acknowledge the required permissions, and then get the token. That token then has to be pasted into the console where the script is running, and once confirmed, it shows the steps of the current day.

If this doesn’t work for you, then there was a mistake in one of the steps while creating your project at Googles dev-console.
For me it looks like this:

At the bottom you can see the fitness and calendar being activated for the project.

Yes I have Fitness API activated. I did visit that url and copied the authorization token, but it didn’t accept the authorization token, and it gave that error? Do i need to put the token in " " , edit: it didn’t help if I put the token into " ". Do you want me to check the version of pip libs?

I kind of doubt that it’s actually worth the effort to dig any deeper. The script is really just a proof of concept on how to get the data. In its current form it does not serve any purpose in conjuction with HASS.
If you really want to keep working on it, then head over to the instructions here.

Its unfortunate that I can’t get it to work, because I’m not skilled enough to make my own stuff from scratch… I need some more easy framework :).

I would be very interested on how to use this data in a sensor in Hass. Unfortunately I don’t have the coding skills to implement this myself. Can someone help me out?

Getting lots of errors after adding the getfit.py in my custom_components folder and adding it as a sensor:

 - platform: getfit
017-07-30 16:27:03 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/discovery.py", line 155, in async_load_platform
    hass, component, hass_config)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/setup.py", line 50, in async_setup_component
    return (yield from setup_tasks[domain])
  File "/usr/lib/python3.4/asyncio/futures.py", line 390, in __iter__
    return self.result()  # May raise too.
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/discovery.py", line 155, in async_load_platform
    hass, component, hass_config)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/setup.py", line 50, in async_setup_component
    return (yield from setup_tasks[domain])
  File "/usr/lib/python3.4/asyncio/futures.py", line 390, in __iter__
    return self.result()  # May raise too.
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/discovery.py", line 155, in async_load_platform
    hass, component, hass_config)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/setup.py", line 50, in async_setup_component
    return (yield from setup_tasks[domain])
  File "/usr/lib/python3.4/asyncio/futures.py", line 390, in __iter__
    return self.result()  # May raise too.
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/asyncio/tasks.py", line 233, in _step
    result = coro.throw(exc)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/setup.py", line 61, in async_setup_component
    return (yield from task)
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/setup.py", line 156, in _async_setup_component
    conf_util.async_process_component_config(hass, config, domain)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/config.py", line 589, in async_process_component_config
    platform = get_platform(domain, p_name)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/loader.py", line 103, in get_platform
    return get_component(PLATFORM_FORMAT.format(domain, platform))
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/loader.py", line 141, in get_component
    module = importlib.import_module(path)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1467, in exec_module
  File "<frozen importlib._bootstrap>", line 1572, in get_code
  File "<frozen importlib._bootstrap>", line 1532, in source_to_code
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/homeassistant/.homeassistant/custom_components/sensor/getfit.py", line 43
    print 'Go to the following link in your browser:'
                                                    ^
SyntaxError: Missing parentheses in call to 'print'

I changed my client_id and client_secret accordingly.

My script is not usable as a custom component. It is just a proof of concept on how to use the API. There have to be quite some modifications for it to work within HASS. Besides that, my script asks for a token each time data is requested. HASS can’t do that on its own. So the very first step would be to modify the script to actually save the access token so it can do its work afterwards.

Ok, well thats’s beyond my knowledge at the moment. Thanks anyway :slight_smile:

Not sure if anybody is still interested in this, but I cobbled this together by patching bits of code from the script posted and the google calendar component.

It seems to work fine as a custom component, but it would need to be cleaned up a fair amount in order to be included in the core. I just wanted the data to play with and see if I could make any sort of useful automation with it so I probably won’t spend any more time on it unless someone else ends up finding a good enough use-case for it (maybe after x steps, assume I’m awake, or if after a certain time I haven’t taken x steps, assume I’ve over-slept and try to wake me?)

4 Likes

I think it could be useful. My first thought is as an Dead man’s switch. If you haven’t moved for time X, start a timer that sends a notification to yourself to make you aware the timer has been started, and when it has finished without being cancelled send another notification to someone to check on you.

It may as well be useful in conjunction with the Bayesian binary sensor to automate other aspects of your home. Like if your home and still moving, don’t turn of lights that usually turn off at a specific point in time. Or just turn them off if you havent moved for some time.

5 Likes

@wardcraigj very nice, I was waiting a long time for this thanks!

I can’t get it to work though:
image

I tried several ID’s. Can’t figure out why it is not working: I activated the fitness api and created login credentials.

Thanks so much for this, awesome!

I am trying to get it to run on my setup, unfortunately I’m getting an error:

AttributeError: module ‘custom_components.google_fit’ has no attribute ‘setup’

Any ideas? Initially I thought it might be a cut/paste indentation error, but I have ruled that out as I cloned your gist directly from your repository…

Use cases:

Would like to light a lamp green / yellow / red depending on how much activity I’ve done in the day. After that next project would be to create a beer fridge that only opens if “I’ve been good”…

1 Like

Hello @wardcraigj - Are you still working on the implementation? Thanks, Thomas

1 Like