Skip to content

Commit

Permalink
Merge pull request #470 from akx/base36-qr-late
Browse files Browse the repository at this point in the history
Late-import base36 and QR code libraries; remove SUPPORT_QR_CODE flag
  • Loading branch information
ikalchev authored Apr 1, 2024
2 parents 5265b54 + 7823c03 commit 4bc2cb0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
12 changes: 0 additions & 12 deletions pyhap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 16 additions & 15 deletions pyhap/accessory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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):
Expand Down

0 comments on commit 4bc2cb0

Please sign in to comment.