diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index 44d0b8891d5..30d62608f9b 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -283,7 +283,7 @@ class EntityPlatform(object): def add_entities(self, new_entities, update_before_add=False): """Add entities for a single platform.""" run_coroutine_threadsafe( - self.async_add_entities(new_entities, update_before_add), + self.async_add_entities(list(new_entities), update_before_add), self.component.hass.loop ).result() diff --git a/tests/helpers/test_entity_component.py b/tests/helpers/test_entity_component.py index 7c6b94963cf..02d8d36dafa 100644 --- a/tests/helpers/test_entity_component.py +++ b/tests/helpers/test_entity_component.py @@ -8,7 +8,7 @@ from unittest.mock import patch, Mock import homeassistant.core as ha import homeassistant.loader as loader from homeassistant.components import group -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.entity import Entity, generate_entity_id from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers import discovery import homeassistant.util.dt as dt_util @@ -356,3 +356,20 @@ class TestHelpersEntityComponent(unittest.TestCase): assert sorted(self.hass.states.entity_ids()) == \ ['test_domain.yummy_beer', 'test_domain.yummy_unnamed_device'] + + def test_adding_entities_with_generator_and_thread_callback(self): + """Test generator in add_entities that calls thread method. + + We should make sure we resolve the generator to a list before passing + it into an async context. + """ + component = EntityComponent(_LOGGER, DOMAIN, self.hass) + + def create_entity(number): + """Create entity helper.""" + entity = EntityTest() + entity.entity_id = generate_entity_id(component.entity_id_format, + 'Number', hass=self.hass) + return entity + + component.add_entities(create_entity(i) for i in range(2))