Skip to content

Commit

Permalink
Only connect and authenticate once
Browse files Browse the repository at this point in the history
My Viomi Smart Kettle SK-152A (product ID 1116, yunmi.kettle.v7) doesn't
like it when you try to connect too frequently, resulting in
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral
XX:XX:XX:XX:XX:XX, addr type: public.

Reusing the same connection makes demo.py work fine.

See drndos#4.
  • Loading branch information
mgedmin committed Jan 8, 2020
1 parent 47b1a44 commit b62824b
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions mikettle/mikettle.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ def __init__(self, mac, product_id, cache_timeout=600, retries=3, iface=None, to
token = MiKettle.generateRandomToken()
self._token = token

self._p = None
self._authenticated = False

def connect(self):
self._p = Peripheral(deviceAddr=self._mac, iface=self._iface)
self._p.setDelegate(self)
if self._p is None:
self._p = Peripheral(deviceAddr=self._mac, iface=self._iface)
self._p.setDelegate(self)

def name(self):
"""Return the name of the device."""
Expand Down Expand Up @@ -175,6 +179,8 @@ def bytes_to_int(bytes):
return result

def auth(self):
if self._authenticated:
return
auth_service = self._p.getServiceByUUID(_UUID_SERVICE_KETTLE)
auth_descriptors = auth_service.getDescriptors()

Expand All @@ -191,6 +197,7 @@ def auth(self):
self._p.writeCharacteristic(_HANDLE_AUTH, MiKettle.cipher(self._token, _KEY2), "true")

self._p.readCharacteristic(_HANDLE_VERSION)
self._authenticated = True

def subscribeToData(self):
controlService = self._p.getServiceByUUID(_UUID_SERVICE_KETTLE_DATA)
Expand Down

0 comments on commit b62824b

Please sign in to comment.