-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
212 lines (177 loc) · 8.62 KB
/
__init__.py
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
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome import pins
from esphome.components import uart
from esphome.const import CONF_ID, CONF_PASSWORD, CONF_SPEED, CONF_STATE, CONF_TRIGGER_ID, \
CONF_UART_ID
CODEOWNERS = ['@OnFreund', '@loongyh']
DEPENDENCIES = ['uart']
AUTO_LOAD = ['binary_sensor', 'sensor']
MULTI_CONF = True
CONF_SENSING_PIN = 'sensing_pin'
CONF_NEW_PASSWORD = 'new_password'
CONF_ON_FINGER_SCAN_MATCHED = 'on_finger_scan_matched'
CONF_ON_FINGER_SCAN_UNMATCHED = 'on_finger_scan_unmatched'
CONF_ON_ENROLLMENT_SCAN = 'on_enrollment_scan'
CONF_ON_ENROLLMENT_DONE = 'on_enrollment_done'
CONF_ON_ENROLLMENT_FAILED = 'on_enrollment_failed'
CONF_FINGER_ID = 'finger_id'
CONF_NUM_SCANS = 'num_scans'
CONF_RXXX_ID = 'rxxx_id'
CONF_COLOR = 'color'
CONF_COUNT = 'count'
rxxx_ns = cg.esphome_ns.namespace('rxxx')
RxxxComponent = rxxx_ns.class_('RxxxComponent', cg.PollingComponent, uart.UARTDevice)
FingerScanMatchedTrigger = rxxx_ns.class_('FingerScanMatchedTrigger',
automation.Trigger.template(
cg.uint16,
cg.uint16))
FingerScanUnmatchedTrigger = rxxx_ns.class_('FingerScanUnmatchedTrigger',
automation.Trigger.template())
EnrollmentScanTrigger = rxxx_ns.class_('EnrollmentScanTrigger',
automation.Trigger.template(
cg.uint8,
cg.uint16))
EnrollmentDoneTrigger = rxxx_ns.class_('EnrollmentDoneTrigger',
automation.Trigger.template(cg.uint16))
EnrollmentFailedTrigger = rxxx_ns.class_('EnrollmentFailedTrigger',
automation.Trigger.template(cg.uint16))
EnrollmentAction = rxxx_ns.class_('EnrollmentAction', automation.Action)
CancelEnrollmentAction = rxxx_ns.class_('CancelEnrollmentAction', automation.Action)
DeleteAction = rxxx_ns.class_('DeleteAction', automation.Action)
DeleteAllAction = rxxx_ns.class_('DeleteAllAction', automation.Action)
LEDControlAction = rxxx_ns.class_('LEDControlAction', automation.Action)
AuraLEDControlAction = rxxx_ns.class_('AuraLEDControlAction', automation.Action)
AuraLEDMode = rxxx_ns.enum('AuraLEDMode')
AURA_LED_STATES = {
'BREATHING': AuraLEDMode.BREATHING,
'FLASHING': AuraLEDMode.FLASHING,
'ALWAYS_ON': AuraLEDMode.ALWAYS_ON,
'ALWAYS_OFF': AuraLEDMode.ALWAYS_OFF,
'GRADUAL_ON': AuraLEDMode.GRADUAL_ON,
'GRADUAL_OFF': AuraLEDMode.GRADUAL_OFF,
}
validate_aura_led_states = cv.enum(AURA_LED_STATES, upper=True)
AURA_LED_COLORS = {
'RED': AuraLEDMode.RED,
'BLUE': AuraLEDMode.BLUE,
'PURPLE': AuraLEDMode.PURPLE,
}
validate_aura_led_colors = cv.enum(AURA_LED_COLORS, upper=True)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(RxxxComponent),
cv.Optional(CONF_SENSING_PIN): pins.gpio_input_pin_schema,
cv.Optional(CONF_PASSWORD): cv.uint32_t,
cv.Optional(CONF_NEW_PASSWORD): cv.uint32_t,
cv.Optional(CONF_ON_FINGER_SCAN_MATCHED): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FingerScanMatchedTrigger),
}),
cv.Optional(CONF_ON_FINGER_SCAN_UNMATCHED): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FingerScanUnmatchedTrigger),
}),
cv.Optional(CONF_ON_ENROLLMENT_SCAN): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(EnrollmentScanTrigger),
}),
cv.Optional(CONF_ON_ENROLLMENT_DONE): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(EnrollmentDoneTrigger),
}),
cv.Optional(CONF_ON_ENROLLMENT_FAILED): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(EnrollmentFailedTrigger),
}),
}).extend(cv.polling_component_schema('500ms')).extend(uart.UART_DEVICE_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
if CONF_PASSWORD in config:
password = config[CONF_PASSWORD]
cg.add(var.set_password(password))
uart_device = yield uart.register_uart_device(var, config)
cg.add(var.set_uart(uart_device))
if CONF_NEW_PASSWORD in config:
new_password = config[CONF_NEW_PASSWORD]
cg.add(var.set_new_password(new_password))
if CONF_SENSING_PIN in config:
sensing_pin = yield cg.gpio_pin_expression(config[CONF_SENSING_PIN])
cg.add(var.set_sensing_pin(sensing_pin))
for conf in config.get(CONF_ON_FINGER_SCAN_MATCHED, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [(cg.uint16, 'finger_id'),
(cg.uint16, 'confidence')], conf)
for conf in config.get(CONF_ON_FINGER_SCAN_UNMATCHED, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_ENROLLMENT_SCAN, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [(cg.uint8, 'scan_number'),
(cg.uint16, 'finger_id')], conf)
for conf in config.get(CONF_ON_ENROLLMENT_DONE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [(cg.uint16, 'finger_id')], conf)
for conf in config.get(CONF_ON_ENROLLMENT_FAILED, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [(cg.uint16, 'finger_id')], conf)
# https://platformio.org/lib/show/382/Adafruit%20Fingerprint%20Sensor%20Library
cg.add_library('https://github.com/loongyh/Adafruit-Fingerprint-Sensor-Library.git', None)
@automation.register_action('rxxx.enroll', EnrollmentAction, cv.maybe_simple_value({
cv.GenerateID(): cv.use_id(RxxxComponent),
cv.Required(CONF_FINGER_ID): cv.templatable(cv.uint16_t),
cv.Optional(CONF_NUM_SCANS): cv.templatable(cv.uint8_t),
}, key=CONF_FINGER_ID))
def rxxx_enroll_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
yield cg.register_parented(var, config[CONF_ID])
template_ = yield cg.templatable(config[CONF_FINGER_ID], args, cg.uint16)
cg.add(var.set_finger_id(template_))
if CONF_NUM_SCANS in config:
template_ = yield cg.templatable(config[CONF_NUM_SCANS], args, cg.uint8)
cg.add(var.set_num_scans(template_))
yield var
@automation.register_action('rxxx.cancel_enroll', CancelEnrollmentAction, cv.Schema({
cv.GenerateID(): cv.use_id(RxxxComponent),
}))
def rxxx_cancel_enroll_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
yield cg.register_parented(var, config[CONF_ID])
yield var
@automation.register_action('rxxx.delete', DeleteAction, cv.maybe_simple_value({
cv.GenerateID(): cv.use_id(RxxxComponent),
cv.Required(CONF_FINGER_ID): cv.templatable(cv.uint16_t),
}, key=CONF_FINGER_ID))
def rxxx_delete_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
yield cg.register_parented(var, config[CONF_ID])
template_ = yield cg.templatable(config[CONF_FINGER_ID], args, cg.uint16)
cg.add(var.set_finger_id(template_))
yield var
@automation.register_action('rxxx.delete_all', DeleteAllAction, cv.Schema({
cv.GenerateID(): cv.use_id(RxxxComponent),
}))
def rxxx_delete_all_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
yield cg.register_parented(var, config[CONF_ID])
yield var
@automation.register_action('rxxx.led_control', LEDControlAction, cv.maybe_simple_value({
cv.GenerateID(): cv.use_id(RxxxComponent),
cv.Required(CONF_STATE): cv.templatable(cv.boolean),
}, key=CONF_STATE))
def rxxx_led_control_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
yield cg.register_parented(var, config[CONF_ID])
template_ = yield cg.templatable(config[CONF_STATE], args, cg.bool_)
cg.add(var.set_state(template_))
yield var
@automation.register_action('rxxx.aura_led_control', AuraLEDControlAction, cv.Schema({
cv.GenerateID(): cv.use_id(RxxxComponent),
cv.Required(CONF_STATE): cv.templatable(validate_aura_led_states),
cv.Required(CONF_SPEED): cv.templatable(cv.uint8_t),
cv.Required(CONF_COLOR): cv.templatable(validate_aura_led_colors),
cv.Required(CONF_COUNT): cv.templatable(cv.uint8_t),
}))
def rxxx_aura_led_control_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
yield cg.register_parented(var, config[CONF_ID])
for key in [CONF_STATE, CONF_SPEED, CONF_COLOR, CONF_COUNT]:
template_ = yield cg.templatable(config[key], args, cg.uint8)
cg.add(getattr(var, f'set_{key}')(template_))
yield var