-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
Entity registry #11979
Entity registry #11979
Conversation
@@ -381,13 +380,9 @@ def _async_process_config(hass, config, component): | |||
|
|||
# Don't create tasks and await them all. The order is important as | |||
# groups get a number based on creation order. | |||
group = yield from Group.async_create_group( | |||
yield from Group.async_create_group( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async_create_group already adds the group. Old code was adding each group twice.
Looks good so far 👍. I'm wondering about the relative imports. Is that now preferred, or do we not care about mixing absolute and relative imports? |
I... don't know about relative imports |
@@ -19,11 +19,13 @@ | |||
from homeassistant.util.async import ( | |||
run_callback_threadsafe, run_coroutine_threadsafe) | |||
import homeassistant.util.dt as dt_util | |||
from .entity_registry import EntityRegistry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a relative import.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I meant I don't know if I like them more
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, then I'll employ a lax attitude until someone says otherwise, if it shows up in another PR. 🍹
Why did you removed unique_id implementations in some cases and left it in others? |
@callback | ||
def async_get_or_create(self, domain, platform, unique_id, *, | ||
suggested_object_id=None): | ||
"""Get entity. Creat if it doesn't exist.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creat -> Create
@@ -14,15 +14,15 @@ | |||
from homeassistant.helpers.entity import Entity, generate_entity_id | |||
from homeassistant.helpers.entity_component import ( | |||
EntityComponent, DEFAULT_SCAN_INTERVAL, SLOW_SETUP_WARNING) | |||
from homeassistant.helpers import entity_component | |||
from homeassistant.helpers import entity_component, entity_registry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'homeassistant.helpers.entity_registry' imported but unused
@andrey-git I have removed several implementations of unique ID that were not unique. For example, they would depend on the name of a device. Unique ID has to be unique no matter the configuration. Serial numbers, device IDs, Mac addresses and BT addresses are great unique IDs. IP address is not perfect and I was in doubt if they should stay as part of unique id. |
I've done one more review and restored some unique ID implementations that I removed earlier. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a couple of comments, but I have a couple of questions:
- IP addresses should not be used as unique IDs in general, considering how common address renewals can be?
- Does or should this play somehow together with discovered devices? Most of the time you either get a mac address (
id
ormac
on my devices) over mdns, or an uuid (udn is part of the upnp standard). Should autodetected components be adapted accordingly to use this information?
@@ -180,7 +180,7 @@ def set(self, settings): | |||
@property | |||
def unique_id(self): | |||
"""Return the ID of this AC.""" | |||
return "{}.{}".format(self.__class__, self._api.ip_address) | |||
return self._api.ip_address |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be something else than the IP address?
@@ -170,7 +170,7 @@ def available(self) -> bool: | |||
@property | |||
def unique_id(self): | |||
"""Return the ID of this light.""" | |||
return '{}.{}'.format(self.__class__, self._ipaddr) | |||
return self._ipaddr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IP address also here.
@@ -70,7 +70,7 @@ def __init__(self, device): | |||
@property | |||
def unique_id(self): | |||
"""Return the ID of this light.""" | |||
return "{}.{}".format(self.__class__, self._address) | |||
return self._address |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably also should be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bluetooth address.
@@ -178,7 +178,7 @@ def effect_list(self): | |||
@property | |||
def unique_id(self) -> str: | |||
"""Return the ID of this light.""" | |||
return "{}.{}".format(self.__class__, self._ipaddr) | |||
return self._ipaddr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IP address. These devices would have two ways to obtain a unique identifier (either by using miio's proprietary api, a bit no-go as the current component uses a different one) or via mdns (mac
field).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I agree, IP address is not a good thing to see as uniqueness. Will remove them.
PATH_REGISTRY = 'entity_registry.yaml' | ||
SAVE_DELAY = 10 | ||
Entry = namedtuple('EntityRegistryEntry', | ||
'entity_id,unique_id,platform,domain') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth using attrs instead of namedtuples in general, see http://www.attrs.org/en/stable/why.html#namedtuples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to add another dependency. If anything, we would use a backport of the data classes that are being added to Python 3.7.
For the current use case namedtuples will be enough.
@rytilahti discovered entities should use the same source for unique ID as configured entities. I have another proposal to better deal with discovered entities: #11947 |
ccba3de
to
49799c6
Compare
49799c6
to
1369ea4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it.
For homematic, we have a unique ID, but they is unique for the device a not for the entity. I.e. a switch with two channel create 2 switch entity but they have the same device ID. what is the idea of that to solve it? Actual we generate unique entity_id but only copy this is not the nice way. We discovery also all this device.
If a device has a unique ID and 2 channels, you can make a unique id by combining the unique ID and the channel id. |
Would it be possible to add functionality to query the registry to do a lookup of entity_id or unique_id? |
There is the get_or_create function and there is an is_registered function. |
So I see a lot of reviews and "I like it" statements. Is it ok to merge? |
I think it is important that eventually IDs can be changed without restarting. |
That will be possible with the current design. It's something I want to be allow people to do via the UI. Rough Implementation:
|
I will implement unique_id as entity_id on homematic |
You don't have to set entity_id. When registering an entity id, the registry will use the available device name or a combination of platform and unique id. |
Yes, I don't set it, I set only the name :) |
Description:
Initial implementation of an entity registry:
To do:
Breaking change:
It's no longer supported to add the same entity twice to Home Assistant. In the past Home Assistant would filter it out based on unique id, now it's up to the platform to make sure this doesn't happen.
Related issue (if applicable): fixes #11533
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>
Example entry for
configuration.yaml
(if applicable):Checklist:
If the code does not interact with devices:
tox
run successfully. Your PR cannot be merged unless tests pass