Allow update entities on add_entities callback (#4114)
* Allow udpate entities on add_entities callback * fix wrong position * update force_update to update_before_add * add unittest for update_befor_add * fix unittest * change mocking
This commit is contained in:
parent
5d43d3eb1c
commit
9c0455e3dc
5 changed files with 50 additions and 31 deletions
|
@ -155,14 +155,15 @@ class EntityComponent(object):
|
|||
self.logger.exception(
|
||||
'Error while setting up platform %s', platform_type)
|
||||
|
||||
def add_entity(self, entity, platform=None):
|
||||
def add_entity(self, entity, platform=None, update_before_add=False):
|
||||
"""Add entity to component."""
|
||||
return run_coroutine_threadsafe(
|
||||
self.async_add_entity(entity, platform), self.hass.loop
|
||||
self.async_add_entity(entity, platform, update_before_add),
|
||||
self.hass.loop
|
||||
).result()
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_add_entity(self, entity, platform=None):
|
||||
def async_add_entity(self, entity, platform=None, update_before_add=False):
|
||||
"""Add entity to component.
|
||||
|
||||
This method must be run in the event loop.
|
||||
|
@ -172,6 +173,13 @@ class EntityComponent(object):
|
|||
|
||||
entity.hass = self.hass
|
||||
|
||||
# update/init entity data
|
||||
if update_before_add:
|
||||
if hasattr(entity, 'async_update'):
|
||||
yield from entity.async_update()
|
||||
else:
|
||||
yield from self.hass.loop.run_in_executor(None, entity.update)
|
||||
|
||||
if getattr(entity, 'entity_id', None) is None:
|
||||
object_id = entity.name or DEVICE_DEFAULT_NAME
|
||||
|
||||
|
@ -274,19 +282,21 @@ class EntityPlatform(object):
|
|||
self.platform_entities = []
|
||||
self._async_unsub_polling = None
|
||||
|
||||
def add_entities(self, new_entities):
|
||||
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), self.component.hass.loop
|
||||
self.async_add_entities(new_entities, update_before_add),
|
||||
self.component.hass.loop
|
||||
).result()
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_add_entities(self, new_entities):
|
||||
def async_add_entities(self, new_entities, update_before_add=False):
|
||||
"""Add entities for a single platform async.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
tasks = [self._async_process_entity(entity) for entity in new_entities]
|
||||
tasks = [self._async_process_entity(entity, update_before_add)
|
||||
for entity in new_entities]
|
||||
|
||||
yield from asyncio.gather(*tasks, loop=self.component.hass.loop)
|
||||
yield from self.component.async_update_group()
|
||||
|
@ -301,9 +311,11 @@ class EntityPlatform(object):
|
|||
second=range(0, 60, self.scan_interval))
|
||||
|
||||
@asyncio.coroutine
|
||||
def _async_process_entity(self, new_entity):
|
||||
def _async_process_entity(self, new_entity, update_before_add):
|
||||
"""Add entities to StateMachine."""
|
||||
ret = yield from self.component.async_add_entity(new_entity, self)
|
||||
ret = yield from self.component.async_add_entity(
|
||||
new_entity, self, update_before_add=update_before_add
|
||||
)
|
||||
if ret:
|
||||
self.platform_entities.append(new_entity)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue