Sequential updates for non-async entities

pull/4271/head
Paulus Schoutsen 2016-11-06 23:12:20 -08:00
parent 6d5f00098a
commit faceb4c1dc
1 changed files with 29 additions and 7 deletions

View File

@ -279,6 +279,7 @@ class EntityPlatform(object):
self.entity_namespace = entity_namespace
self.platform_entities = []
self._async_unsub_polling = None
self._process_updates = False
def add_entities(self, new_entities, update_before_add=False):
"""Add entities for a single platform."""
@ -335,14 +336,35 @@ class EntityPlatform(object):
self._async_unsub_polling()
self._async_unsub_polling = None
@callback
@asyncio.coroutine
def _update_entity_states(self, now):
"""Update the states of all the polling entities.
To protect from flooding the executor, we will update async entities
in parallel and other entities sequential.
This method must be run in the event loop.
"""
for entity in self.platform_entities:
if entity.should_poll:
self.component.hass.loop.create_task(
entity.async_update_ha_state(True)
)
if self._process_updates:
return
self._process_updates = True
try:
tasks = []
to_update = []
for entity in self.platform_entities:
if not entity.should_poll:
continue
update_coro = entity.async_update_ha_state(True)
if hasattr(entity, 'async_update'):
tasks.append(update_coro)
else:
to_update.append(update_coro)
for update_coro in to_update:
yield from update_coro
if tasks:
yield from asyncio.wait(tasks, loop=self.component.hass.loop)
finally:
self._process_updates = False