How change a entity status from C code in a custom component

Following this Blog and ESPHome tutorial I’m able to read entities value
folowing this suggestions
and pass values from Custom component reading sensors through this
tutorial

Now I have some toggles (input.boolean) in my dash board.
How to read the state value “on / off” and
how to set in “switched-on” and “switched-off” from my C code?
I tried

id(toggle_1).publish_state(false);
id(toggle_1).publish_state(true);

and for state reading

x = id(toggle_1).state;

but it doesn’t work
in yaml i defined

sensor:
- platform: homeassistant
  name: "toggle_1"
  entity_id: input_boolean.toggle_1
  id: toggle_1

but nothing
How to solve it?

Thanks @nickrout : Ok: “binary sensor” instead of sensor, but even if the state true/false is associated to “state” the toggle icon in the dash board deons’t change. That actually my true issue. How to manage it?
image

I would start with log on ESP Home dashboard. When you make a state change in HA does it show in the live log as being imported to the Binary Sensor? I am just setting mine up. I can see it in my logs instantly when I trigger a HA switch. I am now about to start on my automation

The LOG works fine
image

image

     ESP_LOGI(TAG_ME, "----toggle:[%d]\n",id(me_start).state);
      if (id(me_start).state==1) {
        id(me_start).publish_state(0);
      } else {
        id(me_start).publish_state(1);
      }

However the visual toggle on dash board doesn’t change. The toggling demo period is 1s.
So it is not a problem of delay responsivity
Maybe publish_state is not enough to change the visual state?

Hi Edge76, did you get this to work?
If so how did you solve it?

So far, not

You cannot change a ha entity’s state by importing it into esphome as a sensor or binary sensor. You can only see it.

However you can change by generating a home assistant service call from within esphome