Google Fit Support

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.

https://gist.github.com/wardcraigj/f4456186759302d75d3619d642999e5e

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

The gist should work as a custom component, but you’ll need to edit some of the values to make it work for you. Specifically, you’ll need to edit the client_id and secret. These values you’ll get by setting up an app and making sure the fit api is enabled. You should be able to use the https://home-assistant.io/components/calendar.google/ instructions as a guide

edit: I just edited the gist to add a fix that could be causing some warnings in the logs

2 Likes

Hi @wardcraigj are you planning on making this an official component?

1 Like

I don’t have any immediate plans to. I’ve noticed it’s not super consistent in pulling steps, so that would require some work, also all the client logic would need to get pulled out and implemented in it’s own library that gets pulled in as a requirement in order for the component to be accepted. I don’t have any really useful automations that use it in my set-up, so I don’t really want to commit to maintaining and supporting it, but if someone wants to build it out, I’d be happy to offer any sort of assistance that I can

Thanks Craig. That makes sense.

I was trying to achieve something similar. Mainly focused on weight, but happy to see if it can be extended for other attributes.

This is my custom_compoments/sensor/google_fit.py:

While it is focused on weight, it could be updated to other type of data by changing:
API_SCOPE = ‘https://www.googleapis.com/auth/fitness.body.read
and
weight_datasources = self._get_datasources(‘com.google.weight’)
(as well as selecting the right attributes afterwards)

I hope it is useful.
If someone wants to partner to extend this and integrate it natively, happy to do so.

Cheers,
Nito

1 Like

Thanks @nitobuendia I was able to get Steps, move time, distance and weight with modifications in your script.
Here is my version of the custom_component
https://github.com/hemantkamalakar/haconfigs/blob/master/custom_components/sensor/google_fit.py

You mean i can use the same client_id and client_secret i use woth google calendar or must i create another ones for Google Fit?

Is this component still in use? I added and was prompted to enter my code but sensors are not added.

Yes. You have to create client_id and client_secret for Google Fit.

I have managed to get authentication and sensors are added but they always state unknown.

Yes. In fact I have added more sensors now. Now it supports
- steps
- distance
- time
- calories
- weight
- height

3 Likes