Skip to content

Commit 1feefd8

Browse files
authored
Revert "Late-import base36 and QR code libraries; remove SUPPORT_QR_CODE flag" (#477)
1 parent 20f5151 commit 1feefd8

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

pyhap/__init__.py

+12
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,15 @@
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

+15-16
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 util
7+
from . import SUPPORT_QR_CODE, util
88
from .const import (
99
CATEGORY_BRIDGE,
1010
CATEGORY_OTHER,
@@ -18,6 +18,10 @@
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+
2125

2226
if TYPE_CHECKING:
2327
from .accessory_driver import AccessoryDriver
@@ -186,12 +190,6 @@ def xhm_uri(self) -> str:
186190
187191
:rtype: str
188192
"""
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
195193
payload = 0
196194
payload |= 0 & 0x7 # version
197195

@@ -255,15 +253,7 @@ def setup_message(self):
255253
Installation through `pip install HAP-python[QRCode]`
256254
"""
257255
pincode = self.driver.state.pincode.decode()
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:
256+
if SUPPORT_QR_CODE:
267257
xhm_uri = self.xhm_uri()
268258
print(f"Setup payload: {xhm_uri}", flush=True)
269259
print(
@@ -274,6 +264,15 @@ def setup_message(self):
274264
f"Or enter this code in your HomeKit app on your iOS device: {pincode}",
275265
flush=True,
276266
)
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+
)
277276

278277
@staticmethod
279278
def run_at_interval(seconds):

0 commit comments

Comments
 (0)