diff --git a/homeassistant/components/config/__init__.py b/homeassistant/components/config/__init__.py index ad7ae14ecb7..682e23dd14c 100644 --- a/homeassistant/components/config/__init__.py +++ b/homeassistant/components/config/__init__.py @@ -94,6 +94,7 @@ class BaseEditConfigView(HomeAssistantView): self.data_schema = data_schema self.post_write_hook = post_write_hook self.data_validator = data_validator + self.mutation_lock = asyncio.Lock() def _empty_config(self): """Empty config if file not found.""" @@ -114,8 +115,9 @@ class BaseEditConfigView(HomeAssistantView): async def get(self, request, config_key): """Fetch device specific config.""" hass = request.app["hass"] - current = await self.read_config(hass) - value = self._get_value(hass, current, config_key) + async with self.mutation_lock: + current = await self.read_config(hass) + value = self._get_value(hass, current, config_key) if value is None: return self.json_message("Resource not found", 404) @@ -148,10 +150,11 @@ class BaseEditConfigView(HomeAssistantView): path = hass.config.path(self.path) - current = await self.read_config(hass) - self._write_value(hass, current, config_key, data) + async with self.mutation_lock: + current = await self.read_config(hass) + self._write_value(hass, current, config_key, data) - await hass.async_add_executor_job(_write, path, current) + await hass.async_add_executor_job(_write, path, current) if self.post_write_hook is not None: hass.async_create_task( @@ -163,15 +166,16 @@ class BaseEditConfigView(HomeAssistantView): async def delete(self, request, config_key): """Remove an entry.""" hass = request.app["hass"] - current = await self.read_config(hass) - value = self._get_value(hass, current, config_key) - path = hass.config.path(self.path) + async with self.mutation_lock: + current = await self.read_config(hass) + value = self._get_value(hass, current, config_key) + path = hass.config.path(self.path) - if value is None: - return self.json_message("Resource not found", 404) + if value is None: + return self.json_message("Resource not found", 404) - self._delete_value(hass, current, config_key) - await hass.async_add_executor_job(_write, path, current) + self._delete_value(hass, current, config_key) + await hass.async_add_executor_job(_write, path, current) if self.post_write_hook is not None: hass.async_create_task(self.post_write_hook(ACTION_DELETE, config_key))