Skip to content

Commit 4bc2cb0

Browse files
authored
Merge pull request #470 from akx/base36-qr-late
Late-import base36 and QR code libraries; remove SUPPORT_QR_CODE flag
2 parents 5265b54 + 7823c03 commit 4bc2cb0

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

pyhap/__init__.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,3 @@
55

66
CHARACTERISTICS_FILE = os.path.join(RESOURCE_DIR, "characteristics.json")
77
SERVICES_FILE = os.path.join(RESOURCE_DIR, "services.json")
8-
9-
10-
# Flag if QR Code dependencies are installed.
11-
# Installation with `pip install HAP-python[QRCode]`.
12-
SUPPORT_QR_CODE = False
13-
try:
14-
import base36 # noqa: F401
15-
import pyqrcode # noqa: F401
16-
17-
SUPPORT_QR_CODE = True
18-
except ImportError:
19-
pass

pyhap/accessory.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional
55
from uuid import UUID
66

7-
from . import SUPPORT_QR_CODE, util
7+
from . import util
88
from .const import (
99
CATEGORY_BRIDGE,
1010
CATEGORY_OTHER,
@@ -18,10 +18,6 @@
1818
from .iid_manager import IIDManager
1919
from .service import Service
2020

21-
if SUPPORT_QR_CODE:
22-
import base36
23-
from pyqrcode import QRCode
24-
2521

2622
if 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

Comments
 (0)