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

Use aiofiles with async open, await data_file #21

Merged
merged 4 commits into from
Jun 12, 2024

Conversation

autoSteve
Copy link
Collaborator

@autoSteve autoSteve commented Jun 11, 2024

The problem with using open is that this is a blocking function.

A solution can be found by utilising the 'aiofiles' library. This pull request changes the blocking open/read data file with an async aiofiles.open, along with an 'await'ed json.load() of the file.

The blocking message has disappeared for this integration for me, which makes complete sense.

Should solve #14.

@autoSteve autoSteve requested a review from BJReplay as a code owner June 11, 2024 10:57
@autoSteve autoSteve marked this pull request as draft June 11, 2024 10:59
@autoSteve autoSteve marked this pull request as ready for review June 11, 2024 10:59
@autoSteve autoSteve marked this pull request as draft June 11, 2024 11:11
@autoSteve
Copy link
Collaborator Author

There is more to this.

@autoSteve
Copy link
Collaborator Author

Early indications are good for me for proposed pull #21 using aiofiles. I am going to wait until the sun comes up and forecasts come in for further analysis. @BJReplay, #14.

@autoSteve
Copy link
Collaborator Author

This may require an "Await me maybe" logic to support earlier versions of HA. Not sure yet, @BJReplay.

@BJReplay
Copy link
Owner

This may require an "Await me maybe" logic to support earlier versions of HA. Not sure yet, @BJReplay.

I would appreciate any information you can provide about this, @autoSteve !

Using json.dump() was resulting in cache file zeroed.
@autoSteve
Copy link
Collaborator Author

On examining the need for "await maybe" I'm pretty sure using async/await always for aiofiles will be good. In all sections of the code that read/write files there are other uses of async.

I'll set this as ready for review, @BJReplay.

@autoSteve autoSteve marked this pull request as ready for review June 12, 2024 04:08
@BJReplay
Copy link
Owner

Thanks - can you confirm that you have run this code and it is working, and what version of HA it is running against?

Can you comment on whether this change is backwards compatible with earlier versions of HA?

@autoSteve
Copy link
Collaborator Author

This code is running on my HA. Version 2024.6.1, and just now updated to 2024.6.2 without issue.

My thinking is that earlier versions should be okay with it. The solcastapi.py makes use of aiohttp for async REST calls, so switching to aiofiles is very much simpatico. I downgraded to 2023.11.3 as a bit of an extreme case, and all seems well.

@autoSteve
Copy link
Collaborator Author

You want I should remove all the surplus space characters everywhere to clean things up before merge, @BJReplay?

@BJReplay
Copy link
Owner

I won't say no!

Yes, that would be really appreciated.

@autoSteve
Copy link
Collaborator Author

autoSteve commented Jun 12, 2024

@sh00t2kill and I are happy now. Excess space usage curtailed and committed, @BJReplay.

(edit) Forgot to mention that this exact version of solcastapi.py is running on my HA and tests fine.

@BJReplay BJReplay merged commit 38315cd into BJReplay:v3 Jun 12, 2024
2 checks passed
@autoSteve autoSteve deleted the Proposed-fix-for-blocking-call branch June 12, 2024 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants