Skip to content

Commit b91b8f9

Browse files
committed
feat: common pair function
Do bluetooth pairing and device paring from module. Lite is not need special pairing. S3 is require sending pair command. connect/disconnect routine moved to main class. We have to use private methods _writeCmd and _waitResp, because 'Already Paired' is error from bluepy point of view and it drop connection in this case. So have to simulate _mgmtCmd to avoid disconnect if device already paired via bluetooth.
1 parent 047158d commit b91b8f9

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

tion_btle/lite.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,7 @@ def encode_state():
285285
[encode_state(), sb, tb, int(request["heater_temp"]), int(request["fan_speed"])] +
286286
self.__presets + lb + [0x00] + self.CRC
287287
)
288+
289+
def _pair(self):
290+
"""Lite is not require special pairing procedure"""
291+
return

tion_btle/s3.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,9 @@ def __try_get_state(self) -> bytearray:
5353
_LOGGER.debug("Response is %s", bytes(response).hex())
5454
return response
5555

56-
def pair(self):
57-
_LOGGER.setLevel("DEBUG")
58-
_LOGGER.debug("Pairing")
59-
_LOGGER.debug("Connecting")
60-
self._do_action(self._connect)
56+
def _pair(self):
6157
_LOGGER.debug("Sending pair command")
6258
self._send_request(self.pair_command)
63-
_LOGGER.debug("Disconnecting")
64-
self._disconnect()
6559
_LOGGER.debug("Done!")
6660

6761
def create_command(self, command: int) -> bytearray:

tion_btle/tion.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,28 @@ def _encode_mode(self, mode: str) -> int:
463463
:return: integer equivalent of mode
464464
"""
465465
return self.modes.index(mode) if mode in self.modes else 2
466+
467+
def pair(self):
468+
_LOGGER.debug("Pairing")
469+
self._connect()
470+
_LOGGER.debug("Connected. BT pairing ...")
471+
try:
472+
# use private methods to avoid disconnect if already paired
473+
self._btle._writeCmd('pair' + '\n')
474+
rsp = self._btle._waitResp('mgmt')
475+
if rsp['estat'][0] == 0 or rsp['estat'][0] == 19 or rsp['code'][0] == 'success':
476+
_LOGGER.debug(rsp['emsg'][0])
477+
else:
478+
_LOGGER.warning("Unexpected response: %s(%d)", rsp['emsg'][0], rsp['estat'][0])
479+
raise TionException(rsp['estat'][0], rsp['emsg'][0])
480+
# device-specific pairing
481+
_LOGGER.debug("Device-specific pairing ...")
482+
self._pair()
483+
_LOGGER.debug("Device pair is done")
484+
finally:
485+
_LOGGER.debug("disconnected")
486+
self._disconnect()
487+
488+
@abc.abstractmethod
489+
def _pair(self):
490+
"""Perform model-specific pair steps"""

0 commit comments

Comments
 (0)