Accept homekit_controller pairing codes both with and without dashes (#30273)
* Handle MalformedPinError from homekit_python * Handle both formats of pin codes
This commit is contained in:
parent
13116d8d3f
commit
8a22a38353
2 changed files with 69 additions and 8 deletions
|
@ -2,6 +2,7 @@
|
|||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
|
||||
import homekit
|
||||
from homekit.controller.ip_implementation import IpPairing
|
||||
|
@ -17,6 +18,8 @@ HOMEKIT_IGNORE = ["Home Assistant Bridge"]
|
|||
HOMEKIT_DIR = ".homekit"
|
||||
PAIRING_FILE = "pairing.json"
|
||||
|
||||
PIN_FORMAT = re.compile(r"^(\d{3})-{0,1}(\d{2})-{0,1}(\d{3})$")
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
@ -59,6 +62,20 @@ def find_existing_host(hass, serial):
|
|||
return entry
|
||||
|
||||
|
||||
def ensure_pin_format(pin):
|
||||
"""
|
||||
Ensure a pin code is correctly formatted.
|
||||
|
||||
Ensures a pin code is in the format 111-11-111. Handles codes with and without dashes.
|
||||
|
||||
If incorrect code is entered, an exception is raised.
|
||||
"""
|
||||
match = PIN_FORMAT.search(pin)
|
||||
if not match:
|
||||
raise homekit.exceptions.MalformedPinError(f"Invalid PIN code f{pin}")
|
||||
return "{}-{}-{}".format(*match.groups())
|
||||
|
||||
|
||||
@config_entries.HANDLERS.register(DOMAIN)
|
||||
class HomekitControllerFlowHandler(config_entries.ConfigFlow):
|
||||
"""Handle a HomeKit config flow."""
|
||||
|
@ -277,6 +294,8 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
|
|||
if pair_info:
|
||||
code = pair_info["pairing_code"]
|
||||
try:
|
||||
code = ensure_pin_format(code)
|
||||
|
||||
await self.hass.async_add_executor_job(self.finish_pairing, code)
|
||||
|
||||
pairing = self.controller.pairings.get(self.hkid)
|
||||
|
@ -284,6 +303,9 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
|
|||
return await self._entry_from_accessory(pairing)
|
||||
|
||||
errors["pairing_code"] = "unable_to_pair"
|
||||
except homekit.exceptions.MalformedPinError:
|
||||
# Library claimed pin was invalid before even making an API call
|
||||
errors["pairing_code"] = "authentication_error"
|
||||
except homekit.AuthenticationError:
|
||||
# PairSetup M4 - SRP proof failed
|
||||
# PairSetup M6 - Ed25519 signature verification failed
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue