Make core to look avilable state of device on servicecall (#7045)

This commit is contained in:
Pascal Vizeli 2017-04-11 17:59:46 +02:00 committed by Paulus Schoutsen
parent f099aee69a
commit d7ca9e7a66
2 changed files with 36 additions and 3 deletions

View File

@ -95,7 +95,7 @@ class EntityComponent(object):
).result() ).result()
def async_extract_from_service(self, service, expand_group=True): 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 all entities if no entities specified in call.
Will return an empty list if entities specified but unknown. 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. This method must be run in the event loop.
""" """
if ATTR_ENTITY_ID not in service.data: 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 return [self.entities[entity_id] for entity_id
in extract_entity_ids(self.hass, service, expand_group) 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 @asyncio.coroutine
def _async_setup_platform(self, platform_type, platform_config, def _async_setup_platform(self, platform_type, platform_config,

View File

@ -50,6 +50,11 @@ class EntityTest(Entity):
"""Return the unique ID of the entity.""" """Return the unique ID of the entity."""
return self._handle('unique_id') return self._handle('unique_id')
@property
def available(self):
"""Return True if entity is available."""
return self._handle('available')
def _handle(self, attr): def _handle(self, attr):
"""Helper for the attributes.""" """Helper for the attributes."""
if attr in self._values: if attr in self._values:
@ -474,3 +479,29 @@ def test_platform_warn_slow_setup(hass):
assert logger_method == _LOGGER.warning assert logger_method == _LOGGER.warning
assert mock_call().cancel.called 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))