---
substitutions:
  devicename: nodemcu-13
  id_name: nodemcu_13
  Device_Location: Shelf Lights
  update_interval: !secret d1-update_interval
  ipaddress: 10.0.0.23
  unique_id: "nodemcu-13"
  deep_sleep_interval: !secret deep_sleep_interval

logger:
  level: WARN

esp8266:
  board: nodemcuv2

wifi:
  ssid: !secret mogwai_wifi_name
  password: !secret mogwai_wifi_password
  <<: !include common/network.yaml

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "$devicename Fallback Hotspot"
    password: !secret mogwai_ap_key

api:
  encryption:
    key: !secret mogwai_api_key

ota:
  platform: esphome
  password: !secret mogwai_ota_key

captive_portal:

esphome:
  name: $devicename
  friendly_name: "NodeMCU-13 - Shelf Lights"
  on_boot:
    then:
      - light.turn_on: puter_lights
      - light.control:
          id: puter_lights
          effect: Breathing

switch:

  - <<: !include common/nodemcu-led-onboard1.yaml

  - <<: !include common/nodemcu-led-onboard2.yaml

  - <<: !include common/restart.yaml

  - platform: template
    name: "00 - Leg Shuffle"
    optimistic: True
    id: redtemp
    turn_on_action:
      - while:
          condition:
            lambda: 'return true;'
          then:
            - switch.turn_on: led_onboard1
            - switch.turn_off: led_onboard2
            - delay: 500ms
            - switch.turn_off: led_onboard1
            - switch.turn_on: led_onboard2
            - delay: 500ms
    turn_off_action:
      - switch.turn_off: led_onboard1
      - switch.turn_off: led_onboard2

interval:

  - <<: !include common/version-interval.yaml

  - <<: !include common/whatami.yaml

<<: !include common/external-heapmon.yaml

sensor:

  - <<: !include common/wifi2.yaml

  - <<: !include common/uptime.yaml

  - <<: !include common/free_space.yaml

  - platform: template
    name: "Random Numbers"
    id: random_sensor
    unit_of_measurement: "%"
    lambda: |-
      return rand() % 100;
    update_interval: !secret d1-update_interval

text_sensor:

  - <<: !include common/version.yaml

mqtt:
  id: mqtt_client
  client_id: $devicename
  broker: 10.0.1.50
  discovery_object_id_generator: device_name
  discovery_retain: False
  discovery: False
  topic_prefix: /sensors/$devicename
  on_message:
    - topic: /sensors/$devicename/ota_mode
      payload: 'ONB'
      then:
        - switch.turn_on: led_onboard1

    - topic: /sensors/$devicename/ota_mode
      payload: 'OFF'
      then:
        - switch.turn_off: led_onboard1

    - topic: /sensors/all-sensors/ota
      payload: 'OFF'
      then:
        - switch.turn_off: led_onboard1

    - topic: /sensors/all-sensors/ota
      payload: 'ONB'
      then:
        - switch.turn_on: led_onboard1

    - topic: /sensors/$devicename/ota_mode
      payload: 'SHELF'
      then:
        - light.turn_on: puter_lights
        - light.control:
            id: puter_lights
            effect: Breathing

    - topic: /sensors/$devicename/ota_mode
      payload: 'SHELF-COMPUTER'
      then:
        - light.turn_on: puter_lights
        - light.control:
            id: puter_lights
            effect: Strobe Effect With Custom Values

    - topic: /sensors/$devicename/ota_mode
      payload: 'SHELF-OFF'
      then:
        - light.turn_off: puter_lights

<<: !include common/time-sntp.yaml

<<: !include common/website.yaml

<<: !include common/wifi1.yaml

<<: !include common/uptime_formatted.yaml

light:
  - platform: rgb
    name: "Kitchen_Light"
    state_topic: '/light/led'
    command_topic: '/light/led/set'
    red: red_output1
    blue: blue_output1
    green: green_output1
    id: puter_lights
    on_turn_on:
      then:
        - light.control:
            id: puter_lights
            effect: Breathing
    effects:
      - lambda:
          name: My Custom Effect
          update_interval: 1s
          lambda: |-
            static int state = 0;
            auto call = id(puter_lights).turn_on();
            // Transition of 1000ms = 1s
            call.set_transition_length(rand() % 1000);
            if (state == 0) {
            call.set_rgb(1.0, 1.0, 1.0);
            } else if (state == 1) {
              call.set_rgb(1.0, 0.0, 1.0);
            } else if (state == 2) {
              call.set_rgb(0.0, 0.0, 1.0);
            } else {
              call.set_rgb(1.0, 0.0, 0.0);
            }
            call.perform();
            state += 1;
            if (state == 4)
              state = 0;

      - lambda:
          name: Breathing
          update_interval: 18s
          lambda: |-

            static int num_executions = 0;
            id(mqtt_client).publish("/light/num_executions",esphome::to_string(num_executions));
            num_executions += 1;

            static int value_state = 0;
            static int color = 1;
            auto call = id(puter_lights).turn_on(); //the id of your light
            static int random_length = 0;
            random_length = (rand() % 15000);
            id(mqtt_client).publish("/light/random_length",esphome::to_string(random_length));
            call.set_transition_length(rand() % 15000);
            if (value_state == 0)
            {
             call.set_brightness(1.0);
            }
             else if (value_state == 1)
            {
             call.set_brightness(0.01);
            }
             call.perform();
             value_state ++;
             if (value_state == 2){
             value_state = 0;
             }

             id(mqtt_client).publish("/light/random",esphome::to_string(value_state));

      - lambda:
          name: RGB Breathing
          update_interval: 16s
          lambda: |-

            #define Color1 0.8, 0.0, 0.4 //These are the colors defined, feel free to change or extend the list
            #define Color2 0.8, 0.0, 0.4 //if you extend the list, dont forget to add them in the switch loop below
            static int state = 0;
            static int color = 1;
            auto call = id(puter_lights).turn_on(); //put the id for your light in here
            call.set_transition_length(15000);
            if (state == 0)
            {
             call.set_brightness(0.01);

            }
             else if (state == 1)
            {
               switch(color)
               {
                 case 1: call.set_rgb(Color1);
                         break;
                 case 2: call.set_rgb(Color2);
                         break;
               }
              call.set_brightness(1.0);
            }

             state ++;
             if (state == 2){
             state = 0;
             }
             color++;
             if(color == 7)
             {
               color = 1;
             }
             call.perform();

      - random:
          name: "My Slow Random Effect"
          transition_length: 30s
          update_interval: 30s

      - random:
      - random:
          name: "My Fast Random Effect"
          transition_length: 3s
          update_interval: 3s

      - strobe:
      - strobe:
          name: Strobe Effect With Custom Values
          colors:
            - state: True
              color_brightness: 100%
              red: 100%
              green: 0%
              blue: 0%
              duration: 500ms
            - state: False
              duration: 250ms

            - state: True
              color_brightness: 100%
              red: 0%
              green: 100%
              blue: 0%
              duration: 500ms
            - state: False
              duration: 250ms

            - state: True
              color_brightness: 100%
              red: 0%
              green: 0%
              blue: 100%
              duration: 500ms
            - state: False
              duration: 250ms

# Example configuration entry
output:

  - platform: esp8266_pwm
    pin: D1
    frequency: 1000 Hz
    id: red_output1
  - platform: esp8266_pwm
    pin: D2
    frequency: 1000 Hz
    id: blue_output1
  - platform: esp8266_pwm
    pin: D5
    frequency: 1000 Hz
    id: green_output1