Skip to content

Commit 8c0239c

Browse files
authored
Only lookup the characteristics once (#67)
1 parent 96b5648 commit 8c0239c

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

switchbot/devices/device.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ async def _send_command_locked(self, key: str, command: bytes) -> bytes:
107107
_LOGGER.debug(
108108
"%s: Connnected to switchbot: %s", self.name, client.is_connected
109109
)
110+
read_char = client.services.get_characteristic(_sb_uuid(comms_type="rx"))
111+
write_char = client.services.get_characteristic(_sb_uuid(comms_type="tx"))
110112
future: asyncio.Future[bytearray] = asyncio.Future()
111113

112114
def _notification_handler(_sender: int, data: bytearray) -> None:
@@ -117,17 +119,17 @@ def _notification_handler(_sender: int, data: bytearray) -> None:
117119
future.set_result(data)
118120

119121
_LOGGER.debug("%s: Subscribe to notifications", self.name)
120-
await client.start_notify(_sb_uuid(comms_type="rx"), _notification_handler)
122+
await client.start_notify(read_char, _notification_handler)
121123

122124
_LOGGER.debug("%s: Sending command, %s", self.name, key)
123-
await client.write_gatt_char(_sb_uuid(comms_type="tx"), command, False)
125+
await client.write_gatt_char(write_char, command, False)
124126

125127
async with async_timeout.timeout(5):
126128
notify_msg = await future
127129
_LOGGER.info("%s: Notification received: %s", self.name, notify_msg)
128130

129131
_LOGGER.debug("%s: UnSubscribe to notifications", self.name)
130-
await client.stop_notify(_sb_uuid(comms_type="rx"))
132+
await client.stop_notify(read_char)
131133

132134
finally:
133135
if client:

0 commit comments

Comments
 (0)