diff --git a/pyhap/__init__.py b/pyhap/__init__.py index abf37c01..e8b2845c 100644 --- a/pyhap/__init__.py +++ b/pyhap/__init__.py @@ -5,15 +5,3 @@ 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 902fc7f7..d67c7c2d 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 SUPPORT_QR_CODE, util +from . import util from .const import ( CATEGORY_BRIDGE, CATEGORY_OTHER, @@ -18,10 +18,6 @@ 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 @@ -190,6 +186,12 @@ 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 @@ -253,7 +255,15 @@ def setup_message(self): Installation through `pip install HAP-python[QRCode]` """ pincode = self.driver.state.pincode.decode() - if SUPPORT_QR_CODE: + 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: xhm_uri = self.xhm_uri() print(f"Setup payload: {xhm_uri}", flush=True) print( @@ -264,15 +274,6 @@ 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):