esphome/mlock-common.yaml

164 lines
4.3 KiB
YAML

# todos:
# - move code into packages to make this re-useable
# - testing
substitutions:
ota_password: ""
api_enckey: ""
wifi_ssid: ""
wifi_password: ""
vault_role_id: ""
vault_secret_id: ""
###### nothing to change below this line ######
esphome:
name: mlock-${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"]["mlock-$name_of_board"];
if(foobar == "True") {
id(mlock_${name_of_board}_switch).turn_on();
}
});
# connect the esphome switch to an physical output
output:
- platform: gpio
pin: D1
id: mlock_${name_of_board}_gpio_relais_output
# switch component for the output state
switch:
- platform: output
name: "Relais Output"
id: mlock_${name_of_board}_switch
output: 'mlock_${name_of_board}_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: ${name_of_board}gpio_input_ausschalter
on_press:
- switch.turn_off: mlock_${name_of_board}_switch
# a template sensor for showing the current switch state (read only)
- platform: template
id: mlock_${name_of_board}_state_output
name: "${name_of_board} Status Ausgang"
publish_initial_state: true
lambda: |-
return id(mlock_${name_of_board}_switch).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: mlock_${name_of_board}_switch