#############################################
#############################################
# Mi Smart LED Desk Lamp 1S
# ESP32, LED Strip with variable white colour temp, rotary encoder
# https://www.mi.com/global/product/mi-led-desk-lamp-1s/
#############################################
#############################################

#############################################
# SECRETS
# Secrets file is usually at config/secrets.yaml
# or config/esphome/secrets.yaml
# depending on your setup with HA
#############################################
# SECRETS FILE needs these specific secrets:
# esp-midesklamp1s_api_key
# esp-midesklamp1s_ota_pass
# esp-midesklamp1s_static_ip (Optional)
# esp-midesklamp1s_gateway (Optional)
# esp-midesklamp1s_subnet (Optional)
#
# SECRETS FILE needs these generic Secrets:
# wifi_ssid
# wifi_password
# fallback_ap_password
# mqtt_server
# mqtt_username
# mqtt_password
# web_server_username (Optional)
# web_server_password (Optional)
#############################################


#############################################
# SPECIFIC DEVICE VARIABLE SUBSTITUTIONS
# If NOT using a secrets file, just replace 
# these with the passwords etc (in quotes)
#############################################
substitutions:
  device_name: esp-midesklamp1s
  friendly_name: "Mi 1S Lamp Office"
  description_comment: "Office Desk Lamp with variable white colour temp and rotary encoder for brightness"
  api_key: !secret esp-midesklamp1s_api_key # unfortunately you can't use substitutions in secrets names
  ota_pass: !secret esp-midesklamp1s_ota_pass # unfortunately you can't use substitutions in secrets names
  static_ip_address: !secret esp-midesklamp1s_ip
  log_level: "INFO" # Define logging level: NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
  update_interval: "60s" # update time for for general sensors etc
  room: "Office"  # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.  

##########################################################################################
# PACKAGES: Included Common Packages
# https://esphome.io/components/packages.html
##########################################################################################
packages:
  #### WIFI, Network (Static/DHCP/IPV6 etc), Fallback AP, Safemode ####
  common_wifi: !include
    file: common/network_common.yaml
    vars:
      local_device_name: "${device_name}"
      local_static_ip_address: "${static_ip_address}"
      local_ota_pass: "${ota_pass}"

  #### HOME ASSISTANT API (choose encryption or no encryption options) ####
  common_api: !include
    file: common/api_common.yaml
    #file: common/api_common_noencryption.yaml
    vars:
      local_api_key: "${api_key}"

  #### MQTT ####
  common_mqtt: !include
    file: common/mqtt_common.yaml
    vars:
      local_device_name: "${device_name}"
  
  #### WEB PORTAL ####
  #common_webportal: !include common/webportal_common.yaml
  #### SNTP (Only use if you want/need accurate timeclocks) ####
  common_sntp:      !include common/sntp_common.yaml

  #### DIAGNOSTICS Sensors ####
  diag_basic:       !include common/include_basic_diag_sensors.yaml
  diag_more:        !include common/include_more_diag_sensors.yaml
  diag_debug:       !include common/include_debug_diag_sensors.yaml
  #diag_resetcount:  !include common/include_resetcount_diag_sensors.yaml

#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
  name: ${device_name}
  friendly_name: ${friendly_name}
  comment: ${description_comment}
  area: ${room}
  #platformio_options:
  #  platform: espressif32@6.6.0
  #  platform_packages:
  #    - framework-arduinoespressif32@2.0.17

#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp32:
  board: esp32doit-devkit-v1
  framework:
    type: arduino
    version: recommended

    # these just for the esp32doit-devkit-v1 and Mi S Lamp 
    #sdkconfig_options:
    #  CONFIG_FREERTOS_UNICORE: y
    #advanced:
    #  ignore_efuse_mac_crc: true
    #  ignore_efuse_custom_mac: true

#############################################    
# ESPHome Logging Enabl
# https://esphome.io/components/logger.html
############################################# 
logger:
  level: ${log_level}  # INFO Level suggested, or DEBUG for testing
  #baud_rate: 0 #set to 0 for no logging via UART, needed if you are using it for other serial things (eg PZEM)
  #esp8266_store_log_strings_in_flash: false
  #tx_buffer_size: 64

#############################################
# Time Component
# https://esphome.io/components/time/index.html#time-component
# Sync with HA Time.  Probably not really necessary.
#############################################
#time:
#  - platform: homeassistant
#    id: homeassistant_time

#############################################
# General Sensors 
# https://esphome.io/components/sensor/index.html
#############################################
sensor:
  # Mi Desk Lamp 1S Configuration
  - platform: rotary_encoder
    id: rotation
    pin_a: GPIO27
    pin_b: GPIO26
    resolution: 2
    on_value:
      then:
        - if:
            condition:
              # Check if Button is pressed while rotating
              lambda: "return id(pushbutton).state;"
            then:
              # If Button is pressed, change CW/WW
              - lambda: |-
                  auto min_temp = id(light1).get_traits().get_min_mireds();
                  auto max_temp = id(light1).get_traits().get_max_mireds();
                  auto cur_temp = id(light1).current_values.get_color_temperature();
                  auto new_temp = max(min_temp, min(max_temp, cur_temp + (x*10)));
                  auto call = id(light1).turn_on();
                  call.set_color_temperature(new_temp);
                  call.perform();
            else:
              # If Button is not pressed, change brightness
              - light.dim_relative:
                  id: light1
                  relative_brightness: !lambda |-
                    return x / 25.0;
        # Reset Rotation to 0
        - sensor.rotary_encoder.set_value:
            id: rotation
            value: 0

#############################################
# Binary Sensors
# https://esphome.io/components/binary_sensor/index.html
#############################################
binary_sensor:
  - platform: gpio
    id: pushbutton
    pin:
      number: GPIO33
      inverted: True
      mode: INPUT_PULLDOWN
    on_click:
      then:
        - light.toggle:
            id: light1
            transition_length: 0.2s

#############################################
# Base Output Component
# https://esphome.io/components/output/index.html#base-output-configuration
#############################################
output:
  - platform: ledc    # Cool White LED PWM
    pin: GPIO2
    id: output_cw
    frequency: 40000Hz
    power_supply: power
  - platform: ledc
    pin: GPIO4        # Warm White LED PWM
    id: output_ww
    power_supply: power
    frequency: 40000Hz

#############################################
# Power Supply Component
# https://esphome.io/components/power_supply.html#power-supply-component
#############################################
power_supply:
  - id: power
    pin: GPIO12
    enable_time: 0s
    keep_on_time: 0s

#############################################
# Light Component
# https://esphome.io/components/light/index.html#light-component
#############################################
light:
  - platform: cwww # https://esphome.io/components/light/cwww.html#cold-white-warm-white-light
    id: light1
    default_transition_length: 0s
    constant_brightness: true
    name: "${friendly_name} Light"
    cold_white: output_cw
    warm_white: output_ww
    cold_white_color_temperature: 4800 K
    warm_white_color_temperature: 2500 K # 2500k is the original value of the lamp. To correct binning for 2700k to look more like 2700k use 2650k instead
    restore_mode: ALWAYS_OFF
    gamma_correct: 0




#globals:
#  - id: fade_brightness
#    type: float
#    initial_value: '0.0'
#  - id: fade_step
#    type: float
#    initial_value: '0.0'


# This script fades the light ON to full brightness over 20 seconds.
#script:
#  - id: fade_on
#    then:
#      # First, capture the current brightness.
#      - lambda: |-
#          // If the light is off, current brightness will be 0.0.
#          float current = id(light1).current_values.get_brightness();
#          id(fade_brightness) = current;
#          // Compute the step size so that after 100 steps (200ms each) we reach full brightness.
#          id(fade_step) = (1.0 - current) / 100.0;
#      - repeat:
#          count: 100
#          then:
#            - lambda: |-
#                id(fade_brightness) += id(fade_step);
#                if (id(fade_brightness) > 1.0) {
#                  id(fade_brightness) = 1.0;
#                }
#                // Update the light’s brightness.
#                id(light1).turn_on({ brightness: id(fade_brightness) });
#            - delay: 200ms

# This script fades the light OFF over 20 seconds.
#  - id: fade_off
#    then:
#      - lambda: |-
#          // Capture current brightness (if the light is on).
#          float current = id(light1).current_values.get_brightness();
#          id(fade_brightness) = current;
#          // Compute step size so that after 100 steps we reach 0.
#          id(fade_step) = current / 100.0;
#      - repeat:
#          count: 100
#          then:
#            - lambda: |-
#                id(fade_brightness) -= id(fade_step);
#                if (id(fade_brightness) < 0.0) {
#                  id(fade_brightness) = 0.0;
#                }
#                id(light1).turn_on({ brightness: id(fade_brightness) });
#            - delay: 200ms
#      # Finally, ensure the light is turned off.
#      - lambda: |-
#          id(light1).turn_off();

#switch:
#  - platform: template
#    name: "Fade on and off"
#    id: virtual_switch
#    turn_on_action:
#      - script.execute: fade_on
#    turn_off_action:
#      - script.execute: fade_off