Skip to content

Commit c6a8b47

Browse files
author
pos-ei-don
committed
refactor(entity): drop explicit entity_id override in complex naming
Complex naming was setting self.entity_id to force an installation-id- prefixed entity_id, which tomer-w noted was a historical mistake from before proper HA entity naming patterns were established. Since DEFAULT_SIMPLE_NAMING is True, new installations already get clean HA-derived entity_ids via simple naming. Complex naming only exists for backward compatibility; existing entities keep their already- registered entity_ids regardless. Removing the explicit self.entity_id override lets HA derive entity_ids from the entity name — the same approach simple naming uses. The unique_id in complex naming is unchanged, so entity identity is fully preserved. No display_id needed.
1 parent f86b400 commit c6a8b47

2 files changed

Lines changed: 5 additions & 24 deletions

File tree

custom_components/victron_mqtt/entity.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ def __init__(
6868
entity_id = f"{entity_platform}.{ENTITY_PREFIX}_{metric.unique_id}"
6969
else:
7070
entity_id = f"{entity_platform}.{ENTITY_PREFIX}_{installation_id}_{metric.unique_id}"
71-
# you have to set the entity_id if we want to make the unique_id different from the default entity_id generation (which is based on the entity name)
72-
self.entity_id = entity_id
7371
self._attr_unique_id = entity_id
7472
self._attr_suggested_display_precision = metric.precision
7573
# Always set translation_key so HA can resolve state/option translations (e.g. select options).

tests/test_hub.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,8 @@ async def test_sensor_complex(
376376
)
377377

378378
assert len(entities) == 2
379-
simple_naming = mock_config_entry.data[CONF_SIMPLE_NAMING]
380-
if simple_naming:
381-
day_entity_id = "select.victron_venus_ess_batterylife_schedule_charge_2_days"
382-
enabled_entity_id = "switch.victron_venus_ess_batterylife_schedule_charge_2_enabled"
383-
else:
384-
day_entity_id = "select.victron_mqtt_123_system_0_system_ess_schedule_charge_2_days"
385-
enabled_entity_id = "switch.victron_mqtt_123_system_0_system_ess_schedule_charge_2_enabled"
379+
day_entity_id = "select.victron_venus_ess_batterylife_schedule_charge_2_days"
380+
enabled_entity_id = "switch.victron_venus_ess_batterylife_schedule_charge_2_enabled"
386381

387382
day_entity = next(entity for entity in entities if entity.entity_id == day_entity_id)
388383
enabled_entity = next(
@@ -677,14 +672,7 @@ async def test_sensor_with_baseline(
677672
# Mock time.monotonic() to return a fixed time
678673
mock_time.return_value = 0
679674

680-
# The entity_id depends on the naming convention:
681-
# simple_naming: HA generates from device name + entity name
682-
# complex_naming: includes installation_id in the unique_id/entity_id
683-
simple_naming = mock_config_entry.data[CONF_SIMPLE_NAMING]
684-
if simple_naming:
685-
entity_id = "sensor.victron_venus_pv_energy"
686-
else:
687-
entity_id = "sensor.victron_mqtt_123_system_0_system_dc_pv_energy"
675+
entity_id = "sensor.victron_venus_pv_energy"
688676

689677
# Mock the restore cache with a previous state value of 1000.0
690678
mock_restore_cache(hass, [State(entity_id, "1000.0")])
@@ -711,13 +699,8 @@ async def test_sensor_with_baseline(
711699
assert state is not None
712700
assert float(state.state) == 1000.004
713701
assert len(entities) == 2
714-
simple_naming = mock_config_entry.data[CONF_SIMPLE_NAMING]
715-
if simple_naming:
716-
energy_entity_id = "sensor.victron_venus_pv_energy"
717-
power_entity_id = "sensor.victron_venus_pv_power"
718-
else:
719-
energy_entity_id = "sensor.victron_mqtt_123_system_0_system_dc_pv_energy"
720-
power_entity_id = "sensor.victron_mqtt_123_system_0_system_dc_pv_power"
702+
energy_entity_id = "sensor.victron_venus_pv_energy"
703+
power_entity_id = "sensor.victron_venus_pv_power"
721704

722705
energy_entity = next(entity for entity in entities if entity.entity_id == energy_entity_id)
723706
energy_state = hass.states.get(energy_entity.entity_id)

0 commit comments

Comments
 (0)