Skip to content

Commit 877f133

Browse files
committed
feat: Add long-term statistics support for performance sensors
- Changed state_class from "total" to "measurement" for all performance sensors - Enables historical tracking beyond default 10-day retention period - Supports trend visualization and statistical analysis - Compatible with negative performance values (losses) Affects: - SaxoInvestmentPerformanceSensor (all-time) - SaxoYTDInvestmentPerformanceSensor - SaxoMonthInvestmentPerformanceSensor - SaxoQuarterInvestmentPerformanceSensor - SaxoAccumulatedProfitLossSensor Closes #10
1 parent 4a70805 commit 877f133

5 files changed

Lines changed: 42 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.7.0] - 2026-02-10
9+
10+
### Added
11+
- **Long-Term Statistics Support**: Performance sensors now support Home Assistant's long-term statistics system ([#10](https://github.com/steynovich/ha-saxo-portfolio/issues/10))
12+
- Changed `state_class` from `"total"` to `"measurement"` for all performance sensors
13+
- Enables historical tracking beyond the default 10-day recorder retention period
14+
- Supports trend visualization in History/Energy panels and statistical analysis
15+
- Compatible with negative performance values (losses)
16+
- Affects all 5 performance sensors:
17+
- `SaxoInvestmentPerformanceSensor` (all-time)
18+
- `SaxoYTDInvestmentPerformanceSensor`
19+
- `SaxoMonthInvestmentPerformanceSensor`
20+
- `SaxoQuarterInvestmentPerformanceSensor`
21+
- `SaxoAccumulatedProfitLossSensor`
22+
23+
### Changed
24+
- **Performance Sensor Configuration**: All performance sensors now use `state_class="measurement"` with `suggested_display_precision=2`
25+
- **SaxoPerformanceSensorBase**: Added default `state_class` and display precision configuration
26+
- **SaxoAccumulatedProfitLossSensor**: Changed from `state_class="total"` to `state_class="measurement"`
27+
28+
### Technical Details
29+
- Updated `SaxoPerformanceSensorBase.__init__()` to set `state_class="measurement"` and `suggested_display_precision=2`
30+
- Updated `SaxoAccumulatedProfitLossSensor.__init__()` to use `state_class="measurement"`
31+
- Home Assistant `state_class="measurement"` allows both positive and negative values, suitable for investment performance tracking
32+
- No breaking changes - existing functionality preserved, statistics tracking now enabled
33+
834
## [2.6.0] - 2026-02-03
935

1036
### Added
@@ -1260,6 +1286,7 @@ Condition #4 ensures reload only happens AFTER initial setup completes, when sen
12601286
- Modular data models for type safety and consistency
12611287
- Proper Home Assistant integration patterns
12621288

1289+
[2.7.0]: https://github.com/steynovich/ha-saxo-portfolio/releases/tag/v2.7.0
12631290
[2.6.0]: https://github.com/steynovich/ha-saxo-portfolio/releases/tag/v2.6.0
12641291
[2.2.1]: https://github.com/steynovich/ha-saxo-portfolio/releases/tag/v2.2.1
12651292
[2.2.0]: https://github.com/steynovich/ha-saxo-portfolio/releases/tag/v2.2.0

CLAUDE.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ python -m pytest tests/ # Run tests
4545
- `SaxoMonthInvestmentPerformanceSensor`, `SaxoQuarterInvestmentPerformanceSensor`
4646
- `SaxoAccumulatedProfitLossSensor` (from `/hist/v3/perf/`)
4747
- `SaxoCashTransferBalanceSensor`
48+
- All performance sensors support long-term statistics (`state_class="measurement"`)
4849

4950
### Diagnostic Sensors
5051
- `SaxoClientIDSensor`, `SaxoAccountIDSensor`, `SaxoNameSensor`
@@ -68,11 +69,12 @@ Device: `"Saxo {ClientId} Portfolio"`
6869
- `SaxoSensorBase` - Common functionality for all sensors
6970
- `SaxoBalanceSensorBase` - Monetary sensors with currency handling, `state_class="total"`
7071
- `SaxoDiagnosticSensorBase` - EntityCategory.DIAGNOSTIC
71-
- `SaxoPerformanceSensorBase` - Time period handling and caching
72+
- `SaxoPerformanceSensorBase` - Time period handling, caching, and long-term statistics (`state_class="measurement"`)
7273

7374
### Key Behaviors
7475
- **Market Hours**: 5 min updates during market hours, 30 min after hours
7576
- **Performance Caching**: 2-hour cache for performance data
77+
- **Long-Term Statistics**: Performance sensors support HA statistics for historical tracking
7678
- **Sticky Availability**: Sensors stay available during updates, unavailable after 15+ min failures
7779
- **Graceful Degradation**: Performance API failures don't block balance data
7880
- **GUI Reauthentication**: OAuth reauth without removing integration

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ A **Platinum-grade** Home Assistant integration for monitoring your Saxo Bank po
1414
- 💰 **Nine Portfolio Sensors**: Real-time balance, performance metrics, and cash transfer tracking from multiple Saxo API endpoints
1515
- 📊 **Seven Diagnostic Sensors**: Built-in monitoring for integration health, account identification, token expiry, and market status
1616
-**Smart Performance Caching**: Performance data updates hourly while balance data remains real-time for optimal API usage
17+
- 📈 **Long-Term Statistics**: Performance sensors support Home Assistant statistics for historical tracking and trend analysis
1718
- 🏷️ **Automatic Entity Naming**: Entity names auto-generated using your Saxo Client ID (e.g., `saxo_123456_cash_balance`)
1819
- 🕒 **Intelligent Scheduling**: Configurable market timezone with dynamic update intervals (5 min during market hours, 30 min after hours)
1920
- 📊 **Performance Analytics**: All-time profit/loss, investment returns, and cash transfer balance tracking
@@ -40,6 +41,12 @@ The integration provides **nine comprehensive sensors** that automatically use y
4041
- **Quarter Investment Performance**: Quarter-to-Date portfolio return percentage (`sensor.saxo_{clientid}_quarter_investment_performance`)
4142
- **Cash Transfer Balance**: Latest cash transfer value tracking deposits and withdrawals (`sensor.saxo_{clientid}_cash_transfer_balance`)
4243

44+
**Long-Term Statistics**: All performance sensors support Home Assistant's long-term statistics system with `state_class: measurement`, enabling:
45+
- Historical data retention beyond the default 10-day recorder purge period
46+
- Trend visualization in History and Energy panels
47+
- Statistical analysis with min, max, and mean values
48+
- Support for both positive and negative performance values
49+
4350
### Key Features
4451
- **API Endpoints Used**: `/port/v1/balances/me`, `/port/v1/clients/me`, `/port/v1/accounts/{AccountKey}`, `/hist/v3/perf/`, `/hist/v4/performance/timeseries`
4552
- **Currency Support**: Automatically detects and displays the appropriate currency unit

custom_components/saxo_portfolio/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"iot_class": "cloud_polling",
1010
"issue_tracker": "https://github.com/steynovich/ha-saxo-portfolio/issues",
1111
"requirements": ["saxo-openapi>=0.6.0"],
12-
"version": "2.6.0"
12+
"version": "2.7.0"
1313
}

custom_components/saxo_portfolio/sensor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ def __init__(self, coordinator: SaxoCoordinator) -> None:
427427
icon="mdi:trending-up",
428428
unit_of_measurement=coordinator.get_currency(),
429429
)
430-
self._attr_state_class = "total"
430+
self._attr_state_class = "measurement"
431+
self._attr_suggested_display_precision = 2
431432

432433
@property
433434
def native_value(self) -> StateType:
@@ -493,6 +494,8 @@ def __init__(
493494
unit_of_measurement="%",
494495
)
495496
self._data_key = data_key
497+
self._attr_state_class = "measurement"
498+
self._attr_suggested_display_precision = 2
496499

497500
@property
498501
def native_value(self) -> StateType:

0 commit comments

Comments
 (0)