Skip to content

Commit

Permalink
Merge pull request #6 from dougiteixeira/2023.01
Browse files Browse the repository at this point in the history
Update to core 2023.01 - 24/01/2023
  • Loading branch information
dougiteixeira authored Jan 24, 2023
2 parents 009ef8a + 58de163 commit 49c81ef
Show file tree
Hide file tree
Showing 104 changed files with 5,340 additions and 6,100 deletions.
6 changes: 5 additions & 1 deletion custom_components/tuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
device_ids.add(device.id)

hass.config_entries.async_setup_platforms(entry, PLATFORMS)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True


Expand Down Expand Up @@ -234,6 +234,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class DeviceListener(TuyaDeviceListener):
"""Device Update Listener."""

# pylint: disable=arguments-differ
# Library incorrectly defines methods as 'classmethod'
# https://github.com/tuya/tuya-iot-python-sdk/pull/48

def __init__(
self,
hass: HomeAssistant,
Expand Down
13 changes: 5 additions & 8 deletions custom_components/tuya/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

from homeassistant.backports.enum import StrEnum
from homeassistant.components.alarm_control_panel import (
SUPPORT_ALARM_ARM_AWAY,
SUPPORT_ALARM_ARM_HOME,
SUPPORT_ALARM_TRIGGER,
AlarmControlPanelEntity,
AlarmControlPanelEntityDescription,
AlarmControlPanelEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
Expand Down Expand Up @@ -99,7 +97,6 @@ def __init__(
description: AlarmControlPanelEntityDescription,
) -> None:
"""Init Tuya Alarm."""
self._attr_supported_features = 0
super().__init__(device, device_manager)
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"
Expand All @@ -109,16 +106,16 @@ def __init__(
description.key, dptype=DPType.ENUM, prefer_function=True
):
if Mode.HOME in supported_modes.range:
self._attr_supported_features |= SUPPORT_ALARM_ARM_HOME
self._attr_supported_features |= AlarmControlPanelEntityFeature.ARM_HOME

if Mode.ARM in supported_modes.range:
self._attr_supported_features |= SUPPORT_ALARM_ARM_AWAY
self._attr_supported_features |= AlarmControlPanelEntityFeature.ARM_AWAY

if Mode.SOS in supported_modes.range:
self._attr_supported_features |= SUPPORT_ALARM_TRIGGER
self._attr_supported_features |= AlarmControlPanelEntityFeature.TRIGGER

@property
def state(self):
def state(self) -> str | None:
"""Return the state of the device."""
if not (status := self.device.status.get(self.entity_description.key)):
return None
Expand Down
20 changes: 4 additions & 16 deletions custom_components/tuya/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def step_scaled(self) -> float:

def scale_value(self, value: float | int) -> float:
"""Scale a value."""
return value * self.step / (10**self.scale)
return value / (10**self.scale)

def scale_value_back(self, value: float | int) -> int:
"""Return raw value for scaled."""
return int((value * (10**self.scale)) / self.step)
return int(value * (10**self.scale))

def remap_value_to(
self,
Expand Down Expand Up @@ -131,6 +131,7 @@ def from_raw(cls, data: str) -> ElectricityTypeData:
class TuyaEntity(Entity):
"""Tuya base device."""

_attr_has_entity_name = True
_attr_should_poll = False

def __init__(self, device: TuyaDevice, device_manager: TuyaDeviceManager) -> None:
Expand All @@ -139,16 +140,6 @@ def __init__(self, device: TuyaDevice, device_manager: TuyaDeviceManager) -> Non
self.device = device
self.device_manager = device_manager

@property
def name(self) -> str | None:
"""Return Tuya device name."""
if (
hasattr(self, "entity_description")
and self.entity_description.name is not None
):
return f"{self.device.name} {self.entity_description.name}"
return self.device.name

@property
def device_info(self) -> DeviceInfo:
"""Return a device description for device registry."""
Expand All @@ -162,9 +153,6 @@ def device_info(self) -> DeviceInfo:
@property
def available(self) -> bool:
"""Return if the device is available."""
if self.device.category == "fdq":
return True

return self.device.online

@overload
Expand Down Expand Up @@ -201,7 +189,7 @@ def find_dpcode(
dpcodes: str | DPCode | tuple[DPCode, ...] | None,
*,
prefer_function: bool = False,
dptype: DPType = None,
dptype: DPType | None = None,
) -> DPCode | EnumTypeData | IntegerTypeData | None:
"""Find a matching DP code available on for this device."""
if dpcodes is None:
Expand Down
191 changes: 139 additions & 52 deletions custom_components/tuya/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription):
# DPCode, to use. If None, the key will be used as DPCode
dpcode: DPCode | None = None

# Value to consider binary sensor to be "on"
on_value: bool | float | int | str = True
# Value or values to consider binary sensor to be "on"
on_value: bool | float | int | str | set[bool | float | int | str] = True


# Commonly used sensors
Expand All @@ -46,6 +46,79 @@ class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription):
# end up being a binary sensor.
# https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq
BINARY_SENSORS: dict[str, tuple[TuyaBinarySensorEntityDescription, ...]] = {
# Multi-functional Sensor
# https://developer.tuya.com/en/docs/iot/categorydgnbj?id=Kaiuz3yorvzg3
"dgnbj": (
TuyaBinarySensorEntityDescription(
key=DPCode.GAS_SENSOR_STATE,
name="Gas",
icon="mdi:gas-cylinder",
device_class=BinarySensorDeviceClass.SAFETY,
on_value="alarm",
),
TuyaBinarySensorEntityDescription(
key=DPCode.CH4_SENSOR_STATE,
name="Methane",
device_class=BinarySensorDeviceClass.GAS,
on_value="alarm",
),
TuyaBinarySensorEntityDescription(
key=DPCode.VOC_STATE,
name="Volatile organic compound",
device_class=BinarySensorDeviceClass.SAFETY,
on_value="alarm",
),
TuyaBinarySensorEntityDescription(
key=DPCode.PM25_STATE,
name="Particulate matter 2.5 µm",
device_class=BinarySensorDeviceClass.SAFETY,
on_value="alarm",
),
TuyaBinarySensorEntityDescription(
key=DPCode.CO_STATE,
name="Carbon monoxide",
icon="mdi:molecule-co",
device_class=BinarySensorDeviceClass.SAFETY,
on_value="alarm",
),
TuyaBinarySensorEntityDescription(
key=DPCode.CO2_STATE,
icon="mdi:molecule-co2",
name="Carbon dioxide",
device_class=BinarySensorDeviceClass.SAFETY,
on_value="alarm",
),
TuyaBinarySensorEntityDescription(
key=DPCode.CH2O_STATE,
name="Formaldehyde",
device_class=BinarySensorDeviceClass.SAFETY,
on_value="alarm",
),
TuyaBinarySensorEntityDescription(
key=DPCode.DOORCONTACT_STATE,
name="Door",
device_class=BinarySensorDeviceClass.DOOR,
),
TuyaBinarySensorEntityDescription(
key=DPCode.WATERSENSOR_STATE,
name="Water leak",
device_class=BinarySensorDeviceClass.MOISTURE,
on_value="alarm",
),
TuyaBinarySensorEntityDescription(
key=DPCode.PRESSURE_STATE,
name="Pressure",
on_value="alarm",
),
TuyaBinarySensorEntityDescription(
key=DPCode.SMOKE_SENSOR_STATE,
name="Smoke",
icon="mdi:smoke-detector",
device_class=BinarySensorDeviceClass.SMOKE,
on_value="alarm",
),
TAMPER_BINARY_SENSOR,
),
# CO2 Detector
# https://developer.tuya.com/en/docs/iot/categoryco2bj?id=Kaiuz3wes7yuy
"co2bj": (
Expand Down Expand Up @@ -114,8 +187,9 @@ class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription):
# https://developer.tuya.com/en/docs/iot/s?id=K9gf48r5zjsy9
"mc": (
TuyaBinarySensorEntityDescription(
key=DPCode.DOORCONTACT_STATE,
key=DPCode.STATUS,
device_class=BinarySensorDeviceClass.DOOR,
on_value={"open", "opened"},
),
),
# Door Window Sensor
Expand All @@ -127,6 +201,64 @@ class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription):
),
TAMPER_BINARY_SENSOR,
),
# Access Control
# https://developer.tuya.com/en/docs/iot/s?id=Kb0o2xhlkxbet
"mk": (
TuyaBinarySensorEntityDescription(
key=DPCode.CLOSED_OPENED_KIT,
device_class=BinarySensorDeviceClass.LOCK,
on_value={"AQAB"},
),
),
# Smart Lock
# https://developer.tuya.com/en/docs/iot/f?id=Kb0o2vbzuzl81
"ms": (
TuyaBinarySensorEntityDescription(
key=DPCode.OPEN_INSIDE,
name="Unlock inside of door",
icon="mdi:home-export-outline",
),
TuyaBinarySensorEntityDescription(
key=DPCode.OPEN_CLOSE,
name="Locking and unlocking event",
icon="mdi:lock-pattern",
),
TuyaBinarySensorEntityDescription(
key=DPCode.DOOR_OPENED,
name="Door",
device_class=BinarySensorDeviceClass.DOOR,
),
TuyaBinarySensorEntityDescription(
key=DPCode.REVERSE_LOCK,
name="Double locking status",
),
TuyaBinarySensorEntityDescription(
key=DPCode.CHILD_LOCK,
name="Child lock",
icon="mdi:account-lock",
entity_category=EntityCategory.CONFIG,
),
TuyaBinarySensorEntityDescription(
key=DPCode.DOORBELL,
name="Doorbell",
device_class=BinarySensorDeviceClass.SOUND,
),
TuyaBinarySensorEntityDescription(
key=DPCode.ANTI_LOCK_OUTSIDE,
name="Double locking by lifting up",
),
TuyaBinarySensorEntityDescription(
key=DPCode.LOCK_MOTOR_STATE,
name="Status",
device_class=BinarySensorDeviceClass.LOCK,
),
TuyaBinarySensorEntityDescription(
key=DPCode.HIJACK,
name="Duress alert",
icon="mdi:lock-alert-outline",
device_class=BinarySensorDeviceClass.SAFETY,
),
),
# Luminance Sensor
# https://developer.tuya.com/en/docs/iot/categoryldcg?id=Kaiuz3n7u69l8
"ldcg": (
Expand Down Expand Up @@ -264,55 +396,6 @@ class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription):
on_value="tilt",
),
),
# Smart Lock
# https://developer.tuya.com/en/docs/iot/f?id=Kb0o2vbzuzl81
"ms": (
TuyaBinarySensorEntityDescription(
key=DPCode.OPEN_INSIDE,
name="Unlock Inside of Door",
icon="mdi:home-export-outline",
),
TuyaBinarySensorEntityDescription(
key=DPCode.OPEN_CLOSE,
name="Locking and Unlocking Event",
icon="mdi:lock-pattern",
),
TuyaBinarySensorEntityDescription(
key=DPCode.DOOR_OPENED,
name="Door",
device_class=BinarySensorDeviceClass.DOOR,
),
TuyaBinarySensorEntityDescription(
key=DPCode.REVERSE_LOCK,
name="Double Locking Status",
),
TuyaBinarySensorEntityDescription(
key=DPCode.CHILD_LOCK,
name="Child Lock",
icon="mdi:account-lock",
entity_category=EntityCategory.CONFIG,
),
TuyaBinarySensorEntityDescription(
key=DPCode.DOORBELL,
name="Doorbell",
device_class=BinarySensorDeviceClass.SOUND,
),
TuyaBinarySensorEntityDescription(
key=DPCode.ANTI_LOCK_OUTSIDE,
name="Double Locking by Lifting Up",
),
TuyaBinarySensorEntityDescription(
key=DPCode.LOCK_MOTOR_STATE,
name="Status",
device_class=BinarySensorDeviceClass.LOCK,
),
TuyaBinarySensorEntityDescription(
key=DPCode.HIJACK,
name="Duress Alert",
icon="mdi:lock-alert-outline",
device_class=BinarySensorDeviceClass.SAFETY,
),
),
}

# Lock (duplicate of 'ms')
Expand Down Expand Up @@ -381,4 +464,8 @@ def is_on(self) -> bool:
dpcode = self.entity_description.dpcode or self.entity_description.key
if dpcode not in self.device.status:
return False

if isinstance(self.entity_description.on_value, set):
return self.device.status[dpcode] in self.entity_description.on_value

return self.device.status[dpcode] == self.entity_description.on_value
Loading

0 comments on commit 49c81ef

Please sign in to comment.