Skip to content

Commit 4dda7f5

Browse files
committed
Use system currency for EVCS session cost (#430, #173718)
- Add MetricType.COST -> SensorDeviceClass.MONETARY mapping - Use hass.config.currency via property instead of hardcoded dollar sign - Add MetricType.COST to DEVICE_CLASS_METRIC_TYPES in merge script
1 parent fa2c4ad commit 4dda7f5

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

.github/scripts/merge_topics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def main():
4444
"MetricType.PRESSURE",
4545
"MetricType.DISTANCE",
4646
"MetricType.POWER_FACTOR",
47+
"MetricType.COST",
4748
"MetricType.SPEED",
4849
"MetricType.LIQUID_VOLUME",
4950
"MetricType.DURATION",

custom_components/victron_mqtt/sensor.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
MetricType.PRESSURE: SensorDeviceClass.PRESSURE,
4343
MetricType.DISTANCE: SensorDeviceClass.DISTANCE,
4444
MetricType.POWER_FACTOR: SensorDeviceClass.POWER_FACTOR,
45+
MetricType.COST: SensorDeviceClass.MONETARY,
4546
MetricType.SPEED: SensorDeviceClass.SPEED,
4647
MetricType.LIQUID_VOLUME: SensorDeviceClass.VOLUME_STORAGE,
4748
MetricType.DURATION: SensorDeviceClass.DURATION,
@@ -111,9 +112,19 @@ def __init__(
111112
self._attr_state_class = METRIC_NATURE_TO_STATE_CLASS.get(
112113
metric.metric_nature
113114
)
114-
self._set_unit_translation()
115+
# MONETARY uses hass.config.currency which isn't available in __init__,
116+
# so it's handled by the native_unit_of_measurement property instead.
117+
if self._attr_device_class != SensorDeviceClass.MONETARY:
118+
self._set_unit_translation()
115119
self._attr_native_value = VictronSensor._normalize_value(metric.value)
116120

121+
@property
122+
def native_unit_of_measurement(self) -> str | None:
123+
"""Return the unit of measurement."""
124+
if self._attr_device_class == SensorDeviceClass.MONETARY:
125+
return self.hass.config.currency
126+
return self._attr_native_unit_of_measurement
127+
117128
@callback
118129
def _on_update_cb(self, value: Any) -> None:
119130
if self._baseline is not None:

0 commit comments

Comments
 (0)