2021-09-30 10:02:56 +00:00
|
|
|
"""Tuya Home Assistant Base Device Model."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-13 18:29:11 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
import json
|
|
|
|
import logging
|
2021-09-30 10:02:56 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from tuya_iot import TuyaDevice, TuyaDeviceManager
|
|
|
|
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-10-12 10:25:03 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
2021-09-30 10:02:56 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN, TUYA_HA_SIGNAL_UPDATE_ENTITY
|
|
|
|
|
2021-10-13 18:29:11 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class IntegerTypeData:
|
|
|
|
"""Integer Type Data."""
|
|
|
|
|
|
|
|
min: int
|
|
|
|
max: int
|
|
|
|
unit: str
|
|
|
|
scale: float
|
|
|
|
step: float
|
|
|
|
|
2021-10-13 20:58:10 +00:00
|
|
|
@classmethod
|
|
|
|
def from_json(cls, data: str) -> IntegerTypeData:
|
2021-10-13 18:29:11 +00:00
|
|
|
"""Load JSON string and return a IntegerTypeData object."""
|
2021-10-13 20:58:10 +00:00
|
|
|
return cls(**json.loads(data))
|
2021-10-13 18:29:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class EnumTypeData:
|
|
|
|
"""Enum Type Data."""
|
|
|
|
|
|
|
|
range: list[str]
|
|
|
|
|
2021-10-13 20:58:10 +00:00
|
|
|
@classmethod
|
|
|
|
def from_json(cls, data: str) -> EnumTypeData:
|
2021-10-13 18:29:11 +00:00
|
|
|
"""Load JSON string and return a EnumTypeData object."""
|
2021-10-13 20:58:10 +00:00
|
|
|
return cls(**json.loads(data))
|
2021-10-13 18:29:11 +00:00
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
|
|
|
|
class TuyaHaEntity(Entity):
|
|
|
|
"""Tuya base device."""
|
|
|
|
|
2021-10-12 03:37:18 +00:00
|
|
|
_attr_should_poll = False
|
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
def __init__(self, device: TuyaDevice, device_manager: TuyaDeviceManager) -> None:
|
|
|
|
"""Init TuyaHaEntity."""
|
2021-10-12 03:37:18 +00:00
|
|
|
self._attr_unique_id = f"tuya.{device.id}"
|
2021-09-30 10:02:56 +00:00
|
|
|
self.tuya_device = device
|
|
|
|
self.tuya_device_manager = device_manager
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str | None:
|
|
|
|
"""Return Tuya device name."""
|
|
|
|
return self.tuya_device.name
|
|
|
|
|
|
|
|
@property
|
2021-10-12 10:25:03 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2021-09-30 10:02:56 +00:00
|
|
|
"""Return a device description for device registry."""
|
2021-10-12 10:25:03 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self.tuya_device.id)},
|
|
|
|
manufacturer="Tuya",
|
|
|
|
name=self.tuya_device.name,
|
|
|
|
model=self.tuya_device.product_name,
|
|
|
|
)
|
2021-09-30 10:02:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if the device is available."""
|
|
|
|
return self.tuya_device.online
|
|
|
|
|
2021-10-12 10:25:03 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2021-09-30 10:02:56 +00:00
|
|
|
"""Call when entity is added to hass."""
|
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
f"{TUYA_HA_SIGNAL_UPDATE_ENTITY}_{self.tuya_device.id}",
|
|
|
|
self.async_write_ha_state,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def _send_command(self, commands: list[dict[str, Any]]) -> None:
|
|
|
|
"""Send command to the device."""
|
2021-10-13 18:29:11 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Sending commands for device %s: %s", self.tuya_device.id, commands
|
|
|
|
)
|
2021-09-30 10:02:56 +00:00
|
|
|
self.tuya_device_manager.send_commands(self.tuya_device.id, commands)
|
2021-10-13 18:29:11 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def scale(value: float | int, scale: float | int) -> float:
|
|
|
|
"""Scale a value."""
|
|
|
|
return value * 1.0 / (10 ** scale)
|