diff --git a/custom_components/matter_experimental/adapter.py b/custom_components/matter_experimental/adapter.py index daa56eaf..96eb9e7c 100644 --- a/custom_components/matter_experimental/adapter.py +++ b/custom_components/matter_experimental/adapter.py @@ -101,6 +101,7 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: self._store = get_matter_store(hass, config_entry) self.platform_handlers: dict[Platform, AddEntitiesCallback] = {} self._platforms_set_up = asyncio.Event() + self._node_lock: dict[int, asyncio.Lock] = {} def register_platform_handler( self, platform: Platform, add_entities: AddEntitiesCallback @@ -130,6 +131,11 @@ def get_server_url(self) -> str: def get_client_session(self) -> aiohttp.ClientSession: return async_get_clientsession(self.hass) + def get_node_lock(self, nodeid) -> asyncio.Lock: + if nodeid not in self._node_lock: + self._node_lock[nodeid] = asyncio.Lock() + return self._node_lock[nodeid] + async def setup_node(self, node: MatterNode) -> None: """Set up an node.""" await self._platforms_set_up.wait() diff --git a/custom_components/matter_experimental/entity.py b/custom_components/matter_experimental/entity.py index d46539a8..545cb433 100644 --- a/custom_components/matter_experimental/entity.py +++ b/custom_components/matter_experimental/entity.py @@ -30,10 +30,8 @@ def device_info(self) -> entity.DeviceInfo | None: """Return device info for device registry.""" return {"identifiers": {(DOMAIN, self._device.node.unique_id)}} - async def async_added_to_hass(self) -> None: - """Handle being added to Home Assistant.""" - await super().async_added_to_hass() - + async def init_matter_device(self) -> None: + """Initialize and subscribe device attributes.""" device_name = ( device_registry.async_get(self.hass) .async_get(self.registry_entry.device_id) @@ -80,6 +78,15 @@ async def async_added_to_hass(self) -> None: self._update_from_device() + async def async_added_to_hass(self) -> None: + """Handle being added to Home Assistant.""" + await super().async_added_to_hass() + + async with self._device.node.matter.adapter.get_node_lock( + self._device.node.node_id + ): + await self.init_matter_device() + async def async_will_remove_from_hass(self) -> None: """Run when entity will be removed from hass.""" if self._unsubscribe is not None: