home-assistant / architecture

Repo to discuss Home Assistant architecture
310 stars 101 forks source link

Officially allow enities to set their entity ID not based on their names #736

Closed ekutner closed 2 years ago

ekutner commented 2 years ago

Context

Deriving entity IDs from the entity name, which is also used as the display name of the entity, may be a reasonable work around when the entity is not offering its own ID but it is very limiting and doesn't always make sense. The main limitations in that approach are:

In reality I didn't find anything that was forcing this connection between ID and display name. In the integration I was working on I found a workaround for explicitly setting the entity ID by setting it like this on my entities: self.entity_id = f'dummy.{self.unique_id}' This "trick" is based on the way HA core currently generates the entity_id but this it is based on reverse engineering the current code it may break at any time. That said, using this trick, everything was working fine with me entities and it allowed me to change the names without affecting anything else in HA.

Proposal

Separate the display name of an entity from its entity ID. The display name can be used as a fallback but if an explicit ID is provided then it should be used.

Consequences

The above limitations will be lifted. Especially the translation of entity names is critical in my eyes and must be supported. To be honest, I don't really see why it can't be supported in the frontend even without the proposed changed but I may be missing something here, so this change should definitely allow for it. As long as names are still used as a fallback for existing integrations that don't provide an explicit entity_id there should be no negative effect on existing integrations.

balloob commented 2 years ago

Entities with a unique ID will register their entity ID and will be assigned the same one even if their name changes.

Entities can already suggest an entity ID by setting the entity_id instance variable before being passed to async_add_entities.

ekutner commented 2 years ago

@balloob As I pointed out, this is doable, and I was doing it in my entity. I'm happy to hear that it's supposed to work like that but it's not documented anywhere that I could find and the entity_id has to be constructed in a specific way for it to work: ".". It would be a very good idea to document it and maybe allow it to be defined in a more natural way. I will open a separate issue about translation of entity names.