Fix sensor unavailability when API fails - preserve previous data#20
Open
DavidBilodeau1 wants to merge 3 commits intomainfrom
Open
Fix sensor unavailability when API fails - preserve previous data#20DavidBilodeau1 wants to merge 3 commits intomainfrom
DavidBilodeau1 wants to merge 3 commits intomainfrom
Conversation
- 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.
…lection into DavidBilodeau1-patch-1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When Home Assistant updates or the Saguenay collection API temporarily fails, the integration crashes with a
TypeError: 'NoneType' object is not subscriptableerror. This causes all sensors to become "unavailable" and requires users to manually remove and re-add the integration.Error Details
Solution
This PR implements robust error handling that allows the integration to gracefully handle API failures and automatically recover:
Changes Made
Data Validation - Added checks to validate API responses before accessing them:
dataisNonedatacontains the expecteddate_collectekeyPreserve Previous Data - When a fetch fails, the integration now keeps the last known good data instead of crashing:
Enhanced Error Handling - Added comprehensive exception handling:
KeyError- for missing dictionary keysTypeError- for unexpected data typesRequestExceptionandValueErrorImproved Logging - Added detailed logging for better diagnostics:
Noneor missing expected keysVersion 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:
Files Changed
custom_components/saguenay_collection/sensor.py- Enhanced error handling and data validationcustom_components/saguenay_collection/manifest.json- Version bump to 1.0.6