38
38
# How long to hold the connection
39
39
# to wait for additional commands for
40
40
# disconnecting the device.
41
- DISCONNECT_DELAY = 59
41
+ DISCONNECT_DELAY = 49
42
42
43
43
44
44
def _sb_uuid (comms_type : str = "service" ) -> UUID | str :
@@ -81,6 +81,7 @@ def __init__(
81
81
self ._read_char : BleakGATTCharacteristic | None = None
82
82
self ._write_char : BleakGATTCharacteristic | None = None
83
83
self ._disconnect_timer : asyncio .TimerHandle | None = None
84
+ self ._expected_disconnect = False
84
85
self .loop = asyncio .get_event_loop ()
85
86
86
87
def _commandkey (self , key : str ) -> str :
@@ -97,8 +98,9 @@ async def _sendcommand(self, key: str, retry: int) -> bytes:
97
98
_LOGGER .debug ("%s: Sending command %s" , self .name , command )
98
99
if self ._operation_lock .locked ():
99
100
_LOGGER .debug (
100
- "%s: Operation already in progress, waiting for it to complete. " ,
101
+ "%s: Operation already in progress, waiting for it to complete; RSSI: %s " ,
101
102
self .name ,
103
+ self .rssi ,
102
104
)
103
105
104
106
max_attempts = retry + 1
@@ -116,8 +118,9 @@ async def _sendcommand(self, key: str, retry: int) -> bytes:
116
118
except BLEAK_EXCEPTIONS :
117
119
if attempt == retry :
118
120
_LOGGER .error (
119
- "%s: communication failed; Stopping trying" ,
121
+ "%s: communication failed; Stopping trying; RSSI: %s " ,
120
122
self .name ,
123
+ self .rssi ,
121
124
exc_info = True ,
122
125
)
123
126
return b"\x00 "
@@ -133,12 +136,18 @@ def name(self) -> str:
133
136
"""Return device name."""
134
137
return f"{ self ._device .name } ({ self ._device .address } )"
135
138
139
+ @property
140
+ def rssi (self ) -> int :
141
+ """Return RSSI of device."""
142
+ return self ._get_adv_value ("rssi" )
143
+
136
144
async def _ensure_connected (self ):
137
145
"""Ensure connection to device is established."""
138
146
if self ._connect_lock .locked ():
139
147
_LOGGER .debug (
140
- "%s: Connection already in progress, waiting for it to complete. " ,
148
+ "%s: Connection already in progress, waiting for it to complete; RSSI: %s " ,
141
149
self .name ,
150
+ self .rssi ,
142
151
)
143
152
if self ._client and self ._client .is_connected :
144
153
self ._reset_disconnect_timer ()
@@ -148,14 +157,16 @@ async def _ensure_connected(self):
148
157
if self ._client and self ._client .is_connected :
149
158
self ._reset_disconnect_timer ()
150
159
return
160
+ _LOGGER .debug ("%s: Connecting; RSSI: %s" , self .name , self .rssi )
151
161
client = await establish_connection (
152
162
BleakClientWithServiceCache ,
153
163
self ._device ,
154
164
self .name ,
165
+ self ._disconnected ,
155
166
cached_services = self ._cached_services ,
156
167
)
157
168
self ._cached_services = client .services
158
- _LOGGER .debug ("%s: Connected" , self .name )
169
+ _LOGGER .debug ("%s: Connected; RSSI: %s " , self .name , self . rssi )
159
170
services = client .services
160
171
self ._read_char = services .get_characteristic (_sb_uuid (comms_type = "rx" ))
161
172
self ._write_char = services .get_characteristic (_sb_uuid (comms_type = "tx" ))
@@ -166,10 +177,24 @@ def _reset_disconnect_timer(self):
166
177
"""Reset disconnect timer."""
167
178
if self ._disconnect_timer :
168
179
self ._disconnect_timer .cancel ()
180
+ self ._expected_disconnect = False
169
181
self ._disconnect_timer = self .loop .call_later (
170
182
DISCONNECT_DELAY , self ._disconnect
171
183
)
172
184
185
+ def _disconnected (self , client : BleakClientWithServiceCache ) -> None :
186
+ """Disconnected callback."""
187
+ if self ._expected_disconnect :
188
+ _LOGGER .debug (
189
+ "%s: Disconnected from device; RSSI: %s" , self .name , self .rssi
190
+ )
191
+ return
192
+ _LOGGER .warning (
193
+ "%s: Device unexpectedly disconnected; RSSI: %s" ,
194
+ self .name ,
195
+ self .rssi ,
196
+ )
197
+
173
198
def _disconnect (self ):
174
199
"""Disconnect from device."""
175
200
self ._disconnect_timer = None
@@ -185,6 +210,7 @@ async def _execute_disconnect(self):
185
210
async with self ._connect_lock :
186
211
if not self ._client or not self ._client .is_connected :
187
212
return
213
+ self ._expected_disconnect = True
188
214
await self ._client .disconnect ()
189
215
self ._client = None
190
216
self ._read_char = None
@@ -206,7 +232,7 @@ def _notification_handler(_sender: int, data: bytearray) -> None:
206
232
return
207
233
future .set_result (data )
208
234
209
- _LOGGER .debug ("%s: Subscribe to notifications" , self .name )
235
+ _LOGGER .debug ("%s: Subscribe to notifications; RSSI: %s " , self .name , self . rssi )
210
236
await client .start_notify (self ._read_char , _notification_handler )
211
237
212
238
_LOGGER .debug ("%s: Sending command: %s" , self .name , key )
0 commit comments