Custom Component Update States

I need some help.

Ive used a sonoff 4CH to build a custom fan controller, not a fan of the ifan controller, dont need remotes im not going to use.

My custom component updates the relays just fine using this

void write_state(float state) override {

      if (state < 0.3) {

        // OFF

        digitalWrite(5, LOW);
        digitalWrite(4, LOW);
        digitalWrite(15, LOW);

      } else if (state < 0.6) {

        // low speed

        digitalWrite(5, HIGH);

        digitalWrite(4, LOW);

        digitalWrite(15, LOW);

      } else if (state < 0.9) {

        // medium speed

        digitalWrite(5, LOW);

        digitalWrite(4, HIGH);

        digitalWrite(15, LOW);

      } else {

        // high speed

        digitalWrite(5, LOW);

        digitalWrite(4, LOW);

        digitalWrite(15, HIGH);

      }

    }

But once ive turned on the pins.
It seems ESPhome doesnt know their state has changed.
So the relay turns on, but its state in HA is reflected as off still.

HA_fan_relay_state

Is there anyway to fix this?

Why are you using a custom component rather than esphome native code?

But if you do have use lambdas see https://esphome.io/components/switch/index.html#lambda-calls

How would you handle the speed control in native esphome?

HA Fan speed

I think I’ll backtrack and just point to the lambda calls in my 2nd post.