wangzhibo
2026-08-30 c8515170fd399b4d2dfaebb59128d4674e58915e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import logging
from typing import Any
 
from ..core import WakewordDetector
from .base import Plugin
 
logger = logging.getLogger(__name__)
 
 
class WakeWordPlugin(Plugin):
    name = "wake_word"
    priority = 30
 
    def __init__(self) -> None:
        self.app = None
        self.detector: WakewordDetector | None = None
 
    def setup(self, app: Any) -> None:
        self.app = app
        self.detector = WakewordDetector(app.config)
        if not self.detector.enabled:
            self.detector = None
            return
        self.detector.on_detected(self._on_detected)
        self.detector.on_error = self._on_error
 
    def start(self) -> None:
        if self.detector is None:
            return
 
        audio_plugin = self.app.plugins.get_plugin("audio") if self.app else None
        audio_source = getattr(audio_plugin, "source", None)
        if audio_source is None:
            logger.warning("audio source unavailable, wakeword plugin not started")
            return
 
        self.detector.start(audio_source)
 
    def stop(self) -> None:
        if self.detector is not None:
            self.detector.stop()
 
    def shutdown(self) -> None:
        self.detector = None
        self.app = None
 
    def _on_detected(self, wake_word: str, full_text: str) -> None:
        if self.app is None:
            return
        self.app.handle_wake_word_detected(wake_word, full_text)
 
    def _on_error(self, error: Exception) -> None:
        logger.error("wakeword detection error: %s", error)