11import abc
22import logging
33import time
4+ import bleak
45import asyncio
56from typing import Callable
67from bleak import BleakClient
@@ -47,7 +48,7 @@ def __init__(self, mac: str):
4748 self ._fan_speed = 0
4849
4950 @abc .abstractmethod
50- def _send_request (self , request : bytearray ) -> bytearray :
51+ async def _send_request (self , request : bytearray ) -> bytearray :
5152 """ Send request to device
5253
5354 Args:
@@ -69,7 +70,7 @@ def _decode_response(self, response: bytearray) -> dict:
6970 pass
7071
7172 @abc .abstractmethod
72- def _encode_request (self , request : dict ) -> bytearray :
73+ async def _encode_request (self , request : dict ) -> bytearray :
7374 """ Encode dictionary of request to byte array
7475
7576 Args:
@@ -80,7 +81,7 @@ def _encode_request(self, request: dict) -> bytearray:
8081 pass
8182
8283 @abc .abstractmethod
83- def get (self , keep_connection : bool = False ) -> dict :
84+ async def get (self , keep_connection : bool = False ) -> dict :
8485 """ Get device information
8586 Returns:
8687 dictionay with device paramters
@@ -115,7 +116,9 @@ async def _process_status(self, code: int) -> str:
115116
116117 @property
117118 async def connection_status (self ):
118- return "connected" if await self ._btle .is_connected () else "disc"
119+ status = "connected" if await self ._btle .is_connected () else "disc"
120+ _LOGGER .debug ("connection_status is %s" % status )
121+ return status
119122
120123 async def _connect (self ):
121124 if self .mac == "dummy" :
@@ -125,15 +128,22 @@ async def _connect(self):
125128 if await self .connection_status == "disc" :
126129 try :
127130 await self ._btle .connect ()
131+ await self ._btle .get_services ()
128132 except exc .BleakError as e :
129133 _LOGGER .warning ("Got %s exception" , str (e ))
130134 time .sleep (2 )
131135 raise e
136+ else :
137+ _LOGGER .debug ("Already connected!" )
132138
133139 async def _disconnect (self ):
134- if await self .connection_status != "disc" :
135- if self .mac != "dummy" :
136- await self ._btle .disconnect ()
140+ try :
141+ if await self .connection_status != "disc" :
142+ if self .mac != "dummy" :
143+ await self ._btle .disconnect ()
144+ finally :
145+ # re-init client
146+ self ._btle = BleakClient (self .mac )
137147
138148 async def _try_write (self , request : bytearray ):
139149 if self .mac != "dummy" :
@@ -177,7 +187,12 @@ async def _do_action(self, action: Callable, max_tries: int = 3, *args, **kwargs
177187
178188 async def _enable_notifications (self ):
179189 _LOGGER .debug ("Enabling notification" )
180- return await self ._btle .start_notify (self .uuid_notify , self ._delegation .handleNotification )
190+ try :
191+ result = await self ._btle .start_notify (self .uuid_notify , self ._delegation .handleNotification )
192+ except bleak .exc .BleakError as e :
193+ _LOGGER .critical ("Got exception %s while enabling notifications!" % str (e ))
194+ raise e
195+ return result
181196
182197 @property
183198 def fan_speed (self ):
0 commit comments