How to set up specific entity_id when developing custom components?

    @property
    def unique_id(self):
        """Return unique ID."""
        return self.uid

I thought this is the function to set up entity_id, but after restarting HA, the entity_id of entities created by my custom components is different from the ones return by this function. why this happens? Thanks.

1 Like

Visible entity ids are created by names and users are allowed to modify it.

What you are referring is internal id of the entities and they are not modifiable. Unique ids are stored in internal files of home assistant (core.entity_registry)

1 Like

thanks a lot!

1 Like

If you set a name property then that will get used as entity_id, or else an entity_id property (which you will want to have passed through generate_entity_id from homeassistant.helpers.entity to make sure it s unique).

2 Likes

Specifying a specific object ID can be quite simple

from homeassistant.helpers.entity import generate_entity_id
...
class MyAwesomeSensor(SensorEntity):

    def __init__(self) -> None:
        """Initialize."""
        self.entity_id = generate_entity_id("sensor.{}", "my_awesome_sensor_id")
1 Like