OpenBeken on MOES/Andeli TuyaMCU switch

In this thread I'll explain how to re-flash this TuyaMCU smartswitch with OpenBeken and connect it to HomeAssistant. OpenBeken is an alternative, open source software for IoT devices, offering, among others independence from the manufacturer's cloud, compatibility with many ecosystems, configurability and support for various platforms, including BK7231T, BK7231N, XR809 and BL602.
For more information about OpenBK, please see: [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
For more information about BL602 support: Tasmota replacement for BL602, programming, pairing with Home Assistant.
Where to purchase the switch
The switch was bought from AliExpress by searching: ANDELI Tuya Smart Life Wifi Light Switch with Glass Panel US Standard Touch Sensor Wall Switches Voice Control Alexa Google Home However, other people refer to this switch as a MOES Switch so it's very likely that this is a dropship product and multiple brands will have the same internal components.

The voltage range of the device is 100-250V 50/60Hz and a max current of 10A. It connects to WiFi using a 2.4GHz signal.
The interior of the switch
Since it is designed to be mounted on a wall, the glass plate can be easily removed, allowing us to dissasemble it without any tools. The switch is composed of two boards, the main board: Where the TuyaMCU and the WB2S chips are located, and a daughter board, where the relays and power are located.


We are only interested on the main board.
Flashing the WB2S
The main board comes with some pads that link directly to VCC, GND, TX and RX. Here is an example of which ones where on my board. Verify that they are correct in your board, as I've seen other revisions on the internet.

I did some tests and it seems that it is not necessary to remove the TuyaMCU chip from the board for the WB2S however, in case that you're having troubles, desolder pin 1 to disconnect it

Flash the chip following the instructions from OpenBeken in my case, I was only able to do it using python. The flash tool needs to be at 3.3V
Once the chip is flash it, remove the soldered cables, reconnect the TuyaMCU if you had desoldered it and reassemble the box, as it seems that the TuyaMCU does not fully boot unless it is plugged to the daugtherboard (probably the reason we can flash it without desoldering).

OpenBeken configuration
After basic configuration is done (see OpenBeken), a startup file named autoexec.bat with the following contents.
startDriver TuyaMCU
setChannelType 1 toggle
linkTuyaMCUOutputToChannel 1 1 1
setChannelType 2 toggle
linkTuyaMCUOutputToChannel 2 1 2
setChannelType 3 toggle
linkTuyaMCUOutputToChannel 3 1 3
In the App panel, drag the file to the OBK file system:

This file will be automatically executed after every reboot. It needs to be restarted manually the first time
Meaning of individual commands:
- startDriver TuyaMCU - runs the TuyaMCU driver
- setChannelType n toggle - set the type of channel number n to On / Off
- linkTuyaMCUOutputToChannel n bool 1 - connects TuyaMCU dpID number 1 with bool type channel number 1
The dpID values can be different for different kinds of devices. If we want to add support for something new, we need to get to know it, e.g. by listening for packages. The dpID values can also be taken from the descriptions of devices intended for Tasmota, because Tuya often only changes the WiFi module and the rest remains the same.
In the case of this switch there are 4 dpID values (dpID1 trough dpID4) that set the state of each relay, there are four as this chip is used for al versions of switch (from 1 to 4 gang), so only the dpID's for the switches that are physically present are needed.
There is another set of 4 dpID (dpID7 to dpID10) which are of type val which toggle the relay after a number of seconds have passed (the user input's the number) I didn't link them as I don't have any use for them, however I'll document it in case they are needed for your application.
After restarting, the buttons are now visible:

Home Assistant Support
Pairing with Home Assistant is very simple, all thanks to the automatic mapping of OpenBeken channels to TuyaMCU variables. The TuyaMCU switch is operated from the HA level in the same way as an ordinary switch. Here is a sample Yaml code:
switch:
- platform: mqtt
# state_color: true
name: "Test A Switch (1)"
unique_id: "TestB_1"
availability:
- topic: "TestB/connected"
state_topic: "TestB/1/get"
state_on: 1
state_off: 0
command_topic: "TestB/1/set"
payload_on: 1
payload_off: 0
qos: 1
- platform: mqtt
# state_color: true
name: "Test A Switch (2)"
unique_id: "TestB_2"
availability:
- topic: "TestB/connected"
state_topic: "TestB/2/get"
state_on: 1
state_off: 0
command_topic: "TestB/2/set"
payload_on: 1
payload_off: 0
qos: 1
- platform: mqtt
# state_color: true
name: "Test A Switch (3)"
unique_id: "TestB_3"
availability:
- topic: "TestB/connected"
state_topic: "TestB/3/get"
state_on: 1
state_off: 0
command_topic: "TestB/3/set"
payload_on: 1
payload_off: 0
qos: 1
Final test
Comments
In this video ("Final test") you can not see what is happening when controlling from a smartphone. Unless my browser is hiding it. [Read more]
Thank you for inserting the test and I'm glad that TuyaMCU works for you. Basically, I tested TuyaMCU only on the basis of the Moes dimmer, but I can see that the well-implemented protocol also works... [Read more]
There is probably a white backlight of the circle and at least on the second and third individually switched on, you can see that the backlight turns on gently ... But you actually have to strain your... [Read more]
The author should frame the video to the smartphone itself and this gadget from Tuya. It does not matter that it has four T12 bits :) [Read more]