diff --git a/custom_components/govee_light_ble/api.py b/custom_components/govee_light_ble/api.py index c6cbd2e..136ca34 100644 --- a/custom_components/govee_light_ble/api.py +++ b/custom_components/govee_light_ble/api.py @@ -44,7 +44,7 @@ async def _ensureConnected(self): if self._client != None and self._client.is_connected: return None if self.receiving_in_progress: - self._stopReceiving() + await self._stopReceiving() self._client = await bleak_retry_connector.establish_connection(BleakClient, self._ble_device, self.address) async def _transmitPacket(self, packet: LedPacket): @@ -116,6 +116,7 @@ async def _startReceiving(self): async def _stopReceiving(self): """ stop receiving packets """ self.receiving_in_progress = False + self.stop_event.set() await self._client.stop_notify(READ_CHARACTERISTIC_UUID) async def sendPacketBuffer(self): @@ -126,15 +127,17 @@ async def sendPacketBuffer(self): await self._ensureConnected() if self._responseExpected: await self._startReceiving() - - for packet in self._packet_buffer: - await self._transmitPacket(packet) - await self._clearPacketBuffer() - - if self._responseExpected: - #wait to receive all exptected packets - await self.stop_event.wait() - await self._stopReceiving() + try: + for packet in self._packet_buffer: + await self._transmitPacket(packet) + await self._clearPacketBuffer() + if self._responseExpected: + #wait to receive all exptected packets + await self.stop_event.wait() + #ensure receiving ist stopped + finally: + if self._responseExpected: + await self._stopReceiving() #not disconnecting seems to improve connection speed async def requestStateBuffered(self):