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

Fix fitbit error when trying to access token after upgrade. #9183

Merged
merged 2 commits into from
Aug 30, 2017

Conversation

tchellomello
Copy link
Contributor

Description:

Fix fibit sensor to correctly access oauth token after upgrade on 0.52

Related issue (if applicable): fixes #9152

Traceback:

"File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/sensor/fitbit.py", line 494, in update
ATTR_ACCESS_TOKEN: self.client.client.token['access_token'],
AttributeError: 'FitbitOauth2Client' object has no attribute 'token'"

Example entry for configuration.yaml (if applicable):

# sensors.yaml
- platform: fitbit
  monitored_resources:
    - "activities/distance"
    - "activities/minutesFairlyActive"
    - "activities/minutesLightlyActive"
    - "activities/minutesSedentary"
    - "activities/minutesVeryActive"
    - "activities/steps"
    - "devices/battery"
    - "sleep/efficiency"

Checklist:

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.

@tchellomello
Copy link
Contributor Author

This PR still needs some work since once the token is expired, on a HASS restart it will throw the error below:

Aug 29 03:44:33 hass hass[3535]:   File "/opt/srv/homeassistant/.virtualenvs/hass-py36/lib/python3.6/site-packages/homeassistant/helpers/entity_component.py", line 164, in _async_setup_platform
Aug 29 03:44:33 hass hass[3535]:     SLOW_SETUP_MAX_WAIT, loop=self.hass.loop)
Aug 29 03:44:33 hass hass[3535]:   File "/usr/lib64/python3.6/asyncio/tasks.py", line 358, in wait_for
Aug 29 03:44:33 hass hass[3535]:     return fut.result()
Aug 29 03:44:33 hass hass[3535]:   File "/usr/lib64/python3.6/asyncio/futures.py", line 245, in result
Aug 29 03:44:33 hass hass[3535]:     raise self._exception
Aug 29 03:44:33 hass hass[3535]:   File "/usr/lib64/python3.6/concurrent/futures/thread.py", line 55, in run
Aug 29 03:44:33 hass hass[3535]:     result = self.fn(*self.args, **self.kwargs)
Aug 29 03:44:33 hass hass[3535]:   File "/opt/srv/homeassistant/.homeassistant/custom_components/sensor/fitbit.py", line 272, in setup_platform
Aug 29 03:44:33 hass hass[3535]:     authd_client.system = authd_client.user_profile_get()["user"]["locale"]
Aug 29 03:44:33 hass hass[3535]:   File "/opt/srv/homeassistant/.virtualenvs/hass-py36/lib/python3.6/site-packages/fitbit/api.py", line 252, in user_profile_get
Aug 29 03:44:33 hass hass[3535]:     return self.make_request(url)
Aug 29 03:44:33 hass hass[3535]:   File "/opt/srv/homeassistant/.virtualenvs/hass-py36/lib/python3.6/site-packages/fitbit/api.py", line 223, in make_request
Aug 29 03:44:33 hass hass[3535]:     response = self.client.make_request(*args, **kwargs)
Aug 29 03:44:33 hass hass[3535]:   File "/opt/srv/homeassistant/.virtualenvs/hass-py36/lib/python3.6/site-packages/fitbit/api.py", line 99, in make_request
Aug 29 03:44:33 hass hass[3535]:     exceptions.detect_and_raise_error(response)
Aug 29 03:44:33 hass hass[3535]:   File "/opt/srv/homeassistant/.virtualenvs/hass-py36/lib/python3.6/site-packages/fitbit/exceptions.py", line 82, in detect_and_raise_error
Aug 29 03:44:33 hass hass[3535]:     raise HTTPUnauthorized(response)
Aug 29 03:44:33 hass hass[3535]: fitbit.exceptions.HTTPUnauthorized: Access token expired: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0SktIM1MiLCJhdWQiOiIyMjdWsecret. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process.

@pvizeli pvizeli changed the title Fix fitbit error when trying to access token after upgrade. WIP: Fix fitbit error when trying to access token after upgrade. Aug 29, 2017
@tchellomello
Copy link
Contributor Author

OK, after studying a little bit the python-fitbit the problem is that a refresh_cb constructor must be passed to the oauth client. So that is why the application currently just work while the token is valid.

More details at orcasgit/python-fitbit#119 (comment)

I'm checking how to implement it.

  The 3rd python-fitbit module requires an extra kwarg on the FitBit
  constructor called refresh_cb. The value should be a function that
  accepts one argument token.

  This value will be a dictionary with the keys:

     'access_token', 'refresh_token', 'expires_at'

  This implements a lambda refresh_cb as required by the Fitbit module
  to work, however the new token will always be save manually on
  every update() call.
@tchellomello tchellomello changed the title WIP: Fix fitbit error when trying to access token after upgrade. Fix fitbit error when trying to access token after upgrade. Aug 30, 2017
@tchellomello
Copy link
Contributor Author

Ok, this PR will now fix the problem. We are now able to refresh the token on every HA restart or when the token gets expired. I tested it and it worked fine.

Basically what happens it that python-fitbit module requires an extra kwarg on the FitBit constructor called refresh_cb. The value should be a function that accepts one argument token.

The fix basically implements a lambda refresh_cb method as required by the Fitbit module to work, however the new token will be always saved manually on every update() call.

@pvizeli PR looks good now. I removed the WIP from the title.

Thanks!

@pvizeli pvizeli added this to the 0.52.2 milestone Aug 30, 2017
@pvizeli pvizeli merged commit f76436f into home-assistant:dev Aug 30, 2017
matemaciek added a commit to matemaciek/home-assistant that referenced this pull request Aug 30, 2017
* upstream/dev: (113 commits)
  Fix fitbit error when trying to access token after upgrade. (home-assistant#9183)
  Upgrade sendgrid to 5.0.1 (home-assistant#9215)
  Upgrade pyasn1 to 0.3.3 and pyasn1-modules to 0.1.1 (home-assistant#9216)
  directv: extended discovery via REST api, bug fix  (home-assistant#8800)
  Bayesian Binary Sensor (home-assistant#8810)
  Add cloud auth support (home-assistant#9208)
  Abode push events and lock, cover, and switch components (home-assistant#9095)
  Lint Sonarr tests
  Upgrade pymysensors to 0.11.1 (home-assistant#9212)
  Refactor rfxtrx (home-assistant#9117)
  Issue home-assistant#6893 in rfxtrx (home-assistant#9130)
  Support for season sensor (home-assistant#8958)
  Add counter component (home-assistant#9146)
  Fix and optimize digitalloggers platform (home-assistant#9203)
  Prevent error when no forecast data was available (home-assistant#9176)
  Add "status" to Sonarr sensor (home-assistant#9204)
  fix worldtidesinfo home-assistant#9184 (home-assistant#9201)
  Update pushbullet.py (home-assistant#9200)
  Fix dht22 when no data was read initially home-assistant#8976 (home-assistant#9198)
  Prevent iCloud exceptions in logfile (home-assistant#9179)
  ...
@tchellomello tchellomello deleted the fix_9152 branch August 30, 2017 15:44
@balloob balloob mentioned this pull request Sep 7, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Dec 11, 2017
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.

Fitbit sensor not working since 52.0 update
4 participants