summaryrefslogtreecommitdiffstats
path: root/esphome/config/couchlight.yaml
blob: 7244163bf1083deb057df611abc64d504aa7dcde (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
substitutions:
  device_name: couchlight
  friendly_name: "Couch Light"
  fan_code: "0000"

external_components:
  - source: github://pr#6300
    components: [ cc1101 ]
  - source: my_components
    components: [ mycc1101 ]

esphome:
  name: ${device_name}
  friendly_name: ${friendly_name}
  devices:
    - id: couchlight
      name: "Couch Light"
    - id: fancontrol
      name: "Fan Control"
  on_boot:
    then:
      - lock.template.publish:
          id: child_lock
          state: UNLOCKED

esp8266:
  board: d1_mini

# Enable logging
logger:

# 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: light1
    device_id: couchlight
    platform: binary
    name: "Spot 1"
    output: relay_light1

  - id: light2
    device_id: couchlight
    platform: binary
    name: "Spot 2"
    output: relay_light2

output:
  - id: relay_light1
    platform: gpio
    pin: GPIO5 # D1

  - id: relay_light2
    platform: gpio
    pin: GPIO4 # D2

  # UNUSED GPIO
  - platform: gpio
    id: unused_gpio1
    pin:
      number: TX # GPIO1
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio3
    pin:
      number: RX # GPIO3
      mode: INPUT_PULLUP

binary_sensor:
  - id: button_light1
    device_id: couchlight
    platform: gpio
    name: "Spot 1 PushButton"
    pin:
      number: GPIO2 # D4
      mode: INPUT_PULLUP
    filters:
      - invert
      - delayed_on: 50ms
    on_press:
      then:
        - if:
            condition:
              lock.is_unlocked: child_lock
            then:
              - light.toggle: light1
    internal: true # hide from UI

  - id: button_light2
    device_id: couchlight
    platform: gpio
    name: "Spot 2 PushButton"
    pin:
      number: GPIO0 # D3
      mode: INPUT_PULLUP
    filters:
      - invert
      - delayed_on: 50ms
    on_press:
      then:
        - if:
            condition:
              lock.is_unlocked: child_lock
            then:
              - light.toggle: light2
    internal: true # hide from UI

lock:
  - id: child_lock
    device_id: couchlight
    platform: template
    name: "Child Lock"
    optimistic: true
    on_lock:
      - script.execute: reset_child_lock
    on_unlock:
      - script.stop: reset_child_lock

spi:
  clk_pin:  GPIO14 # D5
  miso_pin: GPIO12 # D6
  mosi_pin: GPIO13 # D7

mycc1101:
  id: transceiver
  cs_pin: GPIO15 # D8
  gdo0_pin:
    number: GPIO16 # D0
    allow_other_uses: true
  output_power: 10 # 5
  tuner:
    frequency: 433920
    if_frequency: 153
    bandwidth: 270
    symbol_rate: 6000
    sync_mode: None
    carrier_sense_above_threshold: false
    modulation: ASK/OOK

remote_transmitter:
  pin:
    number: GPIO16 # same as GDO0
    allow_other_uses: true
  carrier_duty_percent: 100%

fan:
  - id: myfan
    device_id: fancontrol
    platform: template
    name: "Fan"
    speed_count: 6
    has_direction: true
    restore_mode: ALWAYS_OFF
    on_turn_off:
      - logger.log: "on_turn_off"
      - lambda: |-
          id(transceiver)->wakeup();
          id(fan_stop_frames).execute();
          id(transceiver)->power_down();
    on_turn_on:
      - logger.log: "on_turn_on"
      - lambda: |-
          id(transceiver)->wakeup();
          auto x = id(myfan).speed;
          if      (x == 1) { id(fan_speed1_frames).execute(); }
          else if (x == 2) { id(fan_speed2_frames).execute(); }
          else if (x == 3) { id(fan_speed3_frames).execute(); }
          else if (x == 4) { id(fan_speed4_frames).execute(); }
          else if (x == 5) { id(fan_speed5_frames).execute(); }
          else if (x == 6) { id(fan_speed6_frames).execute(); }
          id(transceiver)->power_down();
    on_speed_set:
      - logger.log: "on_speed_set"
      - lambda: |-
          if (!id(myfan).state)
            return;
          id(transceiver)->wakeup();
          if      (x == 1) { id(fan_speed1_frames).execute(); }
          else if (x == 2) { id(fan_speed2_frames).execute(); }
          else if (x == 3) { id(fan_speed3_frames).execute(); }
          else if (x == 4) { id(fan_speed4_frames).execute(); }
          else if (x == 5) { id(fan_speed5_frames).execute(); }
          else if (x == 6) { id(fan_speed6_frames).execute(); }
          id(transceiver)->power_down();
    on_direction_set:
      - logger.log: "on_direction_set"
      - lambda: |-
          if (!id(myfan).state)
            return;
          id(transceiver)->wakeup();
          id(fan_reverse_frames).execute();
          id(transceiver)->power_down();

script:
  - id: reset_child_lock
    mode: restart
    then:
      - delay: 30min
      - lock.template.publish:
          id: child_lock
          state: UNLOCKED

  # see https://www.alldatasheet.com/datasheet-pdf/pdf/114193/ETC1/M1EN.html
  # code format is 0 <code> 0 <command>
  # commands:
  # - stop: 111101
  # - lvl1: 110111
  # - lvl2: 110101
  # - lvl3: 101111
  # - lvl4: 100111
  # - lvl5: 011101
  # - lvl6: 011111
  # - light: 111110
  # - reverse: 111011
  - id: fan_stop_frames
    then:
      - remote_transmitter.transmit_rc_switch_raw_cc1101:
          code: "0${fan_code}0111101"
          protocol: { pulse_length: 250, sync: [35,1], zero: [1,2], one: [2,1], inverted: true }
          repeat: { times: 10, wait_time: 0s }
  - id: fan_speed1_frames
    then:
      - remote_transmitter.transmit_rc_switch_raw_cc1101:
          code: "0${fan_code}0110111"
          protocol: { pulse_length: 250, sync: [35,1], zero: [1,2], one: [2,1], inverted: true }
          repeat: { times: 10, wait_time: 0s }
  - id: fan_speed2_frames
    then:
      - remote_transmitter.transmit_rc_switch_raw_cc1101:
          code: "0${fan_code}0110101"
          protocol: { pulse_length: 250, sync: [35,1], zero: [1,2], one: [2,1], inverted: true }
          repeat: { times: 10, wait_time: 0s }
  - id: fan_speed3_frames
    then:
      - remote_transmitter.transmit_rc_switch_raw_cc1101:
          code: "0${fan_code}0101111"
          protocol: { pulse_length: 250, sync: [35,1], zero: [1,2], one: [2,1], inverted: true }
          repeat: { times: 10, wait_time: 0s }
  - id: fan_speed4_frames
    then:
      - remote_transmitter.transmit_rc_switch_raw_cc1101:
          code: "0${fan_code}0100111"
          protocol: { pulse_length: 250, sync: [35,1], zero: [1,2], one: [2,1], inverted: true }
          repeat: { times: 10, wait_time: 0s }
  - id: fan_speed5_frames
    then:
      - remote_transmitter.transmit_rc_switch_raw_cc1101:
          code: "0${fan_code}0011101"
          protocol: { pulse_length: 250, sync: [35,1], zero: [1,2], one: [2,1], inverted: true }
          repeat: { times: 10, wait_time: 0s }
  - id: fan_speed6_frames
    then:
      - remote_transmitter.transmit_rc_switch_raw_cc1101:
          code: "0${fan_code}0011111"
          protocol: { pulse_length: 250, sync: [35,1], zero: [1,2], one: [2,1], inverted: true }
          repeat: { times: 10, wait_time: 0s }
  - id: fan_toggle_light_frames
    then:
      - remote_transmitter.transmit_rc_switch_raw_cc1101:
          code: "0${fan_code}0111110"
          protocol: { pulse_length: 250, sync: [35,1], zero: [1,2], one: [2,1], inverted: true }
          repeat: { times: 10, wait_time: 0s }
  - id: fan_reverse_frames
    then:
      - remote_transmitter.transmit_rc_switch_raw_cc1101:
          code: "0${fan_code}0111011"
          protocol: { pulse_length: 250, sync: [35,1], zero: [1,2], one: [2,1], inverted: true }
          repeat: { times: 10, wait_time: 0s }