core: Ecobee Integration breaks after random number of manual restarts of HA

Home Assistant release with the issue: 0.100.x

Last working Home Assistant release (if known): <0.100.x – I think

Operating environment (Hass.io/Docker/Windows/etc.): Docker

Integration: Ecobee

Description of problem: Ecobee integration stops working after a manual HA restart and logs a message. The only way to get it working again is to remove the entire ecobee integration from HA… Restart HA… Remove the app from the ecobee developer page. Then add ecobee integration to HA again, add the app to ecobee developer page, and submit in HA.

2019-11-04 10:51:25 ERROR (SyncWorker_7) [pyecobee] Error while refreshing tokens from ecobee: 400: {'error': 'invalid_grant', 'error_description': 'The authorization grant, token or credentials are invalid, expired, revoked, do not match the redirection URI used in the authorization request, or was issued to another client.', 'error_uri': 'https://tools.ietf.org/html/rfc6749#section-5.2'}
2019-11-04 10:51:25 ERROR (MainThread) [homeassistant.components.ecobee] Error updating ecobee tokens

I also get random messages throughout the day such as…

2019-11-01 17:03:33 WARNING (MainThread) [homeassistant.components.ecobee] Ecobee update failed; attempting to refresh expired tokens

This doesn’t seem to be an indication after a restart that the integration will break. It’s just a random message. There is no rhyme or reason. I manually restart HA for various configuration reasons. Could be the next restart or it could be 10 restarts later. Eventually the ecobee integration fails.

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant): Configuration is done via integrations. No configuration.yaml in HA

Traceback (if applicable): Not Applicable

Additional information: I do not recall seeing this issue pre 0.100.x

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 55 (32 by maintainers)

Most upvoted comments

PR has been submitted: #32008. I was not experiencing this bug, but the integration appears to continue to work correctly with the changes to fix the bug in place.

Again all, sorry for the delay. This isn’t on the HASS dev team, it’s on me. I haven’t had the time needed to push PR #28296 over the line. But, I think I can push a much narrower PR to fix the bug itself in the meantime. I’ve got the code written and will test tonight before opening a PR. Hopefully the devs can merge it while the refactor moves forward in parallel.

Just for greater transparency regarding the issue raised in this thread, I’ll explain why the ecobee integration keeps breaking for all of you.

Before config entries, ecobee authentication tokens were stored in a file called ecobee.conf that was stored in the Home Assistant config directory. On HA startup, the ecobee component would read the tokens from this file and use them to authenticate. The python library took care of retrieving the tokens from ecobee, storing them in the file, and (importantly) refreshing them and updating ecobee.conf with the new tokens if the old tokens became stale. If HA was shut down, on the next startup, ecobee.conf would contain the most recent tokens as the python library had been updating that file each time the tokens were refreshed.

Moving ecobee to using config entries meant that the tokens would now be stored in the entry itself, rather than in ecobee.conf. From a logic perspective, this meant that HA itself would now need to be in charge of refreshing the tokens and updating the config entry with any refreshed tokens. Easy enough.

However, the python library still contained logic as though it was responsible for refreshing tokens. On a failed API access attempt (because of connectivity issues or otherwise), the library would (without being prompted by the logic in HA) retry the failed call but also (and the origin of the bug) attempt to refresh the authentication tokens. If this succeeded, HA would go on using the correct tokens for the time being, but, when those tokens became stale (as they do every 3600 seconds/1 hour) the attempt by HA to refresh them would fail as the tokens in the config entry had been invalidated by the issuing of new tokens to the request made by the python library, and HA was presenting those stale tokens on the refresh request.

This explains why this issue is not cropping up for everyone - if you have no connectivity issues to ecobee (i.e. calls to the API don’t fail), then the library would never independently refresh your tokens and this error would not happen.

I had previously refactored the underlying python library to fix this issue. The broader ecobee refactor was undertaken not only to clean up the component, but also to allow the component to refresh failed calls. For the time being, I think that bumping the library to the newest version (and fixing a few minor aspects of the HA refresh logic) should at least stop the error that is cropping up in this issue.

Thanks for reporting! These changes should get merged into a release soon.