8000
Skip to content

Conversation

@thekeymeister
Copy link
Contributor
@thekeymeister thekeymeister commented Sep 21, 2025

What does this implement/fix?

If an MCU initiated WiFi reset is carried out on some Tuya devices, the MCU will not respond to any commands from ESPHome until it has determined a cloud connection has been established. This also occurs during initial setup of the device. ESPHome does not currently handle WIFI_SELECT or WIFI_RESET.

This will initially ACK the WIFI_RESET/WIFI_SELECT by sending a WIFI_STATE with an empty payload.

It then establishes the requested pairing mode from the received WIFI_RESET/WIFI_SELECT (0x00 = EZ mode, 0x01 = AP mode)

WIFI_STATEs are then sent to simulate the process of establishing a connection to Tuya servers, in the following order.

0x00 (Entered EZ pairing mode) or 0x01 (Entered AP pairing mode)
0x02 (Configured, but not connected)
0x03 (Router connected)
0x04 (Cloud connected)

Upon completion, the MCU is satisfied that a connection is established, and will allow control of the MCU via ESPHome. WIthout this, the device can be stuck and essentially becomes 'listen only'.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

This is an issue that I've personally had for a long time. My 5 year old pressed some buttons on my Zemismart YH002 blind motor which started the WiFi reset procedure, and the device stopped responding to all commands from ESPHome. Others have had similar issues , however the listed fix (initiate WiFi reset) doesn't work for me, it appears later versions of the MCU require a satisfactory response before allowing commands to be executed, and will only report changes in state otherwise.

As well as handling the WiFi reset procedure, this allows for initial setup to be done without ever having to connect to a Tuya server.

Pull request in esphome-docs with documentation (if applicable):

Not applicable

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • nRF52840

Example entry for config.yaml:

Not applicable

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

Tested locally on various Zemismart YH002 devices, I have no other Tuya devices to try. My YH002s have had the WBR3 modules replaced with ESP-12S if that's relevant. These devices only send a WIFI_SELECT rather than a WIFI_RESET, however, the Tuya documentation suggests the required responses are identical.

If user exposed functionality or configuration variables are added/changed:

  • Documentation added/updated in esphome-docs.
    Not applicable

This is the first contribution I've ever made, and first pull request I've ever submitted (long time lurker) - so please go easy on me if I've done something incorrectly!

…E queries

If WiFi reset is carried out on some Tuya devices, they will not respond to any commands from ESPhome until the MCU has determined a connection has established.  ESPhome does not currently handle WIFI_SELECT or WIFI_RESET.

Add ACK for WIFI_SELECT and WIFI_RESET from MCU.

Following a WIFI_SELECT or WIFI_RESET from the MCU, it will send WIFI_STATE to determine the WiFi status.  This will correctly reply in the following way with the relevant delays to simulate an actual connection in progress:-
<300ms  -> 0x00 (pairing/EZ)
<900ms  -> 0x02 (configured, not connected)
<1500ms -> 0x03 (router connected)
>=1500ms-> 0x04 (cloud connected)

If a subsequent WIFI_STATE is received, it will only reply with 0x04 (cloud connected)
Remembers when we last received WIFI_SELECT so we can "ramp" WIFI_STATE replies.
…TATE

Creates wifi_select_seen_ which is set true upon receiving WIFI_SELECT after MCU initiates WiFi reset procedure.  Once correct response is sent to WIFI_STATE implying connection to Tuya servers is successful, wifi_select_seen_ is set false.
This reverts commit 19366a6.
If an MCU initiated WiFi reset is carried out on some Tuya devices, they will not respond to any commands from ESPhome until the MCU has determined a connection has established.  This also occurs during initial setup of the device.  ESPhome does not currently handle WIFI_SELECT or WIFI_RESET.

This will initially ACK the WIFI_RESET/WIFI_SELECT by sending a WIFI_STATE with an empty payload.

It then establishes the pairing mode from the received WIFI_RESET/WIFI_SELECT (0x00 = EZ mode, 0x01 = AP mode)

WIFI_STATEs are then sent to simulate the process of establishing a connection to Tuya servers, in the following order.

0x00 (Entered EZ pairing mode) or 0x01 (Entered AP pairing mode)
0x02 (Configured, but not connected)
0x03 (Router connected)
0x04 (Cloud connected)

Upon completion, the MCU is satisfied that a connection is established, and will allow control of the MCU via esphome.  WIthout this, the device can be stuck and essentially becomes 'listen only'.
If an MCU initiated WiFi reset is carried out on some Tuya devices, they will not respond to any commands from ESPhome until the MCU has determined a connection has established.  This also occurs during initial setup of the device.  ESPhome does not currently handle WIFI_SELECT or WIFI_RESET.

This will initially ACK the WIFI_RESET/WIFI_SELECT by sending a WIFI_STATE with an empty payload.

It then establishes the pairing mode from the received WIFI_RESET/WIFI_SELECT (0x00 = EZ mode, 0x01 = AP mode)

WIFI_STATEs are then sent to simulate the process of establishing a connection to Tuya servers, in the following order.

0x00 (Entered EZ pairing mode) or 0x01 (Entered AP pairing mode)
0x02 (Configured, but not connected)
0x03 (Router connected)
0x04 (Cloud connected)

Upon completion, the MCU is satisfied that a connection is established, and will allow control of the MCU via esphome.  WIthout this, the device can be stuck and essentially becomes 'listen only'.
If an MCU initiated WiFi reset is carried out on some Tuya devices, they will not respond to any commands from ESPhome until the MCU has determined a connection has established.  This also occurs during initial setup of the device.  ESPhome does not currently handle WIFI_SELECT or WIFI_RESET.

This will initially ACK the WIFI_RESET/WIFI_SELECT by sending a WIFI_STATE with an empty payload.

It then establishes the pairing mode from the received WIFI_RESET/WIFI_SELECT (0x00 = EZ mode, 0x01 = AP mode)

WIFI_STATEs are then sent to simulate the process of establishing a connection to Tuya servers, in the following order.

0x00 (Entered EZ pairing mode) or 0x01 (Entered AP pairing mode)
0x02 (Configured, but not connected)
0x03 (Router connected)
0x04 (Cloud connected)

Upon completion, the MCU is satisfied that a connection is established, and will allow control of the MCU via esphome.  WIthout this, the device can be stuck and essentially becomes 'listen only'.
@github-actions
Copy link
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#10822
    components: [tuya]
    refresh: 1h

(Added by the PR bot)

@codecov-commenter
Copy link
codecov-commenter commented Sep 21, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.76%. Comparing base (4729bc8) to head (cd6479c).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev   #10822   +/-   ##
=======================================
  Coverage   68.76%   68.76%           
=======================================
  Files          52       52           
  Lines       10689    10689           
  Branches     1437     1437           
=======================================
  Hits         7350     7350           
  Misses       2958     2958           
  Partials      381      381           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jesserockz jesserockz merged commit 5c943d7 into esphome:dev Sep 22, 2025
34 of 35 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0