Core: Fitbit stops updating

Created on 16 May 2016  路  8Comments  路  Source: home-assistant/core

Make sure you run the latest version before reporting an issue. Feature requests should go in the forum: https://community.home-assistant.io/c/feature-requests

Home Assistant release (hass --version):
0.20.dev0

Python release (python3 --version):

Component/platform:
fitbit

Description of problem:
Fitbit sensor stops updating 2 hours after hass has started.
Two different tracebacks provided.
Expected:
Fitbit updating sensors.

Problem-relevant configuration.yaml entries and steps to reproduce:




    1. 2.
  1. 3.

Traceback (if applicable):

Traceback (most recent call last):
  File "/srv/hass/src/home-assistant/homeassistant/core.py", line 801, in job_handler
    func(arg)
  File "/srv/hass/src/home-assistant/homeassistant/helpers/event.py", line 179, in pattern_time_change_listener
    action(now)
  File "/srv/hass/src/home-assistant/homeassistant/helpers/entity_component.py", line 191, in _update_entity_states
    entity.update_ha_state(True)
  File "/srv/hass/src/home-assistant/homeassistant/helpers/entity.py", line 145, in update_ha_state
    self.update()
  File "/srv/hass/src/home-assistant/homeassistant/util/__init__.py", line 289, in wrapper
    result = method(*args, **kwargs)
  File "/srv/hass/src/home-assistant/homeassistant/components/sensor/fitbit.py", line 365, in update
    @Throttle(MIN_TIME_BETWEEN_UPDATES)
  File "/media/Server/HomeAssistant/deps/fitbit/api.py", line 515, in time_series
    return self.make_request(url)
  File "/media/Server/HomeAssistant/deps/fitbit/api.py", line 227, in make_request
    response = self.client.make_request(*args, **kwargs)
  File "/media/Server/HomeAssistant/deps/fitbit/api.py", line 68, in make_request
    response = self._request(method, url, data=data, auth=auth, **kwargs)
  File "/media/Server/HomeAssistant/deps/fitbit/api.py", line 55, in _request
    return self.session.request(method, url, **kwargs)
  File "/srv/hass/lib/python3.5/site-packages/requests/sessions.py", line 461, in request
    prep = self.prepare_request(req)
  File "/srv/hass/lib/python3.5/site-packages/requests/sessions.py", line 394, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/srv/hass/lib/python3.5/site-packages/requests/models.py", line 299, in prepare
    self.prepare_auth(auth, url)
  File "/srv/hass/lib/python3.5/site-packages/requests/models.py", line 494, in prepare_auth
    r = auth(self)
  File "/media/Server/HomeAssistant/deps/requests_oauthlib/oauth2_auth.py", line 35, in __call__
    http_method=r.method, body=r.body, headers=r.headers)
  File "/media/Server/HomeAssistant/deps/oauthlib/oauth2/rfc6749/clients/base.py", line 194, in add_token
    raise TokenExpiredError()
oauthlib.oauth2.rfc6749.errors.TokenExpiredError: (token_expired)
Traceback (most recent call last):
  File "/srv/hass/src/home-assistant/homeassistant/core.py", line 801, in job_handler
    func(arg)
  File "/srv/hass/src/home-assistant/homeassistant/helpers/event.py", line 179, in pattern_time_change_listener
    action(now)
  File "/srv/hass/src/home-assistant/homeassistant/helpers/entity_component.py", line 191, in _update_entity_states
    entity.update_ha_state(True)
  File "/srv/hass/src/home-assistant/homeassistant/helpers/entity.py", line 145, in update_ha_state
    self.update()
  File "/srv/hass/src/home-assistant/homeassistant/util/__init__.py", line 289, in wrapper
    result = method(*args, **kwargs)
  File "/srv/hass/src/home-assistant/homeassistant/components/sensor/fitbit.py", line 365, in update
    response = self.client.time_series(self.resource_type, period="7d")
  File "/media/Server/HomeAssistant/deps/fitbit/api.py", line 515, in time_series
    return self.make_request(url)
  File "/media/Server/HomeAssistant/deps/fitbit/api.py", line 227, in make_request
    response = self.client.make_request(*args, **kwargs)
  File "/media/Server/HomeAssistant/deps/fitbit/api.py", line 68, in make_request
    response = self._request(method, url, data=data, auth=auth, **kwargs)
  File "/media/Server/HomeAssistant/deps/fitbit/api.py", line 55, in _request
    return self.session.request(method, url, **kwargs)
  File "/srv/hass/lib/python3.5/site-packages/requests/sessions.py", line 461, in request
    prep = self.prepare_request(req)
  File "/srv/hass/lib/python3.5/site-packages/requests/sessions.py", line 394, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/srv/hass/lib/python3.5/site-packages/requests/models.py", line 299, in prepare
    self.prepare_auth(auth, url)
  File "/srv/hass/lib/python3.5/site-packages/requests/models.py", line 494, in prepare_auth
    r = auth(self)
  File "/media/Server/HomeAssistant/deps/requests_oauthlib/oauth2_auth.py", line 35, in __call__
    http_method=r.method, body=r.body, headers=r.headers)
  File "/media/Server/HomeAssistant/deps/oauthlib/oauth2/rfc6749/clients/base.py", line 194, in add_token
    raise TokenExpiredError()
oauthlib.oauth2.rfc6749.errors.TokenExpiredError: (token_expired) 

Additional info:

Most helpful comment

@robbiet480 You are very welcome teamwork++.

The good news is that it's been running for almost a day and the fitbit.conf file and the token got refreshed as expected. I had no tracebacks and the frontend is reporting the data as expected.

Yes, let's backport/import the fix and we should be fine. Thanks @turbokongen and @roflmao for reporting the issue.

@robbiet480 Please let me know if you need help. Are you planning to create a local branch for that or re-open a change request for @orcasgit ?

Thanks guys.
~ mmello

All 8 comments

The fitbit package should be taking care of auto updating the token, but it doesn't appear it is. Will look into it soon-ish.

I also have this problem.

16-06-20 18:58:30 ERROR (ThreadPool Worker 5) [homeassistant.core] BusHandler:Exception doing job
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/core.py", line 809, in job_handler
    func(arg)
  File "/usr/src/app/homeassistant/helpers/event.py", line 179, in pattern_time_change_listener
    action(now)
  File "/usr/src/app/homeassistant/helpers/entity_component.py", line 191, in _update_entity_states
    entity.update_ha_state(True)
  File "/usr/src/app/homeassistant/helpers/entity.py", line 145, in update_ha_state
    self.update()
  File "/usr/src/app/homeassistant/util/__init__.py", line 289, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/app/homeassistant/components/sensor/fitbit.py", line 384, in update
    response = self.client.time_series(self.resource_type, period="7d")
  File "/usr/local/lib/python3.4/site-packages/fitbit/api.py", line 515, in time_series
    return self.make_request(url)
  File "/usr/local/lib/python3.4/site-packages/fitbit/api.py", line 227, in make_request
    response = self.client.make_request(*args, **kwargs)
  File "/usr/local/lib/python3.4/site-packages/fitbit/api.py", line 68, in make_request
    response = self._request(method, url, data=data, auth=auth, **kwargs)
  File "/usr/local/lib/python3.4/site-packages/fitbit/api.py", line 55, in _request
    return self.session.request(method, url, **kwargs)
  File "/usr/local/lib/python3.4/site-packages/requests/sessions.py", line 461, in request
    prep = self.prepare_request(req)
  File "/usr/local/lib/python3.4/site-packages/requests/sessions.py", line 394, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/local/lib/python3.4/site-packages/requests/models.py", line 299, in prepare
    self.prepare_auth(auth, url)
  File "/usr/local/lib/python3.4/site-packages/requests/models.py", line 494, in prepare_auth
    r = auth(self)
  File "/usr/local/lib/python3.4/site-packages/requests_oauthlib/oauth2_auth.py", line 35, in __call__
    http_method=r.method, body=r.body, headers=r.headers)
  File "/usr/local/lib/python3.4/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 194, in add_token
    raise TokenExpiredError()
oauthlib.oauth2.rfc6749.errors.TokenExpiredError: (token_expired)

Hello folks,
I'm also hitting the same problem over here.

Traceback (most recent call last):
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/homeassistant/core.py", line 835, in job_handler
    func(arg)
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/homeassistant/helpers/event.py", line 179, in pattern_time_change_listener
    action(now)
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 180, in _update_entity_states
    entity.update_ha_state(True)
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/homeassistant/helpers/entity.py", line 163, in update_ha_state
    self.update()
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/homeassistant/util/__init__.py", line 289, in wrapper
    result = method(*args, **kwargs)
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/homeassistant/components/sensor/fitbit.py", line 384, in update
    response = self.client.time_series(self.resource_type, period="7d")
  File "/home/hass/.homeassistant/deps/fitbit/api.py", line 515, in time_series
    return self.make_request(url)
  File "/home/hass/.homeassistant/deps/fitbit/api.py", line 227, in make_request
    response = self.client.make_request(*args, **kwargs)
  File "/home/hass/.homeassistant/deps/fitbit/api.py", line 68, in make_request
    response = self._request(method, url, data=data, auth=auth, **kwargs)
  File "/home/hass/.homeassistant/deps/fitbit/api.py", line 55, in _request
    return self.session.request(method, url, **kwargs)
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/requests/sessions.py", line 461, in request
    prep = self.prepare_request(req)
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/requests/sessions.py", line 394, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/requests/models.py", line 299, in prepare
    self.prepare_auth(auth, url)
  File "/home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/requests/models.py", line 494, in prepare_auth
    r = auth(self)
  File "/home/hass/.homeassistant/deps/requests_oauthlib/oauth2_auth.py", line 35, in __call__
    http_method=r.method, body=r.body, headers=r.headers)
  File "/home/hass/.homeassistant/deps/oauthlib/oauth2/rfc6749/clients/base.py", line 194, in add_token
    raise TokenExpiredError()

However when restarting the HASS, the fitbit component is able to get a new token and then it works as expected.

Looking at https://community.home-assistant.io/t/fitbit-error/1087/17 we can see a patch that seemed to fix this issue, however I checked the python-fitbit code https://github.com/orcasgit/python-fitbit and the PR (https://github.com/orcasgit/python-fitbit/pull/92) is not implemented on my local library. We can see the patch on shimeez branch at https://github.com/shimeez/python-fitbit/commit/762b7c6175f7efaaa06055eff5b63a009f9131e8

I've updated my local fitbit/api.py code on my devel box with shimeez's patch and I'll report the results on the next 2 days.

    -- installed the global 
      $ pip install --upgrade  git+git://github.com/shimeez/python-fitbit.git@master

    -- for double check, I've modified the local deps too
      $ diff -ruN ~/.homeassistant/deps/fitbit/api.py.orig ~/.homeassistant/deps/fitbit/api.py
      --- /home/hass/.homeassistant/deps/fitbit/api.py.orig 2016-07-12 00:04:24.111905150 -0400
      +++ /home/hass/.homeassistant/deps/fitbit/api.py  2016-07-12 00:05:04.551618310 -0400
      @@ -10,6 +10,7 @@
           from urllib import urlencode

       from requests_oauthlib import OAuth2, OAuth2Session
      +from oauthlib.oauth2 import TokenExpiredError
       from fitbit.exceptions import (BadResponse, DeleteError, HTTPBadRequest,
                                      HTTPUnauthorized, HTTPForbidden,
                                      HTTPServerError, HTTPConflict, HTTPNotFound,
      @@ -66,7 +67,7 @@
               try:
                   auth = OAuth2(client_id=self.client_id, token=self.token)
                   response = self._request(method, url, data=data, auth=auth, **kwargs)
      -        except HTTPUnauthorized as e:
      +        except (HTTPUnauthorized, TokenExpiredError) as e:
                   self.refresh_token()
                   auth = OAuth2(client_id=self.client_id, token=self.token)
                   response = self._request(method, url, data=data, auth=auth, **kwargs)

    -- looking the same
      $ diff /home/hass/.virtualenvs/home_assistant/lib/python3.4/site-packages/fitbit/api.py  /home/hass/.homeassistant/deps/fitbit/api.py | wc -l 
      0

If that works, we might need to cherrypick the patch as well.

Best Regards,
mmello

@tchellomello Thanks for your research on this. Let me know what you find and if you come back with positive news i'll get the patch in.

@robbiet480 You are very welcome teamwork++.

The good news is that it's been running for almost a day and the fitbit.conf file and the token got refreshed as expected. I had no tracebacks and the frontend is reporting the data as expected.

Yes, let's backport/import the fix and we should be fine. Thanks @turbokongen and @roflmao for reporting the issue.

@robbiet480 Please let me know if you need help. Are you planning to create a local branch for that or re-open a change request for @orcasgit ?

Thanks guys.
~ mmello

I spoke with @robbiet480 and I'll work to submit a patch to the official project and I'll paste the information here.

PR #3244 created since upstream patches for python-fitbit were merged

https://github.com/orcasgit/python-fitbit/pull/97
https://github.com/orcasgit/python-fitbit/pull/99

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Gio76 picture Gio76  路  223Comments

Ciqsky picture Ciqsky  路  129Comments

WilldabeastHA picture WilldabeastHA  路  203Comments

kdschlosser picture kdschlosser  路  374Comments

balloob picture balloob  路  371Comments