Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unique IDs to entities #73

Merged
merged 11 commits into from
Jan 9, 2023
Merged

Add unique IDs to entities #73

merged 11 commits into from
Jan 9, 2023

Conversation

pnbruckner
Copy link
Owner

@pnbruckner pnbruckner commented Dec 21, 2022

This allows some entity attributes to be set via the UI's Entities page.

Closes #72

@pnbruckner
Copy link
Owner Author

The first commit has not been thoroughly tested. Also, this integration is due for some serious updating, so there should be much more to come. If anyone gives this a try, even with just these minimal changes, I'd appreciate any feedback, good or bad.

@RafaelSchridi
Copy link

Working great so far! (had to censor my location, its just a town name behind there)
image
image

if ns:
self._attr_unique_id = f"{ns} {name}"
else:
self._attr_unique_id = name

Choose a reason for hiding this comment

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

I'm not so sure this is a good idea, this would give sensors like "sensor.daylight" as a unique_id right?

Maybe force a prefix when not using a namespace? or no unique_id if you're not using a ns?
not sure what the right approach is.

Choose a reason for hiding this comment

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

Placed this at the wrong method, should have been a comment on the abstract method of sensor.py

Copy link
Owner Author

Choose a reason for hiding this comment

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

If namespace was not specified, then the unique ID (in your example) would be "Daylight". There really shouldn't be more than one daylight sensor configured with no namespace (or with a given namespace, for that matter), so that shouldn't be a problem.

The whole idea of using entity_namespace is to allow having more than one of a given type of sensor (usually with different location information), such that the resulting entity ID is predictable. (If namespace wasn't used, two daylight sensors, say with two different sets of location values, would have unpredictable entity IDs. E.g., one would be sensor.daylight and the other would be sensor.daylight_2, and which is which can't be predetermined. At least, that's what I would expect to happen. I haven't tried it recently.)

The actual value of the unique ID isn't important, just that it's unique for all sensors of the same domain (i.e., sensor or binary_sensor) and for the same platform (i.e., sun2.) The unique ID isn't even displayed to the user.

But what is really important is coming up with a method to determine a unique ID based on the config file, such that it doesn't change when HA restarts, and is very unlikely to change if the configuration is changed. This is important because it "maps" the config file entry to the entity registry entry. As long as the entity_namespace value isn't changed, the mapping will stay the same.

@pnbruckner pnbruckner force-pushed the unique-ids branch 6 times, most recently from d45a6a4 to a133c84 Compare January 3, 2023 20:13
@pnbruckner pnbruckner merged commit 179f202 into master Jan 9, 2023
@pnbruckner pnbruckner deleted the unique-ids branch January 9, 2023 16:15
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.

Add unique_id's to entities
2 participants