Skip to content

Commit f26561b

Browse files
committed
Fix monetary rate sensor state class
1 parent 47b52a1 commit f26561b

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

custom_components/dte_rates/sensor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections import defaultdict
44
from decimal import Decimal
55

6-
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity, SensorStateClass
6+
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
77
from homeassistant.components import persistent_notification
88
from homeassistant.config_entries import ConfigEntry
99
from homeassistant.core import HomeAssistant, callback
@@ -76,7 +76,6 @@ async def async_setup_entry(
7676
class _DteBaseRateSensor(CoordinatorEntity, SensorEntity):
7777
_attr_native_unit_of_measurement = "USD/kWh"
7878
_attr_device_class = SensorDeviceClass.MONETARY
79-
_attr_state_class = SensorStateClass.MEASUREMENT
8079

8180
def __init__(self, coordinator, entry: ConfigEntry) -> None:
8281
super().__init__(coordinator)

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Current research notes:
1212

1313
- `research/rider18-export-rates.md` - Rider 18 export-rate source mapping and PSCR formula decisions.
1414
- `research/issue-3-rate-modifiers.md` - PSCR/tax modifier defaults and calculation scope for real out-of-pocket rates.
15+
- `research/issue-5-entity-type.md` - Home Assistant monetary sensor state-class warning and entity metadata decision.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Issue 5 Entity Type Warning
2+
3+
## Source
4+
5+
- GitHub issue: https://github.com/javaDevJT/DTE-Rates-for-Home-Assistant/issues/5
6+
- Title: Entity type issue
7+
- Created: 2026-05-29
8+
9+
## Report
10+
11+
Home Assistant logs warnings for `DteImportRateSensor` and `DteExportRateSensor`:
12+
13+
`device_class: monetary` was combined with `state_class: measurement`.
14+
15+
Current Home Assistant sensor validation rejects that combination. Monetary sensors may have no state class, or use state class `total` when the entity represents a total amount. These rate sensors represent a current price in `USD/kWh`, not an accumulating monetary total.
16+
17+
## Decision
18+
19+
- Keep `device_class: monetary`.
20+
- Keep `native_unit_of_measurement: USD/kWh`.
21+
- Remove `state_class` from the import/export price sensors.
22+
23+
This preserves the current-price entity shape while avoiding invalid long-term statistics metadata.
24+
25+
## Reference
26+
27+
- Home Assistant sensor developer docs: https://developers.home-assistant.io/docs/core/entity/sensor/

tests/test_sensor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ def test_import_sensor_returns_default_out_of_pocket_rate(monkeypatch):
8484
}
8585

8686

87+
def test_rate_price_sensors_do_not_set_invalid_monetary_state_class(monkeypatch):
88+
monkeypatch.setattr("custom_components.dte_rates.sensor.dt_util.now", lambda: datetime(2026, 3, 1, 12, 0))
89+
90+
coordinator = _coordinator_with_rate()
91+
entry = SimpleNamespace(entry_id="entry_18", data={CONF_SELECTED_RATE: "D1.11", CONF_NET_METERING: False})
92+
93+
import_sensor = DteImportRateSensor(coordinator, entry)
94+
export_sensor = DteExportRateSensor(coordinator, entry)
95+
96+
assert getattr(import_sensor, "_attr_device_class", None) == "monetary"
97+
assert getattr(export_sensor, "_attr_device_class", None) == "monetary"
98+
assert import_sensor.native_unit_of_measurement == "USD/kWh"
99+
assert export_sensor.native_unit_of_measurement == "USD/kWh"
100+
assert getattr(import_sensor, "_attr_state_class", None) is None
101+
assert getattr(export_sensor, "_attr_state_class", None) is None
102+
103+
87104
def test_import_sensor_can_omit_modifiers(monkeypatch):
88105
monkeypatch.setattr("custom_components.dte_rates.sensor.dt_util.now", lambda: datetime(2026, 3, 1, 12, 0))
89106

0 commit comments

Comments
 (0)