summaryrefslogtreecommitdiffstats
path: root/esphome/config/bedlight.yaml
blob: f34f0214a08e62b1ff64a812edde69a3b24e2805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
esphome:
  name: bedlight
  platform: ESP8266
  board: esp01_1m
  #esp8266_restore_from_flash: true

  # light default values
  #on_boot:
  #  then:
  #    - 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));'

binary_sensor:
  - id: user_button
    platform: gpio
    pin: GPIO5
    filters:
      - delayed_on: 30ms
    on_click:
      min_length: 30ms
      max_length: 350ms
      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:
              # make sure button is still pressed
              binary_sensor.is_on: user_button
            then:
              - while:
                  condition:
                    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:
        - homeassistant.event:
            event: esphome.double_click