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

Update climate.py #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Update climate.py #29

wants to merge 1 commit into from

Conversation

Big-Szu
Copy link

@Big-Szu Big-Szu commented Jan 7, 2025

Improved Asynchronous Handling:

Replaced blocking HTTP requests with non-blocking aiohttp requests for better performance and integration with Home Assistant's asynchronous framework.

Session Management:

Introduced a single aiohttp.ClientSession initialized in the __init__ method to avoid repeatedly creating new sessions, which can lead to resource exhaustion.
Added a close method to properly close the session when the component is unloaded.

Token Expiry Handling:

Added logic to detect expired tokens (HTTP 401) and automatically re-authenticate by fetching a new token and retrying the failed request.

Error Handling Enhancements:

Introduced detailed exception handling for network-related issues (aiohttp.ClientError) and JSON decoding errors to prevent the component from crashing and improve error logging.

Concurrency Protection:

Added an asyncio.Lock to ensure that multiple calls to async_update do not lead to race conditions when updating thermostat data.

Content Type Validation:

Enhanced the _get_data method to handle responses with text/html content type that contain valid JSON data, ensuring robust parsing.

Code Refactoring for Logging:

Improved logging by providing more detailed and descriptive messages, including response status and error details, while avoiding logging sensitive data (e.g., tokens).

Improved Asynchronous Handling:

    Replaced blocking HTTP requests with non-blocking aiohttp requests for better performance and integration with Home Assistant's asynchronous framework.

Session Management:

    Introduced a single aiohttp.ClientSession initialized in the __init__ method to avoid repeatedly creating new sessions, which can lead to resource exhaustion.
    Added a close method to properly close the session when the component is unloaded.

Token Expiry Handling:

    Added logic to detect expired tokens (HTTP 401) and automatically re-authenticate by fetching a new token and retrying the failed request.

Error Handling Enhancements:

    Introduced detailed exception handling for network-related issues (aiohttp.ClientError) and JSON decoding errors to prevent the component from crashing and improve error logging.

Concurrency Protection:

    Added an asyncio.Lock to ensure that multiple calls to async_update do not lead to race conditions when updating thermostat data.

Content Type Validation:

    Enhanced the _get_data method to handle responses with text/html content type that contain valid JSON data, ensuring robust parsing.

Code Refactoring for Logging:

    Improved logging by providing more detailed and descriptive messages, including response status and error details, while avoiding logging sensitive data (e.g., tokens).
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.

1 participant