Skip to content

Commit d2b3659

Browse files
committed
Align custom integration unit handling with core integration
- Move unit resolution to _native_unit_of_measurement in base entity - Resolve COST units from HA currency in base helper - Set native unit in async_added_to_hass for sensor/number - Add NumberDeviceClass.MONETARY mapping for MetricType.COST
1 parent f5f1e09 commit d2b3659

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

custom_components/victron_mqtt/entity.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ def __init__(
6969
metric.generic_short_id not in ENTITIES_DISABLE_BY_DEFAULT
7070
)
7171

72-
def _set_unit_translation(self) -> None:
72+
def _native_unit_of_measurement(self) -> str | None:
73+
if self._metric.metric_type == MetricType.COST:
74+
return self.hass.config.currency
75+
7376
unit_of_measurement = self._metric.unit_of_measurement
74-
# We need to set the _attr_native_unit_of_measurement in three cases:
77+
# We need to provide a native unit in three cases:
7578
if (
7679
# 1. Special units which will never need a translation and therefore will not be included in the translation file.
7780
unit_of_measurement in SPECIAL_NATIVE_UNITS
@@ -85,7 +88,9 @@ def _set_unit_translation(self) -> None:
8588
# entry, so we must set the unit programmatically.
8689
or self._metric.metric_type == MetricType.DYNAMIC
8790
):
88-
self._attr_native_unit_of_measurement = unit_of_measurement
91+
return unit_of_measurement
92+
93+
return None
8994

9095
@callback
9196
@abstractmethod

custom_components/victron_mqtt/number.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
MetricType.PRESSURE: NumberDeviceClass.PRESSURE,
3434
MetricType.DISTANCE: NumberDeviceClass.DISTANCE,
3535
MetricType.POWER_FACTOR: NumberDeviceClass.POWER_FACTOR,
36+
MetricType.COST: NumberDeviceClass.MONETARY,
3637
MetricType.SPEED: NumberDeviceClass.SPEED,
3738
MetricType.LIQUID_VOLUME: NumberDeviceClass.VOLUME_STORAGE,
3839
MetricType.DURATION: NumberDeviceClass.DURATION,
@@ -91,8 +92,8 @@ def __init__(
9192
simple_naming,
9293
installation_id,
9394
)
95+
self._attr_native_unit_of_measurement = None
9496
self._attr_device_class = METRIC_TYPE_TO_DEVICE_CLASS.get(metric.metric_type)
95-
self._set_unit_translation()
9697
self._attr_native_value = metric.value
9798
if metric.min_value is not None:
9899
self._attr_native_min_value = metric.min_value
@@ -106,6 +107,11 @@ def _on_update_cb(self, value: Any) -> None:
106107
self._attr_native_value = value
107108
self.async_write_ha_state()
108109

110+
async def async_added_to_hass(self) -> None:
111+
"""Run when entity about to be added to hass."""
112+
self._attr_native_unit_of_measurement = self._native_unit_of_measurement()
113+
await super().async_added_to_hass()
114+
109115
async def async_set_native_value(self, value: float) -> None:
110116
"""Set a new value."""
111117
if TYPE_CHECKING:

custom_components/victron_mqtt/sensor.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ def __init__(
113113
self._attr_state_class = METRIC_NATURE_TO_STATE_CLASS.get(
114114
metric.metric_nature
115115
)
116-
# MONETARY uses hass.config.currency which isn't available in __init__.
117-
if self._attr_device_class != SensorDeviceClass.MONETARY:
118-
self._set_unit_translation()
119116
self._attr_native_value = VictronSensor._normalize_value(metric.value)
120117

121118
@callback
@@ -135,8 +132,7 @@ def _normalize_value(value: Any) -> Any:
135132

136133
async def async_added_to_hass(self) -> None:
137134
"""Restore persistent state for FormulaMetric energy sensors."""
138-
if self._attr_device_class == SensorDeviceClass.MONETARY:
139-
self._attr_native_unit_of_measurement = self.hass.config.currency
135+
self._attr_native_unit_of_measurement = self._native_unit_of_measurement()
140136

141137
# Only restore for cumulative FormulaMetric sensors (TOTAL / TOTAL_INCREASING).
142138
# These metrics start from 0 on each HA restart, so we restore the

0 commit comments

Comments
 (0)