hass.tibber_prices/custom_components/tibber_prices/sensor
Julian Pawlowski 98512672ae feat(lifecycle): implement HA entity best practices for state management
Implemented comprehensive entity lifecycle patterns following Home Assistant
best practices for proper state management and history tracking.
Changes:
- entity.py: Added available property to base class
  - Returns False when coordinator has no data or last_update_success=False
  - Prevents entities from showing stale data during errors
  - Auth failures trigger reauth flow via ConfigEntryAuthFailed

- sensor/core.py: Added state restore and background task handling
  - Changed inheritance: SensorEntity → RestoreSensor
  - Restore native_value from SensorExtraStoredData in async_added_to_hass()
  - Chart sensors restore response data from attributes
  - Converted blocking service calls to background tasks using hass.async_create_task()
  - Eliminates 194ms setup warning by making async_added_to_hass non-blocking

- binary_sensor/core.py: Added state restore and force_update
  - Changed inheritance: BinarySensorEntity → RestoreEntity + BinarySensorEntity
  - Restore is_on state in async_added_to_hass()
  - Added available property override for connection sensor (always True)
  - Added force_update property for connection sensor to track all state changes
  - Other binary sensors use base available logic

- AGENTS.md: Documented entity lifecycle patterns in Common Pitfalls
  - Added "Entity Lifecycle & State Management" section
  - Documents available, state restore, and force_update patterns
  - Explains why each pattern matters for proper HA integration

Impact: Entities no longer show stale data during errors, history has no gaps
after HA restart, connection state changes are properly tracked, and config
entry setup completes in <200ms (under HA threshold).

All patterns verified against HA developer documentation:
https://developers.home-assistant.io/docs/core/entity/
2025-12-07 17:24:41 +00:00
..
attributes refactor(price_info): price data handling to use unified interval retrieval 2025-11-24 10:49:34 +00:00
calculators refactor: simplify needs_tomorrow_data() - remove tomorrow_date parameter 2025-11-24 16:26:08 +00:00
__init__.py fix(imports): update imports after utils package reorganization 2025-11-18 20:07:28 +00:00
chart_data.py feat(sensors): add chart_metadata sensor for lightweight chart configuration 2025-12-05 20:30:54 +00:00
chart_metadata.py refactor(services): remove gradient_stop, use fixed 50% gradient 2025-12-05 20:51:30 +00:00
core.py feat(lifecycle): implement HA entity best practices for state management 2025-12-07 17:24:41 +00:00
definitions.py feat(sensors): add chart_metadata sensor for lightweight chart configuration 2025-12-05 20:30:54 +00:00
helpers.py refactor(price_info): price data handling to use unified interval retrieval 2025-11-24 10:49:34 +00:00
types.py feat(types): add TypedDict documentation and BaseCalculator helpers 2025-11-22 14:32:24 +00:00
value_getters.py feat(sensors): add chart_metadata sensor for lightweight chart configuration 2025-12-05 20:30:54 +00:00