diff --git a/homeassistant/components/tuya/cover.py b/homeassistant/components/tuya/cover.py index 023c5730fde..a2f8eec2a98 100644 --- a/homeassistant/components/tuya/cover.py +++ b/homeassistant/components/tuya/cover.py @@ -5,11 +5,13 @@ from __future__ import annotations from dataclasses import dataclass from typing import Any -from tuya_device_handlers.device_wrapper.base import DeviceWrapper +from tuya_device_handlers.definition.cover import ( + TuyaCoverDefinition, + get_default_definition, +) from tuya_device_handlers.device_wrapper.cover import ( ControlBackModePercentageMappingWrapper, CoverClosedEnumWrapper, - CoverInstructionBooleanWrapper, CoverInstructionEnumWrapper, CoverInstructionSpecialEnumWrapper, ) @@ -153,21 +155,6 @@ COVERS: dict[DeviceCategory, tuple[TuyaCoverEntityDescription, ...]] = { } -def _get_instruction_wrapper( - device: CustomerDevice, description: TuyaCoverEntityDescription -) -> DeviceWrapper | None: - """Get the instruction wrapper for the cover entity.""" - if enum_wrapper := description.instruction_wrapper.find_dpcode( - device, description.key, prefer_function=True - ): - return enum_wrapper - - # Fallback to a boolean wrapper if available - return CoverInstructionBooleanWrapper.find_dpcode( - device, description.key, prefer_function=True - ) - - async def async_setup_entry( hass: HomeAssistant, entry: TuyaConfigEntry, @@ -184,32 +171,19 @@ async def async_setup_entry( device = manager.device_map[device_id] if descriptions := COVERS.get(device.category): entities.extend( - TuyaCoverEntity( - device, - manager, - description, - current_position=description.position_wrapper.find_dpcode( - device, description.current_position - ), - current_state_wrapper=description.current_state_wrapper.find_dpcode( - device, description.current_state - ), - instruction_wrapper=_get_instruction_wrapper( - device, description - ), - set_position=description.position_wrapper.find_dpcode( - device, description.set_position, prefer_function=True - ), - tilt_position=description.position_wrapper.find_dpcode( - device, - (DPCode.ANGLE_HORIZONTAL, DPCode.ANGLE_VERTICAL), - prefer_function=True, - ), - ) + TuyaCoverEntity(device, manager, description, definition) for description in descriptions if ( - description.key in device.function - or description.key in device.status_range + definition := get_default_definition( + device, + current_position_dpcode=description.current_position, + current_state_dpcode=description.current_state, + current_state_wrapper=description.current_state_wrapper, + instruction_dpcode=description.key, + instruction_wrapper=description.instruction_wrapper, + position_wrapper=description.position_wrapper, + set_position_dpcode=description.set_position, + ) ) ) @@ -232,34 +206,31 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity): device: CustomerDevice, device_manager: Manager, description: TuyaCoverEntityDescription, - *, - current_position: DeviceWrapper[int] | None, - current_state_wrapper: DeviceWrapper[bool] | None, - instruction_wrapper: DeviceWrapper[TuyaCoverAction] | None, - set_position: DeviceWrapper[int] | None, - tilt_position: DeviceWrapper[int] | None, + definition: TuyaCoverDefinition, ) -> None: """Init Tuya Cover.""" super().__init__(device, device_manager, description) self._attr_supported_features = CoverEntityFeature(0) - self._current_position = current_position or set_position - self._current_state_wrapper = current_state_wrapper - self._instruction_wrapper = instruction_wrapper - self._set_position = set_position - self._tilt_position = tilt_position + self._current_position = ( + definition.current_position_wrapper or definition.set_position_wrapper + ) + self._current_state_wrapper = definition.current_state_wrapper + self._instruction_wrapper = definition.instruction_wrapper + self._set_position = definition.set_position_wrapper + self._tilt_position = definition.tilt_position_wrapper - if instruction_wrapper: - if TuyaCoverAction.OPEN in instruction_wrapper.options: + if definition.instruction_wrapper: + if TuyaCoverAction.OPEN in definition.instruction_wrapper.options: self._attr_supported_features |= CoverEntityFeature.OPEN - if TuyaCoverAction.CLOSE in instruction_wrapper.options: + if TuyaCoverAction.CLOSE in definition.instruction_wrapper.options: self._attr_supported_features |= CoverEntityFeature.CLOSE - if TuyaCoverAction.STOP in instruction_wrapper.options: + if TuyaCoverAction.STOP in definition.instruction_wrapper.options: self._attr_supported_features |= CoverEntityFeature.STOP - if set_position: + if definition.set_position_wrapper: self._attr_supported_features |= CoverEntityFeature.SET_POSITION - if tilt_position: + if definition.tilt_position_wrapper: self._attr_supported_features |= CoverEntityFeature.SET_TILT_POSITION @property