|
| 1 | +"""Tuya device wrapper.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import base64 |
| 6 | +from typing import TYPE_CHECKING, Any |
| 7 | + |
| 8 | +from .common import DPCodeEnumWrapper, DPCodeRawWrapper, DPCodeStringWrapper |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from tuya_sharing import CustomerDevice # type: ignore[import-untyped] |
| 12 | + |
| 13 | + |
| 14 | +class SimpleEventEnumWrapper(DPCodeEnumWrapper[tuple[str, None]]): |
| 15 | + """Wrapper for event enum DP codes. |
| 16 | +
|
| 17 | + Does not provide attributes. |
| 18 | + """ |
| 19 | + |
| 20 | + def read_device_status( |
| 21 | + self, device: CustomerDevice |
| 22 | + ) -> tuple[str, None] | None: |
| 23 | + """Return the event details.""" |
| 24 | + if (raw_value := self._read_dpcode_value(device)) is None: |
| 25 | + return None |
| 26 | + return (raw_value, None) |
| 27 | + |
| 28 | + |
| 29 | +class Base64Utf8StringWrapper(DPCodeStringWrapper[tuple[str, dict[str, Any]]]): |
| 30 | + """Wrapper for a string message received in a base64/UTF-8 STRING DPCode. |
| 31 | +
|
| 32 | + Raises 'triggered' event, with the message in the event attributes. |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self, dpcode: str, type_information: Any) -> None: |
| 36 | + """Init Base64Utf8StringWrapper.""" |
| 37 | + super().__init__(dpcode, type_information) |
| 38 | + self.options = ["triggered"] |
| 39 | + |
| 40 | + def read_device_status( |
| 41 | + self, device: CustomerDevice |
| 42 | + ) -> tuple[str, dict[str, Any]] | None: |
| 43 | + """Return the event attributes for the alarm message.""" |
| 44 | + if (raw_value := self._read_dpcode_value(device)) is None: |
| 45 | + return None |
| 46 | + return ( |
| 47 | + "triggered", |
| 48 | + {"message": base64.b64decode(raw_value).decode("utf-8")}, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +class Base64Utf8RawWrapper(DPCodeRawWrapper[tuple[str, dict[str, Any]]]): |
| 53 | + """Wrapper for a string message received in a base64/UTF-8 RAW DPCode. |
| 54 | +
|
| 55 | + Raises 'triggered' event, with the message in the event attributes. |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__(self, dpcode: str, type_information: Any) -> None: |
| 59 | + """Init _DoorbellPicWrapper.""" |
| 60 | + super().__init__(dpcode, type_information) |
| 61 | + self.options = ["triggered"] |
| 62 | + |
| 63 | + def read_device_status( |
| 64 | + self, device: CustomerDevice |
| 65 | + ) -> tuple[str, dict[str, Any]] | None: |
| 66 | + """Return the event attributes for the doorbell picture.""" |
| 67 | + if (status := self._read_dpcode_value(device)) is None: |
| 68 | + return None |
| 69 | + return ("triggered", {"message": status.decode("utf-8")}) |
0 commit comments