File: model.py

package info (click to toggle)
python-yolink-api 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 184 kB
  • sloc: python: 1,147; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,523 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
"""YoLink Basic Model."""

from typing import Any, Dict, Optional

from pydantic import BaseModel

from .exception import (
    YoLinkAuthFailError,
    YoLinkClientError,
    YoLinkDeviceConnectionFailed,
    YoLinkUnSupportedMethodError,
)


class BRDP(BaseModel):
    """BRDP of YoLink API."""

    code: Optional[str] = None
    desc: Optional[str] = None
    method: Optional[str] = None
    data: Dict[str, Any] = None
    event: Optional[str] = None

    def check_response(self):
        """Check API Response."""
        if self.code != "000000":
            if self.code == "000103":
                raise YoLinkAuthFailError(self.code, self.desc)
            if self.code == "000201":
                raise YoLinkDeviceConnectionFailed(self.code, self.desc)
            if self.code == "010203":
                raise YoLinkUnSupportedMethodError(self.code, self.desc)
            raise YoLinkClientError(self.code, self.desc)


class BSDPHelper:
    """YoLink API -> BSDP Builder."""

    _bsdp: Dict

    def __init__(self, device_id: str, device_token: str, method: str):
        """Constanst."""
        self._bsdp = {"method": method, "params": {}}
        if device_id is not None:
            self._bsdp["targetDevice"] = device_id
            self._bsdp["token"] = device_token

    def add_params(self, params: Dict):
        """Build params of BSDP."""
        self._bsdp["params"].update(params)
        return self

    def build(self) -> Dict:
        """Generate BSDP."""
        return self._bsdp