File tree Expand file tree Collapse file tree
custom_components/victron_mqtt Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -90,10 +90,9 @@ def __init__(
9090 metric .generic_short_id not in ENTITIES_DISABLE_BY_DEFAULT
9191 )
9292
93- @property
94- def native_unit_of_measurement (self ) -> str | None :
95- """Return the metric unit before entity registration."""
96- if self ._metric .metric_type == MetricType .COST :
93+ def _resolve_native_unit_of_measurement (self ) -> str | None :
94+ """Resolve native unit of measurement for platforms that support it."""
95+ if self ._metric .metric_type is MetricType .COST :
9796 return self .hass .config .currency
9897
9998 unit_of_measurement = self ._metric .unit_of_measurement
Original file line number Diff line number Diff line change @@ -101,6 +101,11 @@ def __init__(
101101 if metric .step is not None :
102102 self ._attr_native_step = metric .step
103103
104+ @property
105+ def native_unit_of_measurement (self ) -> str | None :
106+ """Return the native unit of measurement."""
107+ return self ._resolve_native_unit_of_measurement ()
108+
104109 @callback
105110 def _on_update_cb (self , value : Any ) -> None :
106111 self ._attr_native_value = value
Original file line number Diff line number Diff line change @@ -115,6 +115,11 @@ def __init__(
115115 )
116116 self ._attr_native_value = VictronSensor ._normalize_value (metric .value )
117117
118+ @property
119+ def native_unit_of_measurement (self ) -> str | None :
120+ """Return the native unit of measurement."""
121+ return self ._resolve_native_unit_of_measurement ()
122+
118123 @callback
119124 def _on_update_cb (self , value : Any ) -> None :
120125 if self ._baseline is not None :
Original file line number Diff line number Diff line change @@ -358,6 +358,29 @@ async def test_monetary_sensor_uses_ha_currency(
358358 assert state .attributes .get ("unit_of_measurement" ) == "EUR"
359359
360360
361+ async def test_native_unit_of_measurement_special_unit (
362+ hass : HomeAssistant ,
363+ init_integration ,
364+ ) -> None :
365+ """Test special units like % are exposed as native units."""
366+ victron_hub , mock_config_entry = init_integration
367+
368+ await inject_message (victron_hub , "N/123/battery/0/Soc" , '{"value": 85}' )
369+ await finalize_injection (victron_hub )
370+ await hass .async_block_till_done ()
371+
372+ entity_registry = er .async_get (hass )
373+ entities = er .async_entries_for_config_entry (
374+ entity_registry , mock_config_entry .entry_id
375+ )
376+ entity = next ((entry for entry in entities if entry .unit_of_measurement == "%" ), None )
377+ assert entity is not None
378+
379+ state = hass .states .get (entity .entity_id )
380+ assert state is not None
381+ assert state .attributes ["unit_of_measurement" ] == "%"
382+
383+
361384async def test_sensor_complex (
362385 hass : HomeAssistant ,
363386 init_integration ,
You can’t perform that action at this time.
0 commit comments