I don't see anything inherently wrong in the code.

So let's start from the beginning:

Make sure you downloaded the newer ASCOM driver from the ASCOM website. There are two.
Follow the instructions on the download page:

Please note

  • You must update your switch to the latest firmware!
  • In the controller web page
    • Set External APIs -> Allow REST-Style API: Enabled
    • Setup -> Access Settings -> Allow legacy plaintext login methods: Enabled

Make sure you have the correct ethernet address and password entered in the ASCOM setup dialog. You can use the ASCOM Profile Explorer to check.

Those are the basics. Again, my switch is the older version and uses the older version ASCOM driver.