44from typing import TYPE_CHECKING , Any , Callable , Dict , Iterable , List , Optional
55from uuid import UUID
66
7- from . import SUPPORT_QR_CODE , util
7+ from . import util
88from .const import (
99 CATEGORY_BRIDGE ,
1010 CATEGORY_OTHER ,
1818from .iid_manager import IIDManager
1919from .service import Service
2020
21- if SUPPORT_QR_CODE :
22- import base36
23- from pyqrcode import QRCode
24-
2521
2622if TYPE_CHECKING :
2723 from .accessory_driver import AccessoryDriver
@@ -190,6 +186,12 @@ def xhm_uri(self) -> str:
190186
191187 :rtype: str
192188 """
189+ try :
190+ import base36
191+ except ImportError as ie :
192+ raise RuntimeError (
193+ "The base36 module is required to generate X-HM:// URIs"
194+ ) from ie
193195 payload = 0
194196 payload |= 0 & 0x7 # version
195197
@@ -253,7 +255,15 @@ def setup_message(self):
253255 Installation through `pip install HAP-python[QRCode]`
254256 """
255257 pincode = self .driver .state .pincode .decode ()
256- if SUPPORT_QR_CODE :
258+ try :
259+ from qrcode import QRCode
260+ except ImportError :
261+ print (
262+ "To use the QR Code feature, use 'pip install HAP-python[QRCode]'\n "
263+ f"Enter this code in your HomeKit app on your iOS device: { pincode } " ,
264+ flush = True ,
265+ )
266+ else :
257267 xhm_uri = self .xhm_uri ()
258268 print (f"Setup payload: { xhm_uri } " , flush = True )
259269 print (
@@ -264,15 +274,6 @@ def setup_message(self):
264274 f"Or enter this code in your HomeKit app on your iOS device: { pincode } " ,
265275 flush = True ,
266276 )
267- else :
268- print (
269- "To use the QR Code feature, use 'pip install HAP-python[QRCode]'" ,
270- flush = True ,
271- )
272- print (
273- f"Enter this code in your HomeKit app on your iOS device: { pincode } " ,
274- flush = True ,
275- )
276277
277278 @staticmethod
278279 def run_at_interval (seconds ):
0 commit comments