diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index 5330eafed19..9e059528619 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -95,7 +95,7 @@ class EntityComponent(object): ).result() def async_extract_from_service(self, service, expand_group=True): - """Extract all known entities from a service call. + """Extract all known and available entities from a service call. Will return all entities if no entities specified in call. Will return an empty list if entities specified but unknown. @@ -103,11 +103,13 @@ class EntityComponent(object): This method must be run in the event loop. """ if ATTR_ENTITY_ID not in service.data: - return list(self.entities.values()) + return [entity for entity in self.entities.values() + if entity.available] return [self.entities[entity_id] for entity_id in extract_entity_ids(self.hass, service, expand_group) - if entity_id in self.entities] + if entity_id in self.entities and + self.entities[entity_id].available] @asyncio.coroutine def _async_setup_platform(self, platform_type, platform_config, diff --git a/tests/helpers/test_entity_component.py b/tests/helpers/test_entity_component.py index 38dc01b4387..a76b3a15068 100644 --- a/tests/helpers/test_entity_component.py +++ b/tests/helpers/test_entity_component.py @@ -50,6 +50,11 @@ class EntityTest(Entity): """Return the unique ID of the entity.""" return self._handle('unique_id') + @property + def available(self): + """Return True if entity is available.""" + return self._handle('available') + def _handle(self, attr): """Helper for the attributes.""" if attr in self._values: @@ -474,3 +479,29 @@ def test_platform_warn_slow_setup(hass): assert logger_method == _LOGGER.warning assert mock_call().cancel.called + + +@asyncio.coroutine +def test_extract_from_service_available_device(hass): + """Test the extraction of entity from service and device is available.""" + component = EntityComponent(_LOGGER, DOMAIN, hass) + yield from component.async_add_entities([ + EntityTest(name='test_1'), + EntityTest(name='test_2', available=False), + EntityTest(name='test_3'), + EntityTest(name='test_4', available=False), + ]) + + call_1 = ha.ServiceCall('test', 'service') + + assert ['test_domain.test_1', 'test_domain.test_3'] == \ + sorted(ent.entity_id for ent in + component.async_extract_from_service(call_1)) + + call_2 = ha.ServiceCall('test', 'service', data={ + 'entity_id': ['test_domain.test_3', 'test_domain.test_4'], + }) + + assert ['test_domain.test_3'] == \ + sorted(ent.entity_id for ent in + component.async_extract_from_service(call_2))