Skip to content

Comments

Fix sensor unavailability when API fails - preserve previous data#20

Open
DavidBilodeau1 wants to merge 3 commits intomainfrom
DavidBilodeau1-patch-1
Open

Fix sensor unavailability when API fails - preserve previous data#20
DavidBilodeau1 wants to merge 3 commits intomainfrom
DavidBilodeau1-patch-1

Conversation

@DavidBilodeau1
Copy link
Owner

@DavidBilodeau1 DavidBilodeau1 commented Oct 14, 2025

Problem

When Home Assistant updates or the Saguenay collection API temporarily fails, the integration crashes with a TypeError: 'NoneType' object is not subscriptable error. This causes all sensors to become "unavailable" and requires users to manually remove and re-add the integration.

Error Details

Unexpected error fetching saguenay_collection data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 392, in _async_refresh
    self.data = await self._async_update_data()
  File "/config/custom_components/saguenay_collection/sensor.py", line 65, in _async_update_data
    return await self.hass.async_add_executor_job(fetch_data)
  File "/config/custom_components/saguenay_collection/sensor.py", line 57, in fetch_data
    results[schedule_type] = data["date_collecte"]
TypeError: 'NoneType' object is not subscriptable

Solution

This PR implements robust error handling that allows the integration to gracefully handle API failures and automatically recover:

Changes Made

  1. Data Validation - Added checks to validate API responses before accessing them:

    • Check if data is None
    • Check if data contains the expected date_collecte key
  2. Preserve Previous Data - When a fetch fails, the integration now keeps the last known good data instead of crashing:

    • Sensors continue showing the last successful collection date
    • No more "unavailable" state during temporary API issues
  3. Enhanced Error Handling - Added comprehensive exception handling:

    • KeyError - for missing dictionary keys
    • TypeError - for unexpected data types
    • Better handling of existing RequestException and ValueError
  4. Improved Logging - Added detailed logging for better diagnostics:

    • Warning messages when data is None or missing expected keys
    • Success messages when data is fetched correctly
    • Error messages that explain what went wrong and what action was taken
  5. Version Bump - Updated version from 1.0.5 to 1.0.6

Benefits

No more sensor unavailability - Sensors keep showing last known data during API failures
Automatic recovery - Integration automatically updates when API is working again
No manual intervention - Users no longer need to remove and re-add the integration
Better diagnostics - Improved logging helps identify and troubleshoot issues

Testing

The integration will:

  • Continue to work normally when the API is functioning
  • Gracefully handle API failures by preserving previous data
  • Automatically recover on the next update cycle (every hour) when the API is available again
  • Log appropriate warnings and errors for troubleshooting

Files Changed

  • custom_components/saguenay_collection/sensor.py - Enhanced error handling and data validation
  • custom_components/saguenay_collection/manifest.json - Version bump to 1.0.6

- Add comprehensive error handling for API failures and unexpected responses
- Preserve last known good data when fetch fails instead of crashing
- Add validation for None responses and missing 'date_collecte' key
- Add KeyError and TypeError exception handling
- Improve logging with warnings and success messages
- Bump version to 1.0.6

Fixes issue where sensors become unavailable after Home Assistant updates
or temporary API failures, requiring manual removal and re-addition of
the integration.
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