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:
J. Nick Koston 2020-04-21 17:38:43 -05:00 committed by GitHub
parent ca08b70984
commit d06fce6997
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 75 additions and 14 deletions

View file

@ -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"![image](/api/homekit/pairingqr?{pairing_secret})"
)
hass.components.persistent_notification.create(
message, "HomeKit Setup", HOMEKIT_NOTIFY_ID