Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Late-import base36 and QR code libraries; remove SUPPORT_QR_CODE flag" #477

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pyhap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@

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: 15 additions & 16 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 util
from . import SUPPORT_QR_CODE, util
from .const import (
CATEGORY_BRIDGE,
CATEGORY_OTHER,
Expand All @@ -18,6 +18,10 @@
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 @@ -186,12 +190,6 @@ 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 @@ -255,15 +253,7 @@ def setup_message(self):
Installation through `pip install HAP-python[QRCode]`
"""
pincode = self.driver.state.pincode.decode()
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:
if SUPPORT_QR_CODE:
xhm_uri = self.xhm_uri()
print(f"Setup payload: {xhm_uri}", flush=True)
print(
Expand All @@ -274,6 +264,15 @@ 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
Loading