esphome: name: bedlight platform: ESP8266 board: esp01_1m #esp8266_restore_from_flash: true # light default values on_boot: then: - lock.template.publish: id: child_lock state: UNLOCKED # light default values #- light.control: # id: bed # state: on # brightness: 10% # color_temperature: 330.0 mireds # Enable logging logger: # make RXD / TXD available baud_rate: 0 # Enable Home Assistant API api: encryption: key: !secret api_encryption_key # OTA ota: password: !secret ota_password # WiFi Credentials wifi: domain: .lan ssid: !secret wifi_ssid password: !secret wifi_password # Enable fallback hotspot (captive portal) in case wifi connection fails ap: ssid: "Bedlight Fallback Hotspot" password: !secret wifi_ap_password captive_portal: light: - id: bed platform: cwww name: "Bed" cold_white: pwm_w_t warm_white: pwm_b_t cold_white_color_temperature: 6536 K warm_white_color_temperature: 2000 K #restore_mode: RESTORE_AND_OFF restore_mode: RESTORE_DEFAULT_OFF output: - id: gpio_r platform: gpio pin: GPIO12 - id: gpio_g platform: gpio pin: GPIO15 - id: pwm_b platform: esp8266_pwm pin: GPIO14 - id: pwm_b_t platform: template type: float write_action: - output.set_level: id: pwm_b level: !lambda |- // limit min power if (state > 0.0 && state < 0.0006) return 0.0006; if (state <= 0.0002) return 0.0002; return state; - id: pwm_w platform: esp8266_pwm pin: GPIO4 - id: pwm_w_t platform: template type: float write_action: - output.set_level: id: pwm_w level: !lambda |- // limit min power if (state > 0.0 && state < 0.0006) return 0.0006; if (state <= 0.0002) return 0.0002; return state; - id: gpio0 platform: gpio pin: GPIO0 - id: gpio_txd platform: gpio pin: GPIO1 - id: gpio_rxd platform: gpio pin: GPIO3 globals: - id: g_dim_level type: float initial_value: '0.02' - id: g_min_dim_level type: float initial_value: '0.02' script: - id: reset_dim_direction mode: restart then: - delay: 10s - lambda: 'id(g_dim_level) = abs(id(g_dim_level));' - id: reset_child_lock mode: restart then: - delay: 30min - lock.template.publish: id: child_lock state: UNLOCKED binary_sensor: - id: user_button platform: gpio pin: GPIO5 filters: - delayed_on: 30ms on_click: min_length: 30ms max_length: 350ms then: - if: condition: lock.is_unlocked: child_lock then: - logger.log: "on_click" # TODO: if brightness is low, turn on + transition is also slow - light.toggle: bed # dim up per default unless brightness is 95% - lambda: |- id(g_dim_level) = abs(id(g_dim_level)); if (id(bed).current_values.get_brightness() >= 0.95) id(g_dim_level) *= -1; on_press: then: - delay: 500ms - if: condition: and: - lock.is_unlocked: child_lock # make sure button is still pressed - binary_sensor.is_on: user_button then: - while: condition: and: - lock.is_unlocked: child_lock - binary_sensor.is_on: user_button then: - logger.log: "on_press" - light.dim_relative: id: bed relative_brightness: !lambda |- auto brightness = id(bed).current_values.get_brightness(); // start dimming at this value if (!id(bed).current_values.is_on()) return abs(id(g_min_dim_level)); // don't dim to zero if (id(g_dim_level) < 0 && brightness + id(g_dim_level) <= id(g_min_dim_level)) return 0.0; return id(g_dim_level); transition_length: 100ms - delay: 0.1s # reverse dim direction - lambda: 'id(g_dim_level) = id(g_dim_level) * -1;' - script.execute: reset_dim_direction on_double_click: then: - if: condition: lock.is_unlocked: child_lock then: - homeassistant.event: event: esphome.double_click lock: - id: child_lock platform: template name: "Child Lock" optimistic: true on_lock: - script.execute: reset_child_lock on_unlock: - script.stop: reset_child_lock