Skip to content

Commit 86ff850

Browse files
committed
fix: Lazy import pycaw to avoid COM initialization conflict
1 parent 324ea8d commit 86ff850

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/core/models.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@
1919
logger = logging.getLogger(__name__)
2020

2121

22-
# Windows volume control
23-
try:
24-
from pycaw.pycaw import AudioUtilities
25-
PYCAW_AVAILABLE = True
26-
except ImportError:
27-
PYCAW_AVAILABLE = False
28-
logger.warning("pycaw not available, duck/unduck will be disabled")
22+
# pycaw availability flag (lazy import to avoid COM conflicts)
23+
PYCAW_AVAILABLE = None # Will be set on first use
24+
25+
26+
def _check_pycaw():
27+
"""Check pycaw availability (lazy import)"""
28+
global PYCAW_AVAILABLE
29+
if PYCAW_AVAILABLE is None:
30+
try:
31+
from pycaw.pycaw import AudioUtilities # noqa: F401
32+
PYCAW_AVAILABLE = True
33+
except (ImportError, OSError):
34+
PYCAW_AVAILABLE = False
35+
logger.warning("pycaw not available, duck/unduck will be disabled")
36+
return PYCAW_AVAILABLE
2937

3038

3139
class WakeWordType(str, Enum):
@@ -101,11 +109,12 @@ def __init__(self):
101109

102110
def _init_volume_interface(self) -> None:
103111
"""Initialize volume interface"""
104-
if not PYCAW_AVAILABLE:
112+
if not _check_pycaw():
105113
logger.warning("pycaw not available, volume control disabled")
106114
return
107115

108116
try:
117+
from pycaw.pycaw import AudioUtilities
109118
devices = AudioUtilities.GetSpeakers()
110119
# New pycaw uses EndpointVolume property
111120
self._volume_interface = devices.EndpointVolume

start.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
"""
2-
Home Assistant Windows 客户端启动脚本
3-
解决 Python 模块导入路径问题
2+
Home Assistant Windows Client Startup Script
43
"""
54

65
import sys
76
import os
87

9-
# 添加项目根目录到 Python 路径
8+
# Add project root to Python path
109
project_root = os.path.dirname(os.path.abspath(__file__))
1110
if project_root not in sys.path:
1211
sys.path.insert(0, project_root)
1312

14-
# 导入并运行主程序
13+
# Import and run main program
1514
if __name__ == "__main__":
1615
from src.main import main
1716
main()

0 commit comments

Comments
 (0)