Skip to content

Commit

Permalink
Use a specifc exception for the pairing method error
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Schulze committed Jan 28, 2022
1 parent 52816d4 commit 1252836
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions homekit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'Controller', 'BluetoothAdapterError', 'AccessoryDisconnectedError', 'AccessoryNotFoundError',
'AlreadyPairedError', 'AuthenticationError', 'BackoffError', 'BusyError', 'CharacteristicPermissionError',
'ConfigLoadingError', 'ConfigSavingError', 'ConfigurationError', 'FormatError', 'HomeKitException',
'HttpException', 'IncorrectPairingIdError', 'InvalidAuthTagError', 'InvalidError', 'InvalidSignatureError',
'HttpException', 'IncorrectPairingIdError', 'PairingMethodError', 'InvalidAuthTagError', 'InvalidError', 'InvalidSignatureError',
'MaxPeersError', 'MaxTriesError', 'ProtocolError', 'RequestRejected', 'UnavailableError', 'UnknownError',
'UnpairedError'
]
Expand All @@ -27,7 +27,7 @@
from homekit.exceptions import BluetoothAdapterError, AccessoryDisconnectedError, AccessoryNotFoundError, \
AlreadyPairedError, AuthenticationError, BackoffError, BusyError, CharacteristicPermissionError, \
ConfigLoadingError, ConfigSavingError, ConfigurationError, FormatError, HomeKitException, HttpException, \
IncorrectPairingIdError, InvalidAuthTagError, InvalidError, InvalidSignatureError, MaxPeersError, MaxTriesError, \
IncorrectPairingIdError, PairingMethodError, InvalidAuthTagError, InvalidError, InvalidSignatureError, MaxPeersError, MaxTriesError, \
ProtocolError, RequestRejected, UnavailableError, UnknownError, UnpairedError

from homekit.tools import IP_TRANSPORT_SUPPORTED
Expand Down
33 changes: 14 additions & 19 deletions homekit/controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from enum import IntEnum

from homekit.exceptions import AccessoryNotFoundError, ConfigLoadingError, UnknownError, \
AuthenticationError, ConfigSavingError, AlreadyPairedError, TransportNotSupportedError, MalformedPinError
AuthenticationError, ConfigSavingError, AlreadyPairedError, TransportNotSupportedError, \
MalformedPinError, PairingMethodError
from homekit.protocol import States, Methods, Errors, TlvTypes
from homekit.http_impl import HomeKitHTTPConnection
from homekit.protocol.statuscodes import HapStatusCodes
Expand All @@ -47,6 +48,8 @@
from homekit.controller.ip_implementation import IpPairing, IpSession


logging.basicConfig(level=logging.DEBUG)

class Controller(object):
"""
This class represents a HomeKit controller (normally your iPhone or iPad).
Expand Down Expand Up @@ -466,24 +469,12 @@ def perform_pairing_ble(self, alias, accessory_mac, pin, adapter='hci0', strateg
try:
finish_pairing = self.start_pairing_ble(alias, accessory_mac, adapter, with_hw_auth)
return finish_pairing(pin)
except AccessoryNotFoundError:
raise
except AlreadyPairedError:
raise
except UnavailableError:
raise
except MaxTriesError:
raise
except BusyError:
raise
except MaxPeersError:
raise
except UnavailableError:
raise
except:
if with_hw_auth and (Controller.PairingStrategy.Auto == strategy):
with_hw_auth = False
continue
except PairingMethodError:
if Controller.PairingStrategy.Auto == strategy:
if with_hw_auth:
with_hw_auth = False
continue
raise AuthenticationError('Pairing failed')

raise

Expand Down Expand Up @@ -550,6 +541,10 @@ def finish_pairing(pin):
except StopIteration as result:
pairing = result.value
break
except:
if tlv8.EntryList(request).first_by_id(TlvTypes.State).data == States.M3:
raise PairingMethodError('state: M3 failed')
raise

pairing['AccessoryMAC'] = accessory_mac
pairing['Connection'] = 'BLE'
Expand Down
11 changes: 10 additions & 1 deletion homekit/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#


from typing import Protocol


class HomeKitException(Exception):
"""Generic HomeKit exception.
Attributes:
Expand Down Expand Up @@ -65,6 +68,13 @@ class AuthenticationError(ProtocolError):
"""
pass

class PairingMethodError(ProtocolError):
"""
Raised in on pairing if the current pairing method is not supported.
TODO: Current implementation centers around an error in M3, but it would be better to have FF or checks at the verification
"""
pass


class BackoffError(ProtocolError):
"""
Expand Down Expand Up @@ -133,7 +143,6 @@ class IncorrectPairingIdError(ProtocolError):
"""
pass


class InvalidSignatureError(ProtocolError):
"""
Raised upon receipt of an invalid signature either from an accessory or from the controller.
Expand Down

0 comments on commit 1252836

Please sign in to comment.