File: events.py

package info (click to toggle)
python-aiounifi 79-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: python: 11,124; sh: 5; makefile: 5
file content (65 lines) | stat: -rw-r--r-- 1,928 bytes parent folder | download
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
54
55
56
57
58
59
60
61
62
63
64
65
"""Manage events from UniFi Network Controller."""

from __future__ import annotations

from collections.abc import Callable
import logging
from typing import TYPE_CHECKING

from ..models.event import Event, EventKey
from ..models.message import Message, MessageKey

if TYPE_CHECKING:
    from ..controller import Controller

LOGGER = logging.getLogger(__name__)


SubscriptionCallback = Callable[[Event], None]
SubscriptionType = tuple[SubscriptionCallback, tuple[EventKey, ...] | None]
UnsubscribeType = Callable[[], None]


class EventHandler:
    """Event handler class."""

    def __init__(self, controller: Controller) -> None:
        """Initialize API items."""
        self.controller = controller
        self._subscribers: list[SubscriptionType] = []

        controller.messages.subscribe(self.handler, MessageKey.EVENT)

    def subscribe(
        self,
        callback: SubscriptionCallback,
        event_filter: tuple[EventKey, ...] | EventKey | None = None,
    ) -> UnsubscribeType:
        """Subscribe to events.

        "callback" - callback function to call when on event.
        Return function to unsubscribe.
        """
        if isinstance(event_filter, EventKey):
            event_filter = (event_filter,)

        subscription = (callback, event_filter)
        self._subscribers.append(subscription)

        def unsubscribe() -> None:
            self._subscribers.remove(subscription)

        return unsubscribe

    def handler(self, message: Message) -> None:
        """Receive event from message handler and identifies where the event belong."""
        event = Event(message.data)

        for callback, event_filter in self._subscribers:
            if event_filter is not None and event.key not in event_filter:
                continue
            callback(event)

    def __len__(self) -> int:
        """List number of event subscribers."""
        return len(self._subscribers)