Skip to content

refactor(entity): drop explicit entity_id override in complex naming - #448

Merged
tomer-w merged 1 commit into
tomer-w:mainfrom
pos-ei-don:entity-id-from-display-id
Jul 7, 2026
Merged

refactor(entity): drop explicit entity_id override in complex naming#448
tomer-w merged 1 commit into
tomer-w:mainfrom
pos-ei-don:entity-id-from-display-id

Conversation

@pos-ei-don

@pos-ei-don pos-ei-don commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

You noted in the review that setting self.entity_id in complex naming was probably a mistake — the pattern from before proper HA entity naming was established. This PR just removes it.

What changed:

  • Drop the two lines that explicitly set self.entity_id in the complex-naming branch.
  • HA now derives the entity_id from the entity name, the same way simple naming works.

Why this is safe:

  • unique_id is unchanged, so existing entities keep their already-registered entity_id (HA stores it in the entity registry; the unique_id is just the lookup key).
  • DEFAULT_SIMPLE_NAMING = True, so new installations already use simple naming and never hit this path.
  • Complex naming users with new entities (new device added to an existing install) get HA-derived entity_ids going forward — cleaner and consistent with simple naming.

No library changes neededdisplay_id / victron_mqtt library not touched.

# entity_id only when first creating an entity, so this affects NEW
# entities only; existing installs are unchanged. getattr keeps it
# safe if the bundled library predates display_id.
display_id = getattr(metric, "display_id", metric.unique_id)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need the dynamic getattr? I assume that with the new library you will always have it available.

# safe if the bundled library predates display_id.
display_id = getattr(metric, "display_id", metric.unique_id)
self._attr_unique_id = f"{entity_platform}.{ENTITY_PREFIX}_{installation_id}_{metric.unique_id}"
self.entity_id = f"{entity_platform}.{ENTITY_PREFIX}_{installation_id}_{display_id}"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need this line? I suspect it is old leftover. We dont do something simialr in the simple naming case so why we need it here? If i'm right than maybe the real fix is just to remove this line and than we dont need the change we made in the library.

@pos-ei-don

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

getattr (L80): Agreed — now that display_id ships in the released library, the fallback is dead weight. Dropped it; it reads metric.display_id directly now.

self.entity_id = ... (L82): I don't think this one is a leftover — it's what gives the complex (non-simple) naming its deterministic {platform}.victron_mqtt_<installation_id>_<topic> entity_id scheme. Without it, HA falls back to the name-based default (has_entity_name → device name + entity name), which is exactly what the simple-naming path already does. That's why simple naming doesn't set it and complex naming does — they're two deliberately different schemes:

  • simple: human/name-based entity_ids
  • complex: stable, installation-namespaced, topic-based entity_ids (predictable, and they don't collide across multiple Victron installations)

This PR only changes the suggested id within the complex scheme: from the doubled-prefix unique_id (…_solarcharger_3_solarcharger_total_pv_yield) to the de-duplicated display_id (…_solarcharger_3_total_pv_yield). unique_id is intentionally unchanged, so existing entities keep their ids and only newly created ones get the cleaner suggestion.

If you'd rather drop the deterministic complex scheme altogether and let HA derive entity_ids from names in both modes, then you're right — this line (and display_id) would no longer be needed. But that's a broader behaviour change (complex naming loses the installation_id namespacing), so I kept it scoped to just the de-duplication. Happy to go either way — your call on whether the complex scheme should stay.

@tomer-w

tomer-w commented Jul 1, 2026

Copy link
Copy Markdown
Owner

The history was that I started with a single naming which is aligned with what today called complex naming. It is also important to note I was quite a newbie in the HA integration world, so I probably made every mistake possible. Like even setting those entity ids to begin with. I got feedback from users that having the installation id in the entity id is cumbersome for automations / scripting / dashboards / etc. so I wanted to remove it but didnt know how to do it without breaking current users and without breaking users who do have multiple GX devices. In hindsight I was supposed to just set the unique id, let HA handle the entity id and everything would just work. I was just too uneducated to understand that.
Anyhow, I decided to add the simple naming for new users to work the right way and got stuck with the complex naming for backward compatibility. I dont see any reason for new users to use the 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.
@pos-ei-don
pos-ei-don force-pushed the entity-id-from-display-id branch from 6d2eb06 to c6a8b47 Compare July 1, 2026 17:21
@pos-ei-don pos-ei-don changed the title Use metric.display_id for the default entity_id refactor(entity): drop explicit entity_id override in complex naming Jul 1, 2026
@pos-ei-don

Copy link
Copy Markdown
Contributor Author

You were right — the self.entity_id line was a historical leftover from before proper HA naming patterns were established. The display_id approach was fixing the wrong thing.

Simplified this PR to just remove that line: HA now derives the entity_id from the entity name, same as simple naming. No library changes, no new API needed.

  • unique_id is unchanged → existing entities keep their registered entity_id
  • DEFAULT_SIMPLE_NAMING = True → new installs already use simple naming and never touch this path
  • Complex naming users who get new entities in the future get HA-derived entity_ids going forward

The diff is now 5 lines of deletion only.

@tomer-w
tomer-w merged commit 16957b7 into tomer-w:main Jul 7, 2026
5 checks passed
@tomer-w

tomer-w commented Jul 7, 2026

Copy link
Copy Markdown
Owner

@pos-ei-don , happy we got this resolved. Can you revert back the changes you made in the library if we dont need them? I dont want to have unused leftovers.

@pos-ei-don

Copy link
Copy Markdown
Contributor Author

Done — opened tomer-w/victron_mqtt#110 to revert the display_id addition from #106.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants