-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcu-desk-lamp.yaml
143 lines (130 loc) · 3.42 KB
/
mcu-desk-lamp.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
substitutions:
device: desk-lamp
name: Mi Desk Lamp
comment: ""
esphome:
name: mcu-${device}
comment: ${comment}
esp32:
board: m5stack-atom
framework:
type: esp-idf
version: 5.1.2
platform_version: 6.6.0
packages:
common: !include common/common.yaml
# Mi Desk Lamp
# Pin Function (Name)
# GPIO18: Warm White (GPIO_PWM2)
# GPIO19: Cold White (GPIO_PWM1)
# GPIO21: Rotary switch A pin (GPIO_ROT_A)
# GPIO26: Rotary switch B pin (GPIO_ROT_B)
# GPIO36: Button (GPIO_KEY1)
binary_sensor:
- platform: gpio
pin:
number: 36
inverted: yes
id: rotary_button
# filters:
# invert:
on_click:
- light.toggle: mi_desk_light
# on_double_click:
# - switch.toggle: relay_2
light:
- platform: cwww
name: "${name} Light"
id: mi_desk_light
gamma_correct: 1
default_transition_length: 100ms
restore_mode: ALWAYS_OFF
cold_white: cold_white
warm_white: warm_white
cold_white_color_temperature: 153 mireds
warm_white_color_temperature: 370 mireds
output:
- platform: ledc
pin: 18
max_power: 0.85
id: warm_white
- platform: ledc
pin: 19
max_power: 0.85
id: cold_white
sensor:
- platform: rotary_encoder
id: rotary
pin_a: 21
pin_b: 26
resolution: "2"
# min_value: 128
# max_value: 255
# on_value:
# - while:
# condition:
# binary_sensor.is_off: button
# then:
# - light.dim_relative:
# id: mi_desk_light
# relative_brightness: 5%
# transition_length: 0.1s
# - delay: 0.1s
# filters:
# - or:
# - debounce: 0.1s
# - delta: 10
# - lambda: |-
# if (x < 0.0) return 0.0;
# if (x > 255.0) return 255.0;
# return x;
# on_value:
# then:
# # - output.set_level:
# # id: warm_white
# # level: !lambda "return x/256.0;"
# - output.set_level:
# id: warm_white
# level: !lambda |-
# return x / 255;
on_value:
then:
- lambda: |-
float brightness;
float temp = id(mi_desk_light).remote_values.get_color_temperature();
auto call = id(mi_desk_light).turn_on();
id(mi_desk_light).remote_values.as_brightness( &brightness );
// positive rotation
if (id(rotary).state > 0) {
//
if (id(rotary_button).state) {
temp -= 5.0f;
} else {
brightness -= 0.05f;
}
id(rotary).set_value(0);
// negative rotation
} else if (id(rotary).state < 0) {
if (id(rotary_button).state) {
temp += 5.0f;
} else {
brightness += 0.05f;
}
id(rotary).set_value(0);
}
// out of bounds check for brightness
if (brightness < 0.1f) {
brightness = 0.1f;
} else if (brightness > 1.0f) {
brightness = 1.0f;
}
// out of bounds check for color temp
if (temp < 153.0f) {
temp = 153.0f;
} else if (temp > 370.0f) {
temp = 370.0f;
}
// apply new settings
call.set_brightness(brightness);
call.set_color_temperature(temp);
call.perform();