Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions homeassistant/components/victron_gx/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ def __init__(
self._attr_device_info = device_info
self._attr_unique_id = f"{installation_id}_{metric.unique_id}"
self._attr_suggested_display_precision = metric.precision
# When main_topic is set, omit translation_key/name so HA uses the device name (via _attr_has_entity_name).
# Always set translation_key so HA can resolve state/option translations (e.g. select options).
self._attr_translation_key = metric.generic_short_id.replace("{", "").replace(
Comment thread
tomer-w marked this conversation as resolved.
"}", ""
)
self._attr_translation_placeholders = metric.key_values
# When main_topic is set, override name to None so HA uses the device name (via _attr_has_entity_name).
if metric.main_topic:
self._attr_name = None
else:
self._attr_translation_key = metric.generic_short_id.replace(
"{", ""
).replace("}", "")
self._attr_translation_placeholders = metric.key_values

# Special case for "%" as it should not be coming from the localization file
self._attr_native_unit_of_measurement = (
Expand Down
9 changes: 6 additions & 3 deletions homeassistant/components/victron_gx/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ def _map_device_info(
model=device.model,
serial_number=device.serial_number,
)
# Don't set via_device for the GX device itself
if device.unique_id != "system_0":
device_info["via_device"] = (DOMAIN, f"{installation_id}_system_0")
# Set via_device based on parent_device relationship
if device.parent_device is not None:
device_info["via_device"] = (
DOMAIN,
f"{installation_id}_{device.parent_device.unique_id}",
)
Comment thread
tomer-w marked this conversation as resolved.
Outdated
return device_info

def register_new_metric_callback(
Expand Down
36 changes: 36 additions & 0 deletions tests/components/victron_gx/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,39 @@ async def test_victron_select_actions(
state = hass.states.get(entity_id)
assert state is not None
assert state.state == "auto"


async def test_select_main_topic_has_translation_key(
hass: HomeAssistant,
init_integration: tuple[VictronVenusHub, MockConfigEntry],
entity_registry: er.EntityRegistry,
) -> None:
"""Test that select entities with main_topic=True still have translation_key set.

Regression test: previously main_topic entities skipped setting translation_key,
breaking HA state/option translations.
"""
victron_hub, mock_config_entry = init_integration

# vebus Mode has main_topic=True
await inject_message(
victron_hub,
f"N/{MOCK_INSTALLATION_ID}/vebus/289/Mode",
'{"value": 3}',
)
await finalize_injection(victron_hub)
await hass.async_block_till_done()

entities = er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)

assert len(entities) == 1
entity = entities[0]
# translation_key must be set even for main_topic entities
assert entity.translation_key is not None, (
"main_topic entity must have translation_key"
)
assert entity.translation_key == "vebus_inverter_mode"
# original_name should be None (uses device name via has_entity_name)
assert entity.original_name is None
5 changes: 2 additions & 3 deletions tests/components/victron_gx/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async def test_victron_main_topic_sensor(
init_integration: tuple[VictronVenusHub, MockConfigEntry],
entity_registry: er.EntityRegistry,
) -> None:
"""Test sensor whose metric has main_topic=True uses name instead of translation key."""
"""Test sensor whose metric has main_topic=True keeps translation key and uses device name."""
victron_hub, mock_config_entry = init_integration

# Multi RS MPPT MppOperationMode is a main_topic metric
Expand All @@ -130,8 +130,7 @@ async def test_victron_main_topic_sensor(
assert len(entities) == 1
entity = entities[0]
assert entity.unique_id == f"{MOCK_INSTALLATION_ID}_multi_0_multi_mppt_1_state"
# main_topic entities get their name from the device, not a translation key
assert entity.translation_key is None
assert entity.translation_key == "multi_mppt_mpptnumber_state"

state = hass.states.get(entity.entity_id)
assert state is not None
Expand Down
Loading