Display Homekit QR code when pairing (#34449)
* Display a QR code for homekit pairing This will reduce the failure rate with HomeKit pairing because there is less chance of entry error. * Add coverage * Test that the qr code is created * I cannot spell * Update homeassistant/components/homekit/__init__.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Update homeassistant/components/homekit/__init__.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
1 parent
ca08b70984
commit
d06fce6997
10 files changed
+75
-14
No files matched your search
@@ -2,11 +2,13 @@
|
||||
import ipaddress
|
||||
import logging
|
||||
|
||||
from aiohttp import web
|
||||
import voluptuous as vol
|
||||
from zeroconf import InterfaceChoice
|
||||
|
||||
from homeassistant.components import cover, vacuum
|
||||
from homeassistant.components.cover import DEVICE_CLASS_GARAGE, DEVICE_CLASS_GATE
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.components.media_player import DEVICE_CLASS_TV
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
@@ -28,6 +30,7 @@ from homeassistant.const import (
|
||||
UNIT_PERCENTAGE,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import Unauthorized
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entityfilter import FILTER_SCHEMA
|
||||
from homeassistant.util import get_local_ip
|
||||
@@ -56,6 +59,8 @@ from .const import (
|
||||
DOMAIN,
|
||||
EVENT_HOMEKIT_CHANGED,
|
||||
HOMEKIT_FILE,
|
||||
HOMEKIT_PAIRING_QR,
|
||||
HOMEKIT_PAIRING_QR_SECRET,
|
||||
SERVICE_HOMEKIT_RESET_ACCESSORY,
|
||||
SERVICE_HOMEKIT_START,
|
||||
TYPE_FAUCET,
|
||||
@@ -129,6 +134,8 @@ async def async_setup(hass, config):
|
||||
aid_storage = hass.data[AID_STORAGE] = AccessoryAidStorage(hass)
|
||||
await aid_storage.async_initialize()
|
||||
|
||||
hass.http.register_view(HomeKitPairingQRView)
|
||||
|
||||
conf = config[DOMAIN]
|
||||
name = conf[CONF_NAME]
|
||||
port = conf[CONF_PORT]
|
||||
@@ -445,7 +452,9 @@ class HomeKit:
|
||||
self.driver.add_accessory(self.bridge)
|
||||
|
||||
if not self.driver.state.paired:
|
||||
show_setup_message(self.hass, self.driver.state.pincode)
|
||||
show_setup_message(
|
||||
self.hass, self.driver.state.pincode, self.bridge.xhm_uri()
|
||||
)
|
||||
|
||||
_LOGGER.debug("Driver start")
|
||||
self.hass.add_job(self.driver.start)
|
||||
@@ -459,3 +468,21 @@ class HomeKit:
|
||||
|
||||
_LOGGER.debug("Driver stop")
|
||||
self.hass.add_job(self.driver.stop)
|
||||
|
||||
|
||||
class HomeKitPairingQRView(HomeAssistantView):
|
||||
"""Display the homekit pairing code at a protected url."""
|
||||
|
||||
url = "/api/homekit/pairingqr"
|
||||
name = "api:homekit:pairingqr"
|
||||
requires_auth = False
|
||||
|
||||
# pylint: disable=no-self-use
|
||||
async def get(self, request):
|
||||
"""Retrieve the pairing QRCode image."""
|
||||
if request.query_string != request.app["hass"].data[HOMEKIT_PAIRING_QR_SECRET]:
|
||||
raise Unauthorized()
|
||||
return web.Response(
|
||||
body=request.app["hass"].data[HOMEKIT_PAIRING_QR],
|
||||
content_type="image/svg+xml",
|
||||
)
|
||||
@@ -255,4 +255,4 @@ class HomeDriver(AccessoryDriver):
|
||||
def unpair(self, client_uuid):
|
||||
"""Override super function to show setup message if unpaired."""
|
||||
super().unpair(client_uuid)
|
||||
show_setup_message(self.hass, self.state.pincode)
|
||||
show_setup_message(self.hass, self.state.pincode, self.accessory.xhm_uri())
|
||||
@@ -6,7 +6,8 @@ DOMAIN = "homekit"
|
||||
HOMEKIT_FILE = ".homekit.state"
|
||||
HOMEKIT_NOTIFY_ID = 4663548
|
||||
AID_STORAGE = "homekit-aid-allocations"
|
||||
|
||||
HOMEKIT_PAIRING_QR = "homekit-pairing-qr"
|
||||
HOMEKIT_PAIRING_QR_SECRET = "homekit-pairing-qr-secret"
|
||||
|
||||
# #### Attributes ####
|
||||
ATTR_DISPLAY_NAME = "display_name"
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
"domain": "homekit",
|
||||
"name": "HomeKit",
|
||||
"documentation": "https://www.home-assistant.io/integrations/homekit",
|
||||
"requirements": ["HAP-python==2.8.2", "fnvhash==0.1.0"],
|
||||
"codeowners": ["@bdraco"],
|
||||
"after_dependencies": ["logbook"]
|
||||
"requirements": ["HAP-python==2.8.2","fnvhash==0.1.0","PyQRCode==1.2.1","base36==0.1.1"],
|
||||
"dependencies": ["http"],
|
||||
"after_dependencies": ["logbook"],
|
||||
"codeowners": ["@bdraco"]
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
"""Collection of useful functions for the HomeKit component."""
|
||||
from collections import OrderedDict, namedtuple
|
||||
import io
|
||||
import logging
|
||||
import secrets
|
||||
|
||||
import pyqrcode
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import fan, media_player, sensor
|
||||
@@ -27,6 +30,8 @@ from .const import (
|
||||
FEATURE_PLAY_STOP,
|
||||
FEATURE_TOGGLE_MUTE,
|
||||
HOMEKIT_NOTIFY_ID,
|
||||
HOMEKIT_PAIRING_QR,
|
||||
HOMEKIT_PAIRING_QR_SECRET,
|
||||
TYPE_FAUCET,
|
||||
TYPE_OUTLET,
|
||||
TYPE_SHOWER,
|
||||
@@ -205,13 +210,24 @@ class HomeKitSpeedMapping:
|
||||
return list(self.speed_ranges.keys())[0]
|
||||
|
||||
|
||||
def show_setup_message(hass, pincode):
|
||||
def show_setup_message(hass, pincode, uri):
|
||||
"""Display persistent notification with setup information."""
|
||||
pin = pincode.decode()
|
||||
_LOGGER.info("Pincode: %s", pin)
|
||||
|
||||
buffer = io.BytesIO()
|
||||
url = pyqrcode.create(uri)
|
||||
url.svg(buffer, scale=5)
|
||||
pairing_secret = secrets.token_hex(32)
|
||||
|
||||
hass.data[HOMEKIT_PAIRING_QR] = buffer.getvalue()
|
||||
hass.data[HOMEKIT_PAIRING_QR_SECRET] = pairing_secret
|
||||
|
||||
message = (
|
||||
f"To set up Home Assistant in the Home App, enter the "
|
||||
f"following code:\n### {pin}"
|
||||
f"To set up Home Assistant in the Home App, "
|
||||
f"scan the QR code or enter the following code:\n"
|
||||
f"### {pin}\n"
|
||||
f""
|
||||
)
|
||||
hass.components.persistent_notification.create(
|
||||
message, "HomeKit Setup", HOMEKIT_NOTIFY_ID
|
||||
|
||||
Reference in new issue
Block a user