2023-10-07 19:59:34 +00:00
|
|
|
# todos:
|
|
|
|
# - move code into packages to make this re-useable
|
|
|
|
# - testing
|
|
|
|
|
|
|
|
substitutions:
|
2023-10-07 20:56:07 +00:00
|
|
|
ota_password: ""
|
|
|
|
api_enckey: ""
|
2023-10-07 20:51:48 +00:00
|
|
|
wifi_ssid: ""
|
|
|
|
wifi_password: ""
|
|
|
|
vault_role_id: ""
|
|
|
|
vault_secret_id: ""
|
2023-10-07 19:59:34 +00:00
|
|
|
|
|
|
|
###### nothing to change below this line ######
|
|
|
|
esphome:
|
2023-10-07 20:19:20 +00:00
|
|
|
name: mlock-${name_of_board}
|
2024-07-20 02:25:03 +00:00
|
|
|
includes:
|
|
|
|
- mlock-common.h
|
2024-03-02 01:05:43 +00:00
|
|
|
on_boot:
|
2024-07-16 19:50:11 +00:00
|
|
|
- priority: 300
|
2024-03-02 01:05:43 +00:00
|
|
|
then:
|
2024-05-28 21:04:28 +00:00
|
|
|
- light.addressable_set: { id: status_led, red: 0%, green: 0%, blue: 100% }
|
2024-03-02 01:05:43 +00:00
|
|
|
- priority: -100
|
|
|
|
then:
|
2024-05-28 21:04:28 +00:00
|
|
|
- if:
|
|
|
|
condition:
|
|
|
|
switch.is_on: mlock_${name_of_board}_switch
|
|
|
|
then:
|
|
|
|
- light.addressable_set: { id: status_led, red: 0%, green: 100%, blue: 0% }
|
|
|
|
else:
|
|
|
|
- light.addressable_set: { id: status_led, red: 100%, green: 0%, blue: 0% }
|
2023-10-07 19:59:34 +00:00
|
|
|
|
2024-07-20 02:25:03 +00:00
|
|
|
esp8266:
|
|
|
|
board: d1_mini
|
|
|
|
restore_from_flash: true # max 96 bytes
|
|
|
|
|
2023-10-07 19:59:34 +00:00
|
|
|
# Enable logging
|
|
|
|
logger:
|
|
|
|
|
|
|
|
api:
|
|
|
|
encryption:
|
|
|
|
key: $api_enckey
|
|
|
|
|
|
|
|
ota:
|
2024-07-09 17:49:47 +00:00
|
|
|
password: $ota_password
|
2023-10-07 19:59:34 +00:00
|
|
|
|
|
|
|
wifi:
|
|
|
|
ssid: $wifi_ssid
|
|
|
|
password: $wifi_password
|
|
|
|
|
|
|
|
# Enable fallback hotspot (captive portal) in case wifi connection fails
|
|
|
|
ap:
|
|
|
|
ssid: "$name_of_board Fallback Hotspot"
|
|
|
|
password: "PZe2PJENtBiu"
|
|
|
|
|
|
|
|
manual_ip:
|
|
|
|
static_ip: $ip_addr
|
|
|
|
gateway: 172.23.23.1
|
|
|
|
dns1: 172.23.23.1
|
|
|
|
subnet: 255.255.255.0
|
|
|
|
|
|
|
|
captive_portal:
|
|
|
|
|
|
|
|
spi:
|
|
|
|
clk_pin: GPIO14
|
|
|
|
mosi_pin: GPIO13
|
|
|
|
miso_pin: GPIO12
|
|
|
|
|
|
|
|
http_request:
|
|
|
|
useragent: esphome
|
|
|
|
timeout: 2s
|
|
|
|
id: http_request_data
|
|
|
|
|
|
|
|
globals:
|
2024-07-20 02:25:03 +00:00
|
|
|
- id: vault_api_token
|
2023-10-07 19:59:34 +00:00
|
|
|
type: std::string
|
|
|
|
restore_value: no
|
2024-07-20 02:25:03 +00:00
|
|
|
- id: rfid_tag
|
2023-10-07 19:59:34 +00:00
|
|
|
type: std::string
|
|
|
|
restore_value: no
|
2024-07-20 02:25:03 +00:00
|
|
|
- id: access_allowed
|
|
|
|
type: bool
|
2024-03-02 01:05:43 +00:00
|
|
|
restore_value: no
|
2024-07-20 02:25:03 +00:00
|
|
|
initial_value: 'false'
|
|
|
|
- id: access_cache
|
|
|
|
type: unsigned int[24]
|
|
|
|
restore_value: yes
|
|
|
|
initial_value: '{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}'
|
2023-10-07 19:59:34 +00:00
|
|
|
|
2024-07-20 02:25:03 +00:00
|
|
|
script:
|
|
|
|
- id: check_access
|
|
|
|
then:
|
|
|
|
- if:
|
|
|
|
condition:
|
|
|
|
lambda: |-
|
|
|
|
int rfid_value = decode_token(id(rfid_tag));
|
|
|
|
for (int i = 0; i < 24; i++) {
|
|
|
|
if (id(access_cache)[i] == rfid_value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
then:
|
|
|
|
- script.execute: toggle_switch
|
|
|
|
- script.wait: toggle_switch
|
|
|
|
- if: # return LED to switch state before
|
|
|
|
condition:
|
|
|
|
switch.is_on: mlock_${name_of_board}_switch
|
|
|
|
then:
|
|
|
|
- light.addressable_set: { id: status_led, red: 0%, green: 100%, blue: 0% }
|
|
|
|
else:
|
|
|
|
- light.addressable_set: { id: status_led, red: 100%, green: 0%, blue: 0% }
|
|
|
|
else:
|
|
|
|
- script.execute: fetch_token_vault
|
|
|
|
- id: fetch_token_vault
|
2023-10-07 19:59:34 +00:00
|
|
|
then:
|
|
|
|
# login to vault with role_id to fetch short lived token
|
|
|
|
- http_request.post:
|
|
|
|
url: https://vault.ctdo.de/v1/auth/approle/login
|
2024-07-09 17:49:47 +00:00
|
|
|
verify_ssl: false
|
2023-10-07 19:59:34 +00:00
|
|
|
headers:
|
|
|
|
Content-Type: application/json
|
|
|
|
json:
|
|
|
|
role_id: $vault_role_id
|
|
|
|
secret_id: $vault_secret_id
|
|
|
|
on_response:
|
2024-07-20 02:25:03 +00:00
|
|
|
# fetch token from response, store into vault_api_token
|
2023-10-07 19:59:34 +00:00
|
|
|
then:
|
|
|
|
- lambda: |-
|
2024-07-09 17:49:47 +00:00
|
|
|
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
|
2024-07-20 02:25:03 +00:00
|
|
|
id(vault_api_token) = (const char*) root["auth"]["client_token"];
|
2023-10-07 19:59:34 +00:00
|
|
|
});
|
2024-07-20 02:25:03 +00:00
|
|
|
- script.execute: check_access_vault
|
|
|
|
- id: check_access_vault
|
|
|
|
then:
|
|
|
|
- lambda: |-
|
|
|
|
id(access_allowed) = false;
|
|
|
|
- http_request.get:
|
|
|
|
url: !lambda |-
|
|
|
|
return ((std::string) "https://vault.ctdo.de/v1/maschinenlock/" + id(rfid_tag));
|
|
|
|
verify_ssl: false
|
|
|
|
headers:
|
|
|
|
X-Vault-Token: !lambda return id(vault_api_token).c_str();
|
|
|
|
on_response:
|
|
|
|
then:
|
|
|
|
- if:
|
|
|
|
condition:
|
|
|
|
lambda: 'return status_code == 200;'
|
|
|
|
then: # when found, check if machine is allowed, turn on output or blink LED red
|
|
|
|
- lambda: |-
|
|
|
|
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
|
|
|
|
id(access_allowed) = (root["data"]["mlock-$name_of_board"] == "1");
|
|
|
|
});
|
|
|
|
- if:
|
|
|
|
condition:
|
|
|
|
lambda: 'return id(access_allowed);'
|
|
|
|
then:
|
|
|
|
- script.execute: toggle_switch
|
|
|
|
- script.execute: cache_token
|
|
|
|
- script.wait: toggle_switch
|
|
|
|
else:
|
|
|
|
- repeat:
|
|
|
|
count: 3
|
|
|
|
then:
|
|
|
|
- light.addressable_set: { id: status_led, red: 100%, green: 0%, blue: 0% }
|
|
|
|
- delay: 0.1s
|
|
|
|
- light.addressable_set: { id: status_led, red: 0%, green: 0%, blue: 0% }
|
|
|
|
- delay: 0.1s
|
|
|
|
else: # vault returns 404 on missing/unknown Tag so blink LED
|
|
|
|
- repeat:
|
|
|
|
count: 3
|
|
|
|
then:
|
|
|
|
- light.addressable_set: { id: status_led, red: 100%, green: 0%, blue: 0% }
|
|
|
|
- delay: 0.5s
|
|
|
|
- light.addressable_set: { id: status_led, red: 0%, green: 0%, blue: 0% }
|
|
|
|
- delay: 0.5s
|
|
|
|
- if: # return LED to switch state before
|
|
|
|
condition:
|
|
|
|
switch.is_on: mlock_${name_of_board}_switch
|
|
|
|
then:
|
|
|
|
- light.addressable_set: { id: status_led, red: 0%, green: 100%, blue: 0% }
|
|
|
|
else:
|
|
|
|
- light.addressable_set: { id: status_led, red: 100%, green: 0%, blue: 0% }
|
|
|
|
- id: toggle_switch
|
|
|
|
then:
|
|
|
|
- if:
|
|
|
|
condition:
|
|
|
|
switch.is_on: mlock_${name_of_board}_switch
|
|
|
|
then:
|
|
|
|
- switch.turn_off: mlock_${name_of_board}_switch
|
|
|
|
- homeassistant.event:
|
|
|
|
event: esphome.mlock_locked
|
|
|
|
data:
|
|
|
|
tag: !lambda return id(rfid_tag);
|
|
|
|
machine: ${name_of_board}
|
|
|
|
else:
|
|
|
|
- switch.turn_on: mlock_${name_of_board}_switch
|
|
|
|
- homeassistant.event:
|
|
|
|
event: esphome.mlock_unlocked
|
|
|
|
data:
|
|
|
|
tag: !lambda return id(rfid_tag);
|
|
|
|
machine: ${name_of_board}
|
|
|
|
- text.set:
|
|
|
|
id: ${name_of_board}_letzte_entsperrung
|
|
|
|
value: !lambda return id(rfid_tag);
|
|
|
|
- id: cache_token
|
|
|
|
then:
|
|
|
|
- lambda: |-
|
|
|
|
int rfid_value = decode_token(id(rfid_tag));
|
|
|
|
int rfid_value_pos = 23;
|
|
|
|
// search the token in the list to keep the access_cache unique
|
|
|
|
for (int i = 0; i < 24; i++) {
|
|
|
|
if (id(access_cache)[i] == rfid_value) {
|
|
|
|
rfid_value_pos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// shift the existing entries down
|
|
|
|
for (int i = rfid_value_pos; i >= 1; i--) {
|
|
|
|
id(access_cache)[i] = id(access_cache)[i - 1];
|
|
|
|
}
|
|
|
|
// write the new token to the start
|
|
|
|
id(access_cache)[0] = rfid_value;
|
|
|
|
|
|
|
|
rc522_spi:
|
|
|
|
cs_pin: GPIO15
|
|
|
|
on_tag:
|
|
|
|
then:
|
|
|
|
- light.addressable_set: { id: status_led, red: 100%, green: 60%, blue: 0% }
|
|
|
|
# small delay so the light can update its color
|
|
|
|
- delay: 15ms
|
|
|
|
# store the tag id into global variable
|
|
|
|
- lambda: |-
|
|
|
|
id(rfid_tag) = x;
|
|
|
|
- script.execute: check_access
|
2024-07-16 19:50:11 +00:00
|
|
|
- text.set:
|
|
|
|
id: ${name_of_board}_letzter_token
|
2024-07-20 02:25:03 +00:00
|
|
|
value: !lambda return id(rfid_tag);
|
2023-10-07 19:59:34 +00:00
|
|
|
|
|
|
|
# switch component for the output state
|
|
|
|
switch:
|
2024-03-02 01:05:43 +00:00
|
|
|
- platform: gpio
|
|
|
|
pin: D1
|
2023-10-07 19:59:34 +00:00
|
|
|
name: "Relais Output"
|
2023-10-07 20:19:20 +00:00
|
|
|
id: mlock_${name_of_board}_switch
|
2024-03-02 01:16:31 +00:00
|
|
|
internal: true # hide from Homeassistant, so no one can turn it on without Tag-Scanning
|
2023-10-07 19:59:34 +00:00
|
|
|
|
|
|
|
binary_sensor:
|
|
|
|
# sensor input for Turning Device off
|
|
|
|
- platform: gpio
|
|
|
|
pin:
|
|
|
|
number: D3
|
|
|
|
inverted: true
|
|
|
|
mode: INPUT_PULLUP
|
2023-10-07 20:19:20 +00:00
|
|
|
id: ${name_of_board}gpio_input_ausschalter
|
2023-10-07 19:59:34 +00:00
|
|
|
on_press:
|
2023-10-07 20:19:20 +00:00
|
|
|
- switch.turn_off: mlock_${name_of_board}_switch
|
2024-03-02 01:05:43 +00:00
|
|
|
- light.addressable_set: { id: status_led, red: 100%, green: 0%, blue: 0% }
|
|
|
|
|
2023-10-07 19:59:34 +00:00
|
|
|
# a template sensor for showing the current switch state (read only)
|
|
|
|
- platform: template
|
2023-10-07 20:19:20 +00:00
|
|
|
id: mlock_${name_of_board}_state_output
|
2023-10-07 19:59:34 +00:00
|
|
|
name: "${name_of_board} Status Ausgang"
|
|
|
|
publish_initial_state: true
|
|
|
|
lambda: |-
|
2023-10-07 20:19:20 +00:00
|
|
|
return id(mlock_${name_of_board}_switch).state;
|
2023-10-07 19:59:34 +00:00
|
|
|
|
|
|
|
# a button element for Homeassistant UI to allow turning off
|
|
|
|
button:
|
|
|
|
- platform: template
|
|
|
|
name: "${name_of_board} Ausschalter"
|
|
|
|
id: ${name_of_board}_btn_ausschalter
|
|
|
|
on_press:
|
2023-10-07 20:19:20 +00:00
|
|
|
- switch.turn_off: mlock_${name_of_board}_switch
|
2024-03-02 01:05:43 +00:00
|
|
|
- light.addressable_set: { id: status_led, red: 100%, green: 0%, blue: 0% }
|
2024-07-20 02:25:03 +00:00
|
|
|
- platform: template
|
|
|
|
name: "${name_of_board} Tokencache leeren"
|
|
|
|
id: ${name_of_board}_btn_tokencache_leeren
|
|
|
|
on_press:
|
|
|
|
- lambda: |-
|
|
|
|
for (int i = 0; i < 24; i++) {
|
|
|
|
id(access_cache)[i] = 0;
|
|
|
|
}
|
2024-03-02 01:05:43 +00:00
|
|
|
|
|
|
|
light:
|
|
|
|
- platform: neopixelbus
|
|
|
|
type: GRB
|
|
|
|
variant: WS2812
|
|
|
|
pin: D4
|
|
|
|
name: status_led
|
|
|
|
id: status_led
|
|
|
|
num_leds: 1
|
|
|
|
internal: true
|
|
|
|
|
2024-05-28 20:36:16 +00:00
|
|
|
text:
|
|
|
|
- platform: template
|
|
|
|
name: "$name_of_board Letzte Entsperung"
|
|
|
|
id: ${name_of_board}_letzte_entsperrung
|
|
|
|
optimistic: true
|
|
|
|
mode: text
|
2024-07-09 17:25:42 +00:00
|
|
|
- platform: template
|
|
|
|
name: "$name_of_board Zuletzt Gelesener Token"
|
|
|
|
id: ${name_of_board}_letzter_token
|
|
|
|
optimistic: true
|
|
|
|
mode: text
|