Add support for bluetooth pairing in esphome (#88603)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
J. Nick Koston 2023-03-06 14:24:35 -10:00 committed by GitHub
parent 5ccaa549d1
commit ee89922c1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -43,6 +43,7 @@ CCCD_NOTIFY_BYTES = b"\x01\x00"
CCCD_INDICATE_BYTES = b"\x02\x00"
MIN_BLUETOOTH_PROXY_VERSION_HAS_CACHE = 3
MIN_BLUETOOTH_PROXY_HAS_PAIRING = 4
DEFAULT_MAX_WRITE_WITHOUT_RESPONSE = DEFAULT_MTU - GATT_HEADER_SIZE
_LOGGER = logging.getLogger(__name__)
@ -386,13 +387,33 @@ class ESPHomeClient(BaseBleakClient):
@api_error_as_bleak_error
async def pair(self, *args: Any, **kwargs: Any) -> bool:
"""Attempt to pair."""
raise NotImplementedError("Pairing is not available in ESPHome.")
if self._connection_version < MIN_BLUETOOTH_PROXY_HAS_PAIRING:
raise NotImplementedError(
"Pairing is not available in ESPHome with version {self._connection_version}."
)
response = await self._client.bluetooth_device_pair(self._address_as_int)
if response.paired:
return True
_LOGGER.error(
"Pairing with %s failed due to error: %s", self.address, response.error
)
return False
@verify_connected
@api_error_as_bleak_error
async def unpair(self) -> bool:
"""Attempt to unpair."""
raise NotImplementedError("Pairing is not available in ESPHome.")
if self._connection_version < MIN_BLUETOOTH_PROXY_HAS_PAIRING:
raise NotImplementedError(
"Unpairing is not available in ESPHome with version {self._connection_version}."
)
response = await self._client.bluetooth_device_unpair(self._address_as_int)
if response.success:
return True
_LOGGER.error(
"Unpairing with %s failed due to error: %s", self.address, response.error
)
return False
@api_error_as_bleak_error
async def get_services(