File: notification.py

package info (click to toggle)
zwave-js-server-python 0.67.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,820 kB
  • sloc: python: 15,886; sh: 21; javascript: 16; makefile: 2
file content (181 lines) | stat: -rw-r--r-- 6,165 bytes parent folder | download | duplicates (2)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Model for a Zwave Node's Notification Event.

https://zwave-js.github.io/node-zwave-js/#/api/node?id=quotnotificationquot
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Literal, TypedDict

from ..const.command_class.multilevel_switch import MultilevelSwitchCommand
from ..const.command_class.power_level import PowerLevelTestStatus
from ..util.helpers import parse_buffer

if TYPE_CHECKING:
    from .node import Node


class BaseNotificationDataType(TypedDict):
    """Represent a base notification event data dict type."""

    source: Literal["node"]  # required
    event: Literal["notification"]  # required
    nodeId: int  # required
    endpointIndex: int  # required
    ccId: int  # required


@dataclass
class BaseNotification:
    """Model for a Zwave Node's notification event."""

    node: Node
    data: BaseNotificationDataType = field(repr=False)
    node_id: int = field(init=False, repr=False)
    endpoint_idx: int = field(init=False)
    command_class: int = field(init=False)

    def __post_init__(self) -> None:
        """Post initialization."""
        self.node_id = self.data["nodeId"]
        self.endpoint_idx = self.data["endpointIndex"]
        self.command_class = self.data["ccId"]


class EntryControlNotificationArgsDataType(TypedDict, total=False):
    """Represent args for a Entry Control CC notification event data dict type."""

    eventType: int  # required
    eventTypeLabel: str  # required
    dataType: int  # required
    dataTypeLabel: str  # required
    eventData: str | dict[str, Any]


class EntryControlNotificationDataType(BaseNotificationDataType):
    """Represent an Entry Control CC notification event data dict type."""

    args: EntryControlNotificationArgsDataType  # required


@dataclass
class EntryControlNotification(BaseNotification):
    """Model for a Zwave Node's Entry Control CC notification event."""

    data: EntryControlNotificationDataType = field(repr=False)
    event_type: int = field(init=False)
    event_type_label: str = field(init=False)
    data_type: int = field(init=False)
    data_type_label: str = field(init=False)
    event_data: str | dict[str, Any] | None = field(init=False, default=None)

    def __post_init__(self) -> None:
        """Post initialize."""
        super().__post_init__()
        self.event_type = self.data["args"]["eventType"]
        self.event_type_label = self.data["args"]["eventTypeLabel"]
        self.data_type = self.data["args"]["dataType"]
        self.data_type_label = self.data["args"]["dataTypeLabel"]
        if event_data := self.data["args"].get("eventData"):
            self.event_data = parse_buffer(event_data)


class NotificationNotificationArgsDataType(TypedDict, total=False):
    """Represent args for a Notification CC notification event data dict type."""

    type: int  # required
    label: str  # required
    event: int  # required
    eventLabel: str  # required
    parameters: dict[str, Any]


class NotificationNotificationDataType(BaseNotificationDataType):
    """Represent a Notification CC notification event data dict type."""

    args: NotificationNotificationArgsDataType  # required


@dataclass
class NotificationNotification(BaseNotification):
    """Model for a Zwave Node's Notification CC notification event."""

    data: NotificationNotificationDataType = field(repr=False)
    type_: int = field(init=False)
    label: str = field(init=False)
    event: int = field(init=False)
    event_label: str = field(init=False)
    parameters: dict[str, Any] = field(init=False)

    def __post_init__(self) -> None:
        """Post initialize."""
        super().__post_init__()
        self.type_ = self.data["args"]["type"]
        self.label = self.data["args"]["label"]
        self.event = self.data["args"]["event"]
        self.event_label = self.data["args"]["eventLabel"]
        self.parameters = self.data["args"].get("parameters", {})


class PowerLevelNotificationArgsDataType(TypedDict):
    """Represent args for a Power Level CC notification event data dict type."""

    testNodeId: int
    status: int
    acknowledgedFrames: int


class PowerLevelNotificationDataType(BaseNotificationDataType):
    """Represent a Power Level CC notification event data dict type."""

    args: PowerLevelNotificationArgsDataType  # required


@dataclass
class PowerLevelNotification(BaseNotification):
    """Model for a Zwave Node's Power Level CC notification event."""

    data: PowerLevelNotificationDataType = field(repr=False)
    test_node_id: int = field(init=False)
    status: PowerLevelTestStatus = field(init=False)
    acknowledged_frames: int = field(init=False)

    def __post_init__(self) -> None:
        """Post initialize."""
        super().__post_init__()
        self.test_node_id = self.data["args"]["testNodeId"]
        self.status = PowerLevelTestStatus(self.data["args"]["status"])
        self.acknowledged_frames = self.data["args"]["acknowledgedFrames"]


class MultilevelSwitchNotificationArgsDataType(TypedDict, total=False):
    """Represent args for a Multi Level Switch CC notification event data dict type."""

    eventType: int  # required
    eventTypeLabel: str  # required
    direction: str


class MultilevelSwitchNotificationDataType(BaseNotificationDataType):
    """Represent a Multi Level Switch CC notification event data dict type."""

    args: MultilevelSwitchNotificationArgsDataType  # required


@dataclass
class MultilevelSwitchNotification(BaseNotification):
    """Model for a Zwave Node's Multi Level CC notification event."""

    data: MultilevelSwitchNotificationDataType = field(repr=False)
    event_type: MultilevelSwitchCommand = field(init=False)
    event_type_label: str = field(init=False)
    direction: str | None = field(init=False)

    def __post_init__(self) -> None:
        """Post initialize."""
        super().__post_init__()
        self.event_type = MultilevelSwitchCommand(self.data["args"]["eventType"])
        self.event_type_label = self.data["args"]["eventTypeLabel"]
        self.direction = self.data["args"].get("direction")