File: duration.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 (38 lines) | stat: -rw-r--r-- 1,153 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
"""Provide a model for Z-Wave JS Duration."""

from __future__ import annotations

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


class DurationDataType(TypedDict, total=False):
    """Represent a Duration data dict type."""

    # https://github.com/zwave-js/node-zwave-js/blob/v11-dev/packages/core/src/values/Duration.ts#L11
    unit: Literal["seconds", "minutes"]  # required
    value: int | float


@dataclass
class Duration:
    """Duration class."""

    data: DurationDataType | Literal["unknown", "default"] = field(repr=False)
    unit: Literal["seconds", "minutes", "unknown", "default"] = field(init=False)
    value: int | float | None = field(init=False)

    def __post_init__(self) -> None:
        """Post init."""
        if isinstance(self.data, str):
            self.unit = self.data
            self.value = None
            return
        self.unit = self.data["unit"]
        self.value = self.data.get("value")

    def __repr__(self) -> str:
        """Return the representation."""
        if self.value:
            return f"{self.value} {self.unit}"
        return f"{self.unit} duration"