Add support for bluetooth pairing in esphome (#88603)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
5ccaa549d1
commit
ee89922c1b
1 changed files with 23 additions and 2 deletions
|
@ -43,6 +43,7 @@ CCCD_NOTIFY_BYTES = b"\x01\x00"
|
||||||
CCCD_INDICATE_BYTES = b"\x02\x00"
|
CCCD_INDICATE_BYTES = b"\x02\x00"
|
||||||
|
|
||||||
MIN_BLUETOOTH_PROXY_VERSION_HAS_CACHE = 3
|
MIN_BLUETOOTH_PROXY_VERSION_HAS_CACHE = 3
|
||||||
|
MIN_BLUETOOTH_PROXY_HAS_PAIRING = 4
|
||||||
|
|
||||||
DEFAULT_MAX_WRITE_WITHOUT_RESPONSE = DEFAULT_MTU - GATT_HEADER_SIZE
|
DEFAULT_MAX_WRITE_WITHOUT_RESPONSE = DEFAULT_MTU - GATT_HEADER_SIZE
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -386,13 +387,33 @@ class ESPHomeClient(BaseBleakClient):
|
||||||
@api_error_as_bleak_error
|
@api_error_as_bleak_error
|
||||||
async def pair(self, *args: Any, **kwargs: Any) -> bool:
|
async def pair(self, *args: Any, **kwargs: Any) -> bool:
|
||||||
"""Attempt to pair."""
|
"""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
|
@verify_connected
|
||||||
@api_error_as_bleak_error
|
@api_error_as_bleak_error
|
||||||
async def unpair(self) -> bool:
|
async def unpair(self) -> bool:
|
||||||
"""Attempt to unpair."""
|
"""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
|
@api_error_as_bleak_error
|
||||||
async def get_services(
|
async def get_services(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue