From 1bbeaa722ccd2889a96a4d4b54149d456b7c6107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20L=C3=B6nnberg?= Date: Thu, 23 Dec 2021 15:07:23 +0100 Subject: [PATCH] Support Tuya cover with operation mach_operate (#62650) --- homeassistant/components/tuya/const.py | 2 ++ homeassistant/components/tuya/cover.py | 32 +++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/tuya/const.py b/homeassistant/components/tuya/const.py index 4fc2f37f106..f19109e4104 100644 --- a/homeassistant/components/tuya/const.py +++ b/homeassistant/components/tuya/const.py @@ -206,6 +206,7 @@ class DPCode(str, Enum): LIGHT = "light" # Light LIGHT_MODE = "light_mode" LOCK = "lock" # Lock / Child lock + MACH_OPERATE = "mach_operate" MATERIAL = "material" # Material MODE = "mode" # Working mode / Mode MOTION_RECORD = "motion_record" @@ -221,6 +222,7 @@ class DPCode(str, Enum): PERCENT_STATE = "percent_state" PERCENT_STATE_2 = "percent_state_2" PERCENT_STATE_3 = "percent_state_3" + POSITION = "position" PHASE_A = "phase_a" PHASE_B = "phase_b" PHASE_C = "phase_c" diff --git a/homeassistant/components/tuya/cover.py b/homeassistant/components/tuya/cover.py index 572d440f937..a450c38e2a0 100644 --- a/homeassistant/components/tuya/cover.py +++ b/homeassistant/components/tuya/cover.py @@ -38,6 +38,9 @@ class TuyaCoverEntityDescription(CoverEntityDescription): current_state_inverse: bool = False current_position: DPCode | tuple[DPCode, ...] | None = None set_position: DPCode | None = None + open_instruction_value: str = "open" + close_instruction_value: str = "close" + stop_instruction_value: str = "stop" COVERS: dict[str, tuple[TuyaCoverEntityDescription, ...]] = { @@ -67,6 +70,16 @@ COVERS: dict[str, tuple[TuyaCoverEntityDescription, ...]] = { set_position=DPCode.PERCENT_CONTROL_3, device_class=CoverDeviceClass.CURTAIN, ), + TuyaCoverEntityDescription( + key=DPCode.MACH_OPERATE, + name="Curtain", + current_position=DPCode.POSITION, + set_position=DPCode.POSITION, + device_class=CoverDeviceClass.CURTAIN, + open_instruction_value="FZ", + close_instruction_value="ZZ", + stop_instruction_value="STOP", + ), # switch_1 is an undocumented code that behaves identically to control # It is used by the Kogan Smart Blinds Driver TuyaCoverEntityDescription( @@ -193,11 +206,11 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity): self._attr_supported_features |= SUPPORT_OPEN | SUPPORT_CLOSE elif device.function[description.key].type == "Enum": data_type = EnumTypeData.from_json(device.function[description.key].values) - if "open" in data_type.range: + if description.open_instruction_value in data_type.range: self._attr_supported_features |= SUPPORT_OPEN - if "close" in data_type.range: + if description.close_instruction_value in data_type.range: self._attr_supported_features |= SUPPORT_CLOSE - if "stop" in data_type.range: + if description.stop_instruction_value in data_type.range: self._attr_supported_features |= SUPPORT_STOP # Determine type to use for setting the position @@ -309,7 +322,7 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity): """Open the cover.""" value: bool | str = True if self.device.function[self.entity_description.key].type == "Enum": - value = "open" + value = self.entity_description.open_instruction_value commands: list[dict[str, str | int]] = [ {"code": self.entity_description.key, "value": value} @@ -336,7 +349,7 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity): """Close cover.""" value: bool | str = False if self.device.function[self.entity_description.key].type == "Enum": - value = "close" + value = self.entity_description.close_instruction_value commands: list[dict[str, str | int]] = [ {"code": self.entity_description.key, "value": value} @@ -381,7 +394,14 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity): def stop_cover(self, **kwargs: Any) -> None: """Stop the cover.""" - self._send_command([{"code": self.entity_description.key, "value": "stop"}]) + self._send_command( + [ + { + "code": self.entity_description.key, + "value": self.entity_description.stop_instruction_value, + } + ] + ) def set_cover_tilt_position(self, **kwargs): """Move the cover tilt to a specific position."""