Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions homeassistant/helpers/entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,14 @@ async def async_add_entities(self, new_entities, update_before_add=False):
return

hass = self.hass
component_entities = set(hass.states.async_entity_ids(self.domain))

device_registry = await \
hass.helpers.device_registry.async_get_registry()
entity_registry = await \
hass.helpers.entity_registry.async_get_registry()
tasks = [
self._async_add_entity(entity, update_before_add,
component_entities, entity_registry,
device_registry)
entity_registry, device_registry)
for entity in new_entities]

# No entities for processing
Expand All @@ -235,8 +233,7 @@ async def async_add_entities(self, new_entities, update_before_add=False):
)

async def _async_add_entity(self, entity, update_before_add,
component_entities, entity_registry,
device_registry):
entity_registry, device_registry):
"""Add an entity to the platform."""
if entity is None:
raise ValueError('Entity cannot be None')
Expand Down Expand Up @@ -329,25 +326,24 @@ async def _async_add_entity(self, entity, update_before_add,
if self.entity_namespace is not None:
suggested_object_id = '{} {}'.format(self.entity_namespace,
suggested_object_id)

entity.entity_id = entity_registry.async_generate_entity_id(
self.domain, suggested_object_id)
self.domain, suggested_object_id, self.entities.keys())

# Make sure it is valid in case an entity set the value themselves
if not valid_entity_id(entity.entity_id):
raise HomeAssistantError(
'Invalid entity id: {}'.format(entity.entity_id))
elif entity.entity_id in component_entities:
elif (entity.entity_id in self.entities or
entity.entity_id in self.hass.states.async_entity_ids(
self.domain)):
msg = 'Entity id already exists: {}'.format(entity.entity_id)
if entity.unique_id is not None:
msg += '. Platform {} does not generate unique IDs'.format(
self.platform_name)
raise HomeAssistantError(
msg)
raise HomeAssistantError(msg)

entity_id = entity.entity_id
self.entities[entity_id] = entity
component_entities.add(entity_id)
entity.async_on_remove(lambda: self.entities.pop(entity_id))

if hasattr(entity, 'async_added_to_hass'):
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/helpers/entity_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ def async_get_entity_id(self, domain: str, platform: str, unique_id: str):
return None

@callback
def async_generate_entity_id(self, domain, suggested_object_id):
def async_generate_entity_id(self, domain, suggested_object_id,
known_object_ids=None):
"""Generate an entity ID that does not conflict.

Conflicts checked against registered and currently existing entities.
"""
return ensure_unique_string(
'{}.{}'.format(domain, slugify(suggested_object_id)),
chain(self.entities.keys(),
self.hass.states.async_entity_ids(domain))
self.hass.states.async_entity_ids(domain),
known_object_ids if known_object_ids else [])
)

@callback
Expand Down