Skip to content

Commit 5291bd5

Browse files
committed
Imprve handling of native_unit_of_measurement
1 parent 38efc3f commit 5291bd5

4 files changed

Lines changed: 25 additions & 11 deletions

File tree

custom_components/victron_mqtt/entity.py

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

72-
def _native_unit_of_measurement(self) -> str | None:
72+
@property
73+
def native_unit_of_measurement(self) -> str | None:
74+
"""Return the metric unit before entity registration."""
7375
if self._metric.metric_type == MetricType.COST:
7476
return self.hass.config.currency
7577

custom_components/victron_mqtt/number.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def __init__(
9292
simple_naming,
9393
installation_id,
9494
)
95-
self._attr_native_unit_of_measurement = None
9695
self._attr_device_class = METRIC_TYPE_TO_DEVICE_CLASS.get(metric.metric_type)
9796
self._attr_native_value = metric.value
9897
if metric.min_value is not None:
@@ -107,11 +106,6 @@ def _on_update_cb(self, value: Any) -> None:
107106
self._attr_native_value = value
108107
self.async_write_ha_state()
109108

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-
115109
async def async_set_native_value(self, value: float) -> None:
116110
"""Set a new value."""
117111
if TYPE_CHECKING:

custom_components/victron_mqtt/sensor.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ def __init__(
105105
super().__init__(
106106
device, metric, device_info, "sensor", simple_naming, installation_id
107107
)
108-
self._attr_native_unit_of_measurement = None
109108
self._attr_device_class = METRIC_TYPE_TO_DEVICE_CLASS.get(metric.metric_type)
110109
# Enum sensors must not have a state class
111110
if self._attr_device_class == SensorDeviceClass.ENUM:
@@ -133,8 +132,6 @@ def _normalize_value(value: Any) -> Any:
133132

134133
async def async_added_to_hass(self) -> None:
135134
"""Restore persistent state for FormulaMetric energy sensors."""
136-
self._attr_native_unit_of_measurement = self._native_unit_of_measurement()
137-
138135
# Only restore for cumulative FormulaMetric sensors (TOTAL / TOTAL_INCREASING).
139136
# These metrics start from 0 on each HA restart, so we restore the
140137
# previous accumulated value as a baseline and add new increments on top.

tests/test_hub.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,27 @@ async def test_sensor_without_unit_does_not_crash(
323323
assert state.attributes.get("unit_of_measurement") is None
324324

325325

326+
async def test_sensor_uses_registry_unit(
327+
hass: HomeAssistant,
328+
init_integration,
329+
) -> None:
330+
"""Test sensor unit is available when the entity is registered."""
331+
victron_hub, mock_config_entry = init_integration
332+
333+
await inject_message(victron_hub, "N/123/battery/0/Dc/0/Current", '{"value": 10.5}')
334+
await finalize_injection(victron_hub)
335+
await hass.async_block_till_done()
336+
337+
entity_registry = er.async_get(hass)
338+
entities = er.async_entries_for_config_entry(
339+
entity_registry, mock_config_entry.entry_id
340+
)
341+
assert len(entities) > 0
342+
343+
entity = next(entry for entry in entities if entry.translation_key == "battery_current")
344+
assert entity.unit_of_measurement == "A"
345+
346+
326347
async def test_monetary_sensor_uses_ha_currency(
327348
hass: HomeAssistant,
328349
init_integration,
@@ -340,6 +361,7 @@ async def test_monetary_sensor_uses_ha_currency(
340361
entity_registry, mock_config_entry.entry_id
341362
)
342363
assert len(entities) == 1
364+
assert entities[0].unit_of_measurement == "EUR"
343365

344366
state = hass.states.get(entities[0].entity_id)
345367
assert state is not None
@@ -1091,4 +1113,3 @@ async def test_number_with_step(
10911113
assert state is not None
10921114
assert float(state.state) == 57.6
10931115
assert state.attributes.get("step") == 0.1
1094-

0 commit comments

Comments
 (0)