Fix race condition in group.set (#18796)
This commit is contained in:
parent
4e272624eb
commit
38ecf71307
2 changed files with 29 additions and 1 deletions
|
@ -207,6 +207,13 @@ async def async_setup(hass, config):
|
||||||
DOMAIN, SERVICE_RELOAD, reload_service_handler,
|
DOMAIN, SERVICE_RELOAD, reload_service_handler,
|
||||||
schema=RELOAD_SERVICE_SCHEMA)
|
schema=RELOAD_SERVICE_SCHEMA)
|
||||||
|
|
||||||
|
service_lock = asyncio.Lock()
|
||||||
|
|
||||||
|
async def locked_service_handler(service):
|
||||||
|
"""Handle a service with an async lock."""
|
||||||
|
async with service_lock:
|
||||||
|
await groups_service_handler(service)
|
||||||
|
|
||||||
async def groups_service_handler(service):
|
async def groups_service_handler(service):
|
||||||
"""Handle dynamic group service functions."""
|
"""Handle dynamic group service functions."""
|
||||||
object_id = service.data[ATTR_OBJECT_ID]
|
object_id = service.data[ATTR_OBJECT_ID]
|
||||||
|
@ -284,7 +291,7 @@ async def async_setup(hass, config):
|
||||||
await component.async_remove_entity(entity_id)
|
await component.async_remove_entity(entity_id)
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN, SERVICE_SET, groups_service_handler,
|
DOMAIN, SERVICE_SET, locked_service_handler,
|
||||||
schema=SET_SERVICE_SCHEMA)
|
schema=SET_SERVICE_SCHEMA)
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
|
|
|
@ -431,3 +431,24 @@ async def test_update_entity(hass):
|
||||||
|
|
||||||
assert len(entity.async_update_ha_state.mock_calls) == 2
|
assert len(entity.async_update_ha_state.mock_calls) == 2
|
||||||
assert entity.async_update_ha_state.mock_calls[-1][1][0] is True
|
assert entity.async_update_ha_state.mock_calls[-1][1][0] is True
|
||||||
|
|
||||||
|
|
||||||
|
async def test_set_service_race(hass):
|
||||||
|
"""Test race condition on setting service."""
|
||||||
|
exception = False
|
||||||
|
|
||||||
|
def async_loop_exception_handler(_, _2) -> None:
|
||||||
|
"""Handle all exception inside the core loop."""
|
||||||
|
nonlocal exception
|
||||||
|
exception = True
|
||||||
|
|
||||||
|
hass.loop.set_exception_handler(async_loop_exception_handler)
|
||||||
|
|
||||||
|
await async_setup_component(hass, 'group', {})
|
||||||
|
component = EntityComponent(_LOGGER, DOMAIN, hass, group_name='yo')
|
||||||
|
|
||||||
|
for i in range(2):
|
||||||
|
hass.async_create_task(component.async_add_entities([MockEntity()]))
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert not exception
|
||||||
|
|
Loading…
Add table
Reference in a new issue