-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcu-bedroom-lamp-left.yaml
340 lines (315 loc) · 9.95 KB
/
mcu-bedroom-lamp-left.yaml
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
substitutions:
device: bedroom-lamp-left
name: Bedroom Lamp Left
comment: ""
default_transition_length: 800ms
esphome:
name: mcu-${device}
comment: ${comment}
esp32:
board: esp32doit-devkit-v1
framework:
type: esp-idf
sdkconfig_options:
CONFIG_FREERTOS_UNICORE: y
ESP_MAC_IGNORE_MAC_CRC_ERROR: y
advanced:
ignore_efuse_mac_crc: true
external_components:
- source:
type: git
url: https://github.com/mmakaay/esphome-xiaomi_bslamp2
ref: dev
refresh: 1h
packages:
common: !include common/common.yaml
# Mi Desk Lamp
# Pin Function (Name)
# GPIO02: Button (GPIO_KEY1)
# GPIO04: Cold White (GPIO_PWM1)
# GPIO05: Warm White (GPIO_PWM2)
# GPIO12: Rotary switch A pin (GPIO_ROT_A)
# GPIO13: Rotary switch B pin (GPIO_ROT_B)
# ------------------------------------------------------------------------
# Behavior: default
#
# * Presets that resemble the original Xiaomi firmware
# (one group of RGB colors, one group of white light colors)
# * Tapping the power button, toggles the light
# * Holding the power button, turns on night light mode
# * Tapping the color button, activates the next preset
# * Holding the color button, activates the next preset group
# * Touching the slider sets the brightness level and turns on the lamp
# * When the light is on, the front panel illumination is on
# * When the night light is on, only the power button is illuminated
# ------------------------------------------------------------------------
# A global variable to keep track of the brightness level. This one is
# used by the update_front_panel script to represent the brightness level
# using the front panel illumination.
#
# This variable is used instead of querying the light component directly,
# because that brightness level may vary during a light transition. The
# behavior that I want: when you touch the front panel slider at 75%, then
# the illumination rises directly to that level, while the light might do
# a smooth transition.
globals:
- id: current_brightness
type: float
initial_value: "0.0"
- id: group_control
type: bool
initial_value: "false"
i2c:
id: front_panel_i2c
sda: GPIO21
scl: GPIO19
scan: true
# This script updates the front panel illumination.
script:
- id: update_front_panel
mode: restart
then:
- front_panel.turn_off_leds: ALL
# In rgb or white light mode, turn on the front panel illumination,
# and show the current brightness level.
- if:
condition:
or:
- text_sensor.state:
id: my_light_mode
state: rgb
- text_sensor.state:
id: my_light_mode
state: white
then:
- front_panel.turn_on_leds: [ POWER, COLOR ]
- front_panel.set_level: !lambda return id(current_brightness);
# In night light mode, turn off the front panel illumination, except
# for the power button.
- if:
condition:
- text_sensor.state:
id: my_light_mode
state: night
then:
- front_panel.turn_on_leds: [ POWER ]
- front_panel.update_leds:
# If you want to control light presets from Home Assistant, then you can
# make use of the following service.
api:
reboot_timeout: 0s
services:
- service: activate_preset
variables:
my_group: string
my_preset: string
then:
- preset.activate:
group: !lambda 'return my_group;'
preset: !lambda 'return my_preset;'
# These OTA triggers are used to provide some visual feedback during the OTA
# flashing process. The light is turned blue when the upgrade starts, the
# brightness indicator will represent the update progress (fills up from 0%
# to 100%), the light will flash red when the upgrade fails or green when the
# upgrade succeeds.
ota:
platform: esphome
password: !secret ota_password
on_begin:
then:
- light.disco_on:
id: my_light
red: 0%
green: 0%
blue: 100%
brightness: 2%
transition_length: 0s
on_progress:
then:
- front_panel.set_level: !lambda return (x / 100.0f);
- front_panel.update_leds:
on_end:
then:
- light.disco_on:
id: my_light
red: 0%
green: 100%
blue: 0%
brightness: 2%
transition_length: 0s
on_error:
then:
- light.disco_on:
id: my_light
red: 100%
green: 0%
blue: 0%
brightness: 2%
- delay: 1s
- light.disco_off:
id: my_light
# This component controls the LED lights of the lamp.
light:
- platform: xiaomi_bslamp2
id: my_light
name: ${name}
default_transition_length: ${default_transition_length}
# When the brightness changes, update the front panel illumination.
on_brightness:
then:
- globals.set:
id: current_brightness
value: !lambda return x;
- script.execute: update_front_panel
# You can use any effects that you like. These are just examples.
effects:
- random:
name: "Slow Random"
transition_length: 30s
update_interval: 30s
- random:
name: "Fast Random"
transition_length: 3s
update_interval: 3s
# You can define one or more groups of presets. These presets can
# be activated using various "preset.activate" action options.
# The presets can for example be used to mimic the behavior of the
# original firmware (tapping the color button = go to next preset,
# holding the color button = switch between RGB and white light mode).
# These bindings have been setup below, using the binary_sensor for
# the color button.
presets:
# rgb:
# red: { red: 100%, green: 0%, blue: 0% }
# green: { red: 0%, green: 100%, blue: 0% }
# blue: { red: 0%, green: 0%, blue: 100% }
# yellow: { red: 100%, green: 100%, blue: 0% }
# purple: { red: 100%, green: 0%, blue: 100% }
# randomize: { effect: Fast Random }
white:
cold: { color_temperature: 153 mireds }
chilly: { color_temperature: 275 mireds }
luke: { color_temperature: 400 mireds }
warm: { color_temperature: 588 mireds }
# Binary sensors can be created for handling front panel touch / release
# events. To specify what part of the front panel to look at, the "for"
# parameter can be set to: "POWER_BUTTON", "COLOR_BUTTON" or "SLIDER".
binary_sensor:
# When tapping the power button, toggle the light.
# When holding the power button, turn on night light mode.
- platform: xiaomi_bslamp2
id: my_power_button
for: POWER_BUTTON
on_multi_click:
- timing:
- ON for at most 0.8s
then:
- light.toggle: my_light
- timing:
- ON for at least 0.8s
then:
- light.turn_on:
id: my_light
brightness: 1%
# When tapping the color button, activate the next preset.
# When holding the color button, activate the next preset group.
- platform: xiaomi_bslamp2
id: my_color_button
for: COLOR_BUTTON
on_multi_click:
- timing:
- ON for at most 0.6s
then:
- preset.activate:
next: preset
- timing:
- ON for at least 0.6s
then:
- globals.set:
id: group_control
value: !lambda return !id(group_control);
# - timing:
# - ON for at least 0.6s
# then:
# - preset.activate:
# next: group
# This sensor component publishes touch events for the front panel slider.
# The published value represents the level at which the slider was touched.
# By default, values range from 0.01 to 1.00 (in 20 steps). This range can
# be modified using the "range_from" and "range_to" parameters.
sensor:
# When the slider is touched, update the brightness.
# Brightness 0.01 initiates the light night mode, which has already
# been handled above (by holding the power button). Therefore, brightness
# starts from 0.02 here, to not trigger night mode using the slider.
- platform: xiaomi_bslamp2
id: my_slider_level
range_from: 0.02
on_value:
- if:
condition:
lambda: |-
return id(group_control);
then:
- homeassistant.service:
service: light.turn_on
data:
light_name: bedroom_lamp
brightness: !lambda return x;
else:
- light.turn_on:
id: my_light
brightness: !lambda return x;
# This text sensor propagates the currently active light mode.
# The possible light modes are: "off", "rgb", "white" and "night".
text_sensor:
- platform: xiaomi_bslamp2
# name: ${name} Light Mode
id: my_light_mode
output:
- platform: ledc
id: output_red
pin: GPIO13
frequency: 3000
channel: 0
- platform: ledc
id: output_green
pin: GPIO14
frequency: 3000
channel: 1
- platform: ledc
id: output_blue
pin: GPIO5
frequency: 3000
channel: 2
- platform: ledc
id: output_white
pin: GPIO12
frequency: 10000
channel: 4
- platform: gpio
id: output_master1
pin: GPIO33
- platform: gpio
id: output_master2
pin:
number: GPIO4
mode: OUTPUT
# This float output can be used to control the front panel illumination +
# level indicator LEDs. Value 0.0 turns off the illumination. Other values
# (up to 1.0) turn on the illumination and set the level indicator to the
# requested level.
- platform: xiaomi_bslamp2
id: my_front_panel_illumination
xiaomi_bslamp2:
light:
red: output_red
green: output_green
blue: output_blue
white: output_white
master1: output_master1
master2: output_master2
front_panel:
i2c: front_panel_i2c
address: 0x2C
trigger_pin: GPIO16