---
substitutions:
  device: basement
  name: Basement
  area: Basement
  comment: "${area} | Sockets"

esphome:
  name: mcu-${device}
  friendly_name: ${name}
  area: ${area}
  comment: ${comment}

esp8266:
  board: esp01_1m

packages:
  common: !include common/common_esp8266.yaml

# Sonoff Dual R2
# Pin 	  Function
# GPIO10	Push Button
# GPIO00	Pin "button 0" on expansion header
# GPIO09	Pin "button 1" on expansion header
# GPIO12	Relay #1 and red color on LED
# GPIO05	Relay #2 and green color on LED
# GPIO13	Blue Status LED

globals:
  - id: blower_auto
    type: bool
    restore_value: true
    initial_value: "true"

binary_sensor:
  - platform: gpio
    pin:
      number: 0
      mode: INPUT_PULLUP
      inverted: True
    name: Light Switch
    device_class: light
    on_click:
      - min_length: 50ms
        max_length: 350ms
        then:
          - homeassistant.service:
              service: light.toggle
              data:
                entity_id: light.mcu_basement_2_light
      - min_length: 351ms
        max_length: 1000ms
        then:
          - homeassistant.service:
              service: light.toggle
              data:
                entity_id: light.exercise_room
      - min_length: 1001ms
        max_length: 10000ms
        then:
          # - homeassistant.service:
          #     service: light.turn_off
          #     data:
          #       entity_id: light.basement_light
          - homeassistant.service:
              service: light.turn_off
              data:
                entity_id: light.exercise_room
          - homeassistant.service:
              service: light.turn_off
              data:
                entity_id: light.exercise_room_fan
  - platform: gpio
    pin:
      number: 10
      mode: INPUT_PULLUP
      inverted: true
    name: Socket 1 Switch
    on_press:
      - switch.toggle: relay_2
  - platform: gpio
    pin:
      number: 9
      mode: INPUT_PULLUP
      inverted: true
    name: Door
    device_class: door
    filters:
      invert:
    on_press:
      - homeassistant.service:
          service: light.turn_on
          data:
            entity_id: light.mcu_basement_2_light

select:
  - platform: template
    name: Sewage Treatment Blower
    icon: mdi:hair-dryer-outline
    optimistic: true
    options:
      - "ON"
      - "AUTO"
      - "OFF"
    initial_option: "AUTO"
    restore_value: true
    on_value:
      then:
        - lambda: |-
            switch (i) {
              case 0: {
                id(blower_auto) = false;
                id(relay_1).turn_on();
              } break;
              case 1: {
                id(blower_auto) = true;
              } break;
              case 2:
                id(blower_auto) = false;
                id(relay_1).turn_off();
            }

status_led:
  pin:
    number: 13
    inverted: yes

switch:
  - platform: gpio
    name: Sewage Treatment Blower
    icon: mdi:hair-dryer-outline
    pin: 12
    id: relay_1
    restore_mode: ALWAYS_ON
  - platform: gpio
    name: Socket 1
    icon: mdi:power-socket-fr
    pin: 5
    id: relay_2

time:
  - platform: homeassistant
    timezone: Europe/Bratislava
    on_time:
      - seconds: 0
        minutes: /15
        then:
          - if:
              condition:
                lambda: 'return id(blower_auto);'
              then:
                - switch.turn_on: relay_1
                - delay: 12min
                - switch.turn_off: relay_1
