Skip to content

Commit 16957b7

Browse files
pos-ei-donpos-ei-don
andauthored
refactor(entity): drop explicit entity_id override in complex naming (#448)
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. Co-authored-by: pos-ei-don <philipp.wacker@ibf-solutions.com>
1 parent 8fd9508 commit 16957b7

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
@@ -399,13 +399,8 @@ async def test_sensor_complex(
399399
)
400400

401401
assert len(entities) == 2
402-
simple_naming = mock_config_entry.data[CONF_SIMPLE_NAMING]
403-
if simple_naming:
404-
day_entity_id = "select.victron_venus_ess_batterylife_schedule_charge_2_days"
405-
enabled_entity_id = "switch.victron_venus_ess_batterylife_schedule_charge_2_enabled"
406-
else:
407-
day_entity_id = "select.victron_mqtt_123_system_0_system_ess_schedule_charge_2_days"
408-
enabled_entity_id = "switch.victron_mqtt_123_system_0_system_ess_schedule_charge_2_enabled"
402+
day_entity_id = "select.victron_venus_ess_batterylife_schedule_charge_2_days"
403+
enabled_entity_id = "switch.victron_venus_ess_batterylife_schedule_charge_2_enabled"
409404

410405
day_entity = next(entity for entity in entities if entity.entity_id == day_entity_id)
411406
enabled_entity = next(
@@ -700,14 +695,7 @@ async def test_sensor_with_baseline(
700695
# Mock time.monotonic() to return a fixed time
701696
mock_time.return_value = 0
702697

703-
# The entity_id depends on the naming convention:
704-
# simple_naming: HA generates from device name + entity name
705-
# complex_naming: includes installation_id in the unique_id/entity_id
706-
simple_naming = mock_config_entry.data[CONF_SIMPLE_NAMING]
707-
if simple_naming:
708-
entity_id = "sensor.victron_venus_pv_energy"
709-
else:
710-
entity_id = "sensor.victron_mqtt_123_system_0_system_dc_pv_energy"
698+
entity_id = "sensor.victron_venus_pv_energy"
711699

712700
# Mock the restore cache with a previous state value of 1000.0
713701
mock_restore_cache(hass, [State(entity_id, "1000.0")])
@@ -734,13 +722,8 @@ async def test_sensor_with_baseline(
734722
assert state is not None
735723
assert float(state.state) == 1000.004
736724
assert len(entities) == 2
737-
simple_naming = mock_config_entry.data[CONF_SIMPLE_NAMING]
738-
if simple_naming:
739-
energy_entity_id = "sensor.victron_venus_pv_energy"
740-
power_entity_id = "sensor.victron_venus_pv_power"
741-
else:
742-
energy_entity_id = "sensor.victron_mqtt_123_system_0_system_dc_pv_energy"
743-
power_entity_id = "sensor.victron_mqtt_123_system_0_system_dc_pv_power"
725+
energy_entity_id = "sensor.victron_venus_pv_energy"
726+
power_entity_id = "sensor.victron_venus_pv_power"
744727

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

0 commit comments

Comments
 (0)