From 5f63a3f6979f46f96ad0b878290e9c10d6d8d66f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Nov 2024 09:08:07 -0500 Subject: [PATCH] Revert "Late-import base36 and QR code libraries; remove SUPPORT_QR_CODE flag" --- pyhap/__init__.py | 12 ++++++++++++ pyhap/accessory.py | 31 +++++++++++++++---------------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/pyhap/__init__.py b/pyhap/__init__.py index e8b2845c..abf37c01 100644 --- a/pyhap/__init__.py +++ b/pyhap/__init__.py @@ -5,3 +5,15 @@ CHARACTERISTICS_FILE = os.path.join(RESOURCE_DIR, "characteristics.json") SERVICES_FILE = os.path.join(RESOURCE_DIR, "services.json") + + +# Flag if QR Code dependencies are installed. +# Installation with `pip install HAP-python[QRCode]`. +SUPPORT_QR_CODE = False +try: + import base36 # noqa: F401 + import pyqrcode # noqa: F401 + + SUPPORT_QR_CODE = True +except ImportError: + pass diff --git a/pyhap/accessory.py b/pyhap/accessory.py index d67c7c2d..902fc7f7 100644 --- a/pyhap/accessory.py +++ b/pyhap/accessory.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional from uuid import UUID -from . import util +from . import SUPPORT_QR_CODE, util from .const import ( CATEGORY_BRIDGE, CATEGORY_OTHER, @@ -18,6 +18,10 @@ from .iid_manager import IIDManager from .service import Service +if SUPPORT_QR_CODE: + import base36 + from pyqrcode import QRCode + if TYPE_CHECKING: from .accessory_driver import AccessoryDriver @@ -186,12 +190,6 @@ def xhm_uri(self) -> str: :rtype: str """ - try: - import base36 - except ImportError as ie: - raise RuntimeError( - "The base36 module is required to generate X-HM:// URIs" - ) from ie payload = 0 payload |= 0 & 0x7 # version @@ -255,15 +253,7 @@ def setup_message(self): Installation through `pip install HAP-python[QRCode]` """ pincode = self.driver.state.pincode.decode() - try: - from qrcode import QRCode - except ImportError: - print( - "To use the QR Code feature, use 'pip install HAP-python[QRCode]'\n" - f"Enter this code in your HomeKit app on your iOS device: {pincode}", - flush=True, - ) - else: + if SUPPORT_QR_CODE: xhm_uri = self.xhm_uri() print(f"Setup payload: {xhm_uri}", flush=True) print( @@ -274,6 +264,15 @@ def setup_message(self): f"Or enter this code in your HomeKit app on your iOS device: {pincode}", flush=True, ) + else: + print( + "To use the QR Code feature, use 'pip install HAP-python[QRCode]'", + flush=True, + ) + print( + f"Enter this code in your HomeKit app on your iOS device: {pincode}", + flush=True, + ) @staticmethod def run_at_interval(seconds):