first test

This commit is contained in:
Lucas Pleß 2023-10-07 21:59:34 +02:00
parent f4483c17b0
commit 746abea941
1 changed files with 164 additions and 0 deletions

164
mlock-common.yaml Normal file
View File

@ -0,0 +1,164 @@
# todos:
# - move code into packages to make this re-useable
# - testing
substitutions:
vault_role_id: !secret mlock_role_id
vault_secret_id: !secret mlock_secret_id
ota_password: "39e67462fb5b961d1c9de6d9ee7bd571"
api_enckey: "F815gY6pnO0yyUSLqYbzE0VrW0j9uGlv9lJ3XXUer2U="
wifi_ssid: !secret wifi_ssid
wifi_password: !secret wifi_password
###### nothing to change below this line ######
esphome:
name: $name_of_board
platform: ESP8266
board: d1_mini
# Enable logging
logger:
api:
encryption:
key: $api_enckey
ota:
password: $ota_password
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:
- id: my_token
type: std::string
restore_value: no
- id: my_tag
type: std::string
restore_value: no
rc522_spi:
cs_pin: GPIO15
on_tag:
then:
# store the tag id into global variable
- lambda: |-
id(my_tag) = x;
- homeassistant.tag_scanned: !lambda 'return x;'
# login to vault with role_id to fetch short lived token
- http_request.post:
url: https://vault.ctdo.de/v1/auth/approle/login
headers:
Content-Type: application/json
json:
role_id: $vault_role_id
secret_id: $vault_secret_id
verify_ssl: false
on_response:
# fetch token from response, store into my_token
then:
# - logger.log:
# format: "Data: %s"
# args:
# - id(http_request_data).get_string()
- lambda: |-
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
const char* foobar = root["auth"]["client_token"];
id(my_token) = foobar;
});
# use the token to get json of scanned tag from vault
- http_request.get:
url: !lambda |-
return ((std::string) "https://vault.ctdo.de/v1/maschinenlock/" + id(my_tag));
headers:
X-Vault-Token: !lambda |-
return id(my_token).c_str();
on_response:
# when found, check if machine is allowed, turn on output
then:
- if:
condition:
lambda: 'return status_code == 200;'
then:
- lambda: |-
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
std::string foobar = root["data"]["$name_of_board"];
if(foobar == "True") {
id(switch_output).turn_on();
}
});
# connect the esphome switch to an physical output
output:
- platform: gpio
pin: D1
id: gpio_relais_output
# switch component for the output state
switch:
- platform: output
name: "Relais Output"
id: ${name_of_board}_switch_output
output: 'gpio_relais_output'
internal: true # hide from Homeassistant, so no one can turn it on without Tag-Scanning
binary_sensor:
# sensor input for Turning Device off
- platform: gpio
pin:
number: D3
inverted: true
mode: INPUT_PULLUP
id: gpio_input_ausschalter
on_press:
- switch.turn_off: switch_output
# a template sensor for showing the current switch state (read only)
- platform: template
name: "${name_of_board} Status Ausgang"
publish_initial_state: true
lambda: |-
return id(switch_output).state;
# 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:
- switch.turn_off: switch_output