Skip to content

Conversation

@JulianKahnert
Copy link
Owner

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:

  1. Source Layer (HMCharacteristic.swift): Returns nil on errors instead of creating empty items
  2. Persistence Layer (EntityStorageDbRepository.swift): Validates items before database writes
  3. Application Layer (HomeManager.swift): Gracefully handles validation errors

Changes

  • ✅ Added EntityStorageError.invalidData error type
  • ✅ Added hasNoSensorData computed property to validate all 16 sensor fields
  • ✅ Repository validation rejects items with all nil fields
  • ✅ HomeManager catches validation errors (debug level, not critical)
  • ✅ HMCharacteristic returns nil instead of empty items on errors
  • ✅ 17 new unit tests covering all sensor field types
  • ✅ Integration tests for repository validation

Test Results

✅ SwiftLint passed (auto-fixed)
✅ swift build - successful
✅ swift test - all 52 tests passed

Test Plan

  • Unit tests verify hasNoSensorData for all sensor types
  • Repository validation tests confirm invalid items are rejected
  • Valid items with sensor data still persist correctly
  • Error handling prevents assertionFailure for expected validation errors
  • Manual test: Disconnect HomeKit device and verify no invalid DB entries created
  • Manual test: Reconnect device and verify valid data is stored

Files Changed

  • Sources/HAModels/EntityStorageItem.swift - Validation logic
  • Sources/Server/Models/EntityStorageDbRepository.swift - Repository validation
  • Sources/HAApplicationLayer/HomeManager.swift - Error handling
  • Sources/Adapter/Extensions/HMCharacteristic.swift - Return nil on errors
  • Tests/HomeAutomationKitTests/EntityStorageItemTests.swift - Unit tests (new)
  • Tests/ServerTests/EntityStorageDbRepositoryTests.swift - Integration tests

- Remove point markers from chart for cleaner visualization
- Add chart selection support with hover interaction
- Display value tooltip and vertical rule when hovering over chart
- Show point marker only at selected position
Resolves #95

This fix prevents EntityStorageItem objects with all sensor fields set
to nil from being written to the database. Empty items were previously
created when HomeKit characteristic reading failed, polluting the
database with meaningless data.

Changes:
- Add hasNoSensorData validation method to EntityStorageItem to detect
  items with all nil sensor fields
- Add validation in EntityStorageDbRepository.add() that throws
  EntityStorageError.invalidData for empty items
- Update HomeManager to catch and handle validation errors gracefully
  with debug-level logging
- Update HMCharacteristic.getEntityStorageItem() to return nil instead
  of creating empty EntityStorageItem when errors occur
- Add comprehensive unit tests for validation logic covering all 16
  sensor field types
- Add EntityStorageError.invalidData case for validation failures

The fix implements defense in depth:
1. Source layer (HMCharacteristic): Returns nil on error instead of
   creating empty items
2. Persistence layer (EntityStorageDbRepository): Validates items
   before database write
3. Application layer (HomeManager): Handles validation errors without
   triggering assertions

All tests pass and Swift package builds successfully.
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.

Fix invalid database entries with all null sensor fields

2 participants