From a0ed833349ecb6d674e4ed913be9c7c1bf195eb3 Mon Sep 17 00:00:00 2001 From: Necroneco Date: Wed, 15 May 2024 22:11:14 +0800 Subject: [PATCH] Fix `InvalidStateError: invalid state` when enable statistics sensors. --- custom_components/xiaomi_gateway3/core/gate/miot.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/custom_components/xiaomi_gateway3/core/gate/miot.py b/custom_components/xiaomi_gateway3/core/gate/miot.py index 6e367e8d..53222c61 100644 --- a/custom_components/xiaomi_gateway3/core/gate/miot.py +++ b/custom_components/xiaomi_gateway3/core/gate/miot.py @@ -90,7 +90,12 @@ async def mqtt_publish_multiple( self, device: XDevice, payload: dict, gw2, delay: float = 1.0 ): fut = asyncio.get_event_loop().create_future() - device.add_listener(fut.set_result) + + def try_set_result(r): + if not fut.done(): + fut.set_result(r) + + device.add_listener(try_set_result) await self.mqtt.publish("miio/command", payload) try: async with asyncio.timeout(delay): @@ -98,4 +103,4 @@ async def mqtt_publish_multiple( except TimeoutError: await gw2.mqtt.publish("miio/command", payload) finally: - device.remove_listener(fut.set_result) + device.remove_listener(try_set_result)