Fix fitbit error when trying to access token after upgrade.#9183
Conversation
|
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. |
|
OK, after studying a little bit the 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.
|
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 @pvizeli PR looks good now. I removed the Thanks! |
* 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) ...
Description:
Fix fibit sensor to correctly access oauth token after upgrade on
0.52Related issue (if applicable): fixes #9152
Traceback:
Example entry for
configuration.yaml(if applicable):Checklist:
If the code does not interact with devices:
toxrun successfully. Your PR cannot be merged unless tests pass