esphome:
  on_boot:
    priority: -200
    then:
      - wait_until:
          wifi.connected:
      - lambda: |-
          id(fan_power_state).execute();

switch:
  - platform: template
    id: power_fan
    name: "Fan: Power"
    optimistic: true
    turn_on_action:
      - http_request.post: http://${hostname_fan}/switch/power/turn_on
    turn_off_action:
      - http_request.post: http://${hostname_fan}/switch/power/turn_off

interval:
  - interval: 1min
    then:
      - script.execute: fan_power_state

script:
  - id: fan_power_state
    then: 
      - http_request.get:
          url: "http://${hostname_fan}/switch/power"
          headers:
            Content-Type: application/json
          capture_response: true
          on_response:
            then:
              - lambda: |-
                  json::parse_json(body, [](JsonObject root) -> bool {
                    id(power_fan).publish_state(root["value"]);
                    return true;
                  });

prometheus:
  relabel:
    power_fan:
      id: power_fan
      name: "Power Fan"
