Skip to content

Update __init__.py#14

Closed
IgorSimic wants to merge 3 commits into
nkgilley:masterfrom
IgorSimic:master
Closed

Update __init__.py#14
IgorSimic wants to merge 3 commits into
nkgilley:masterfrom
IgorSimic:master

Conversation

@IgorSimic
Copy link
Copy Markdown

Home assistant uses this package and calls the get_devices function one or more times, depending on how many devices are defined in the home assistant configuration. If at the moment of initialization there is no internet or the joaoapps server temporarily blocks calls, the join integration is not initialized and the only way to initialize it again is to restart the home assistant. I made a change so that the response for each api_key is saved locally and in case the internet is not available, the function returns the saved data instead of False. In this way, it is possible to restart the home assistant even in moments when the internet is not available without being left without join integration.

Please merge this pull request.
Best regards
Igor Simić

IgorSimic added 3 commits May 19, 2026 11:55
Home assistant uses this package and calls the get_devices function one or more times, depending on how many devices are defined in the home assistant configuration.
If at the moment of initialization there is no internet or the joaoapps server temporarily blocks calls, the join integration is not initialized and the only way to initialize it again is to restart the home assistant.
I made a change so that the response for each api_key is saved locally and in case the internet is not available, the function returns the saved data instead of False.
In this way, it is possible to restart the home assistant even in moments when the internet is not available without being left without join integration.
Fixed typo at the end of get_devices function
Forgotten import
@nkgilley
Copy link
Copy Markdown
Owner

Hi Igor,

Thank you very much for submitting this pull request! Improving offline resilience is a fantastic idea, and you are absolutely right that temporary network outages or API rate blocks during startup shouldn't completely prevent Home Assistant from initializing the Join integration.

I like the concept, but during code review, I identified a few edge cases and implementation bugs that we need to address before this can be merged safely:

1. Unhandled Connection Exceptions

When there is no active internet connection, calling requests.get() throws a network exception (such as requests.exceptions.ConnectionError or Timeout). Because this call isn't wrapped in a try...except block, the exception will propagate and crash the application before it ever hits the else block to load from the cache.

2. Read-Only / Ephemeral Filesystem Issues

Using os.path.dirname(__file__) attempts to save cache files directly inside the Python package installation directory. In many standard setups (such as Home Assistant OS, Docker containers, or system-wide pip installs), package directories are mounted as read-only.

  • If the directory is read-only, attempting to write the cache file when the API is successful will throw a PermissionError and crash the entire function, breaking normal online operation.
  • Additionally, package directories are ephemeral in containerized environments and will be wiped out whenever the container is rebuilt or restarted.

3. API Keys in Filenames (Security & OS Compatibility)

Saving files using the raw api_key as the filename exposes sensitive credentials in plaintext. It can also cause issues if the API key contains characters that are illegal or unsafe on certain filesystems. Hashing the key (e.g., using SHA-256) is a safer, cleaner approach.


Suggested Implementation

To make this completely safe for all environments, we should:

  1. Wrap the API and JSON decoding requests in a try...except block.
  2. Resolve a safe, writable cache directory (like the user home cache or temp directory) and fall back gracefully if writing fails.
  3. Hash the API key to generate a secure, filesystem-safe filename.
  4. Catch all file operation errors and fail silently so cache directory issues never crash normal operation.

@nkgilley nkgilley closed this May 22, 2026
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