summaryrefslogtreecommitdiffstats
path: root/esphome/config/bedlight.yaml
blob: ac83f5d18ac2454814d3fc075ccc0b4c4e89b754 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
substitutions:
  device_name: bedlight
  friendly_name: "Bed Light"

esphome:
  name: ${device_name}
  friendly_name: ${friendly_name}
  area: Bed Room

  # 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

esp8266:
  board: esp01_1m
  restore_from_flash: true

preferences:
  flash_write_interval: 5min

# Enable logging
logger:
  # make RXD / TXD available
  baud_rate: 0

# Enable Home Assistant API
api:
  encryption:
    key: !secret api_encryption_key

# OTA
ota:
  - platform: esphome
    password: !secret ota_password

# WiFi Credentials
wifi:
  domain: .lan
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  # see https://github.com/esphome/issues/issues/1532
  power_save_mode: HIGH

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name} 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

  - platform: gpio
    id: unused_gpio0
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio1
    pin:
      number: GPIO1
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio2
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio3
    pin:
      number: GPIO3
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio12
    pin:
      number: GPIO12
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio13
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio15
    pin:
      number: GPIO15
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio16
    pin:
      number: GPIO16
      mode: INPUT_PULLDOWN

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: 60min
      - 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