Update image_processing async (#15082)

* scan() -> async_job

* added async_scan
This commit is contained in:
Diogo Gomes 2018-07-02 15:57:52 +01:00 committed by Pascal Vizeli
parent 2145ac5e46
commit f3588a8782

View File

@ -69,27 +69,32 @@ SERVICE_SCAN_SCHEMA = vol.Schema({
@bind_hass
def scan(hass, entity_id=None):
"""Force process an image."""
"""Force process of all cameras or given entity."""
hass.add_job(async_scan, hass, entity_id)
@callback
@bind_hass
def async_scan(hass, entity_id=None):
"""Force process of all cameras or given entity."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.services.call(DOMAIN, SERVICE_SCAN, data)
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_SCAN, data))
@asyncio.coroutine
def async_setup(hass, config):
async def async_setup(hass, config):
"""Set up the image processing."""
component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)
yield from component.async_setup(config)
await component.async_setup(config)
@asyncio.coroutine
def async_scan_service(service):
async def async_scan_service(service):
"""Service handler for scan."""
image_entities = component.async_extract_from_service(service)
update_task = [entity.async_update_ha_state(True) for
entity in image_entities]
if update_task:
yield from asyncio.wait(update_task, loop=hass.loop)
await asyncio.wait(update_task, loop=hass.loop)
hass.services.async_register(
DOMAIN, SERVICE_SCAN, async_scan_service,
@ -124,8 +129,7 @@ class ImageProcessingEntity(Entity):
"""
return self.hass.async_add_job(self.process_image, image)
@asyncio.coroutine
def async_update(self):
async def async_update(self):
"""Update image and process it.
This method is a coroutine.
@ -134,7 +138,7 @@ class ImageProcessingEntity(Entity):
image = None
try:
image = yield from camera.async_get_image(
image = await camera.async_get_image(
self.camera_entity, timeout=self.timeout)
except HomeAssistantError as err:
@ -142,7 +146,7 @@ class ImageProcessingEntity(Entity):
return
# process image data
yield from self.async_process_image(image.content)
await self.async_process_image(image.content)
class ImageProcessingFaceEntity(ImageProcessingEntity):