Fix invalid database entries with all null sensor fields (fixes #95) #98
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.
Summary
Fixes #95 - Prevents invalid database entries where all sensor fields are null by implementing validation at multiple layers.
Problem
The database was being polluted with entries where all sensor field columns are null. This occurred when HomeKit characteristic reading failed but the fallback
EntityStorageItem(entityId: entityId)was still written to the database.Solution
Implemented defense in depth across three layers:
HMCharacteristic.swift): Returnsnilon errors instead of creating empty itemsEntityStorageDbRepository.swift): Validates items before database writesHomeManager.swift): Gracefully handles validation errorsChanges
EntityStorageError.invalidDataerror typehasNoSensorDatacomputed property to validate all 16 sensor fieldsnilinstead of empty items on errorsTest Results
Test Plan
hasNoSensorDatafor all sensor typesFiles Changed
Sources/HAModels/EntityStorageItem.swift- Validation logicSources/Server/Models/EntityStorageDbRepository.swift- Repository validationSources/HAApplicationLayer/HomeManager.swift- Error handlingSources/Adapter/Extensions/HMCharacteristic.swift- Return nil on errorsTests/HomeAutomationKitTests/EntityStorageItemTests.swift- Unit tests (new)Tests/ServerTests/EntityStorageDbRepositoryTests.swift- Integration tests