############################################
#############################################
# ATHOM 15W RGBWW Bulb Flasher
#
# # V1.0 2025-02-15 Initial Version
#
# Device https://www.athom.tech/blank-1/15w-color-bulb
# 
# DESCRIPTION
# Starts up and flashes a RGBWW lightbulb a white colour
# On and off time (duty cycle) is configurable
# Colour Temp (in K) is configurable
# There is no fading between transitions
#
#############################################
#############################################


#############################################
# USER VARIABLE SUBSTITUTIONS
# Give the device a useful name & description here
# and change values accordingly.
#############################################
substitutions:
  devicename: esp-athombulbflasher
  friendly_name: "Flasher for Athom 15W Bulb"
  description_comment: "Athom RGBWW light bulb that will flash on boot and allow colour temp & duty cycle adjustment"

  # Adjust the color temperature here (200 mired is approx 5000k
  white_temp: "3000K"

  # Adjust the ON/OFF times for flashing.
  # 600ms on, 400ms off => 60% ON, 40% OFF duty cycle
  on_time_ms: "500ms"
  off_time_ms: "500ms"


#############################################
# OTHER VARIABLE SUBSTITUTIONS
# Give the device a useful name & description here
# and change values accordingly.
#############################################

   # If NOT using a secrets file, just replace these with the passwords etc (in quotes)
  api_key: !secret esp-athombulbflasher_api_key # unfortunately you can't use substitutions inside secrets names
  ota_pass: !secret esp-athombulbflasher_ota_pass # unfortunately you can't use substitutions inside secrets names
  wifi_ssid: !secret wifi_ssid
  wifi_password: !secret wifi_password
  fallback_ap_password: !secret fallback_ap_password
  
  # Add these if we are giving it a static ip, or remove them in the Wifi section
  #static_ip_address: !secret esp-athombulbflasher_static_ip
  #static_ip_gateway: !secret esp-athombulbflasher_gateway
  #static_ip_subnet: !secret esp-athombulbflasher_subnet

  #mqtt_server: !secret mqtt_server
  #mqtt_username: !secret mqtt_username
  #mqtt_password: !secret mqtt_password
  #mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like

  # Add these if we are using the internal web server (this is pretty processor intensive)
  #web_server_username: !secret web_server_username
  #web_server_password: !secret web_server_password

  #update_interval: 60s # update time for for general sensors etc


#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esphome:
  name: ${devicename}
  friendly_name: ${friendly_name}
  comment: ${description_comment} # appears on the esphome page in HA
  on_boot:
    priority: 800
    then:
      - light.turn_on:
          id: my_light
          effect: "Flashing White"

#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html
#############################################
esp8266:
  board: esp01_1m 

#############################################    
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
############################################# 
logger:
  level: INFO  # 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

#############################################    
# Enable the Home Assistant API
# https://esphome.io/components/api.html
#############################################
api:
  encryption:
    key: ${api_key}

#############################################    
# Enable Over the Air Update Capability
# https://esphome.io/components/ota.html?highlight=ota
#############################################  
ota:
  - platform: esphome
    password: ${ota_pass}

#############################################    
# Safe Mode
# Safe mode will detect boot loops
# https://esphome.io/components/safe_mode
#############################################  
safe_mode:

#############################################    
# Wifi Settings 
# https://esphome.io/components/wifi.html
#
# Power Save mode (can reduce wifi reliability)
# NONE (least power saving, Default for ESP8266)
# LIGHT (Default for ESP32)
# HIGH (most power saving)
#############################################  
wifi:
  ssid: ${wifi_ssid}
  password: ${wifi_password}
  #power_save_mode: LIGHT   # https://esphome.io/components/wifi.html#wifi-power-save-mode
  #manual_ip:               # optional static IP address
    #static_ip: ${static_ip_address}
    #gateway: ${static_ip_gateway}
    #subnet: ${static_ip_subnet}
  ap:   # Details for fallback hotspot in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
    ssid: ${devicename} AP
    password: ${fallback_ap_password}
    ap_timeout: 30min # Time until it brings up fallback AP. default is 1min
  min_auth_mode: WPA2

captive_portal:  # extra fallback mechanism for when connecting if the configured WiFi fails
  

#############################################
# MQTT Monitoring
# https://esphome.io/components/mqtt.html?highlight=mqtt
# MUST also have api enabled if you enable MQTT
#############################################
#mqtt:
  #broker: ${mqtt_server}
  #topic_prefix: ${mqtt_topic}/${devicename}
  #username: ${mqtt_username}
  #password: ${mqtt_password}
  ##discovery: True # enable entity discovery (true is default)
  ##discover_ip: True # enable device discovery (true is default)


#############################################    
# Web Portal for display and monitoring
# Turning this off is probably a good idea to save resources.
# https://esphome.io/components/web_server.html
#############################################   
#web_server:
#  port: 80
#  auth:
#   username: ${web_server_username} # probably a good idea to secure it
#   password: ${web_server_password}



# ---------------------------------------------------------
# OUTPUTS: 5 x PWM channels for the Athom 15W RGBWW bulb
# ---------------------------------------------------------
output:
  - platform: esp8266_pwm
    id: output_red
    pin: GPIO4
    frequency: 1000 Hz

  - platform: esp8266_pwm
    id: output_green
    pin: GPIO5
    frequency: 1000 Hz

  - platform: esp8266_pwm
    id: output_blue
    pin: GPIO12
    frequency: 1000 Hz

  # Typically one channel is cold white...
  - platform: esp8266_pwm
    id: output_cold_white
    pin: GPIO14
    frequency: 1000 Hz

  # ...and one channel is warm white (inverted on GPIO13).
  - platform: esp8266_pwm
    id: output_warm_white
    pin: GPIO13
    frequency: 1000 Hz
    inverted: true

# ---------------------------------------------------------
# LIGHT COMPONENT: RGBWW + a strobe effect for flashing
# ---------------------------------------------------------
light:
  - platform: rgbww
    name: ${friendly_name}
    id: my_light
    red: output_red
    green: output_green
    blue: output_blue
    cold_white: output_cold_white
    warm_white: output_warm_white
    
    # Typical white temperature mapping for RGBWW
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K

    # Ensure it always powers on (and with our desired effect).
    restore_mode: ALWAYS_ON
    
    # You can omit this or set it to zero for no fade on effect changes.
    default_transition_length: 0s

    # The custom strobe effect for flashing white
    effects:
      - strobe:
          name: "Flashing White"
          colors:
            # Note the Kelvin value is replaced at compile time:
            - state: True
              brightness: 100%
              color_temperature: "${white_temp}"
              duration: ${on_time_ms}
            - state: False
              brightness: 0%
              color_temperature: "${white_temp}"
              duration: ${off_time_ms}
              
