nitobuendia / oura-custom-component

Oura Custom Component for Home-Assistant. Adds Oura Ring sleep information.
108 stars 25 forks source link

No such file or directory when accessing token #11

Closed nitobuendia closed 2 years ago

nitobuendia commented 2 years ago

@nitobuendia, i encountered the same issue with the token file not being found: image Fresh install of the integration today. To be fair I don't even have data directory under my config dir.

Edit: manual ssh to the HA container and creating config/data directory fixed the issue for me. Suggestion: add to the integration initialization create of useconfig/data` directory instead of failing it it's not existent :)

Thanks for the integration 💯

Originally posted by @leikoilja in https://github.com/nitobuendia/oura-custom-component/issues/6#issuecomment-986212828

nitobuendia commented 2 years ago

Hi @leikoilja - yes, your suggestion is how we "fixed" #6.

This was the code snippet that fixed it:

    os.makedirs(os.path.dirname(self.token_file_name), exist_ok=True)
    with open(self.token_file_name, 'w+') as token_file:
      token_file.write(json.dumps(access_token_data))

w+ creates the file if it doesn't exist, while the first (and new) line creates the directory path.

I think I may have missed the call from views.py (line 44), which may be happening before the api.py makes that call.


I know you've already fixed it, but would you be willing to remove the config file and test out if making the below changes fixes it? If yes, I am happy to do a small refactor to move all the token read/write together so this doesn't happen in the futrue.

The change would be changing views.py line 44 and 45 from:

    with open(token_file_name, 'w+') as token_file:
      token_file.write(json.dumps(code_data))

to:

    os.makedirs(os.path.dirname(token_file_name), exist_ok=True)
    with open(token_file_name, 'w+') as token_file:
      token_file.write(json.dumps(code_data))

If you cannot test, it's fine, I may try in a few days/weeks.

Thanks!

leikoilja commented 2 years ago

image

which is fair enough, but your patch works fine, i just tested it:

import os
...
    os.makedirs(os.path.dirname(token_file_name), exist_ok=True)
    with open(token_file_name, 'w+') as token_file:
      token_file.write(json.dumps(code_data))
nitobuendia commented 2 years ago

Thanks for testing it, @leikoilja - sending a fix now. And yes, the import was missing, thanks for adding it.

leikoilja commented 2 years ago

Thanks, @nitobuendia 💥💪🏼

nitobuendia commented 2 years ago

Note for future reference: a better way to solve it in the future would have been to move all the token file calls to a new helper module. The methods seems to already be there on api.py, but these would be shared by api.py and views.py.