Unable to flash ESPHome to Sonoff M5

Hey, I didn’t expect I would come back here, but I just found the solution, after trying to flash another sonoff device, having similar issues, I decided to try something looking really dangerous… I tried to flash the device with 230v ac power connected, and guess what - it worked!

I’m still using the same FTDI232, output set to 3.3v, using following procedure:

  1. Connect the FTDI cables (VCC, RX, TX, GND) but don’t connect it to the PC USB yet
  2. With ac power turned off, connect the 230v Lin and N cables
  3. Press and hold the button (my switch is 1-gang, so the gpio00 is connected to the button)
  4. Turn on AC power. You should see the blue led turned off, meaning the device is not attempting to connect to Wifi, but rather waits for the programming.
  5. After a moment, release the button.
  6. Plug in the USB cable into the PC
  7. In Home Assistant ESPHome tab, create new device and connect. Of course you need to have the usb/serial driver installed. I’m using this one: https://ftdichip.com/drivers/
    It should connect, then takes a moment creating config, programming and the device goes online with no functionality.
  8. Power off the device, disconnect the FTDI, assemble and seal the cover.
  9. Power on, the device should become online in a moment in ESPHome.
  10. Update yaml config and install it using wireless update. Done.

BTW, if you’re stuck with the yaml config, I’m copying mine below.
The one that google finds here is invalid.
Here you’ll find the ESPHome reference, but they only provide ready config for 2- and 3-gang, while the 1-gang is slightly different.
Here’s mine:

substitutions:
  friendly_name: Switch M5
  device_name: sonoff-switch-m5

esphome:
  name: ${device_name}
  friendly_name: ${friendly_name}

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

api:
  encryption:
    key: "xxx"

ota:
  - platform: esphome
    password: "xxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  on_connect:
    - light.turn_on: wifi_status_led
  on_disconnect:
    - light.turn_off: wifi_status_led

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${device_name}
    password: !secret wifi_failover_password

captive_portal:

# ---------

text_sensor:
  - platform: wifi_info
    ip_address:
      id: ip_address
      name: "IP Address"
      icon: "mdi:wan"

switch:
  - platform: gpio
    name: "Switch"
    pin: GPIO23
    id: relay_1
    on_turn_on:
      - output.turn_on: led_1
    on_turn_off:
      - output.turn_off: led_1
    restore_mode: RESTORE_DEFAULT_OFF

output:
  - platform: gpio
    id: led_1
    pin:
      number: GPIO19
      inverted: False

  - platform: ledc
    id: pwm_output
    pin: GPIO18
    frequency: 1000 Hz

light:
  - platform: status_led
    id: wifi_status_led
    internal: True
    pin:
      number: GPIO05
      inverted: True

binary_sensor:
  - platform: gpio
    name: "Button"
    pin:
      number: GPIO00
      mode: INPUT_PULLUP
      inverted: False
    on_press:
      - switch.toggle: relay_1