added status LED WS2812

This commit is contained in:
Lucas Pleß 2024-03-02 02:05:43 +01:00
parent e4f12cd9c8
commit bf8cfd182f
1 changed files with 77 additions and 23 deletions

View File

@ -15,6 +15,13 @@ esphome:
name: mlock-${name_of_board}
platform: ESP8266
board: d1_mini
on_boot:
- priority: 600
then:
- light.addressable_set: { id: status_led, red: 0%, green: 0%, blue: 50% }
- priority: -100
then:
- light.addressable_set: { id: status_led, red: 0%, green: 0%, blue: 100% }
# Enable logging
logger:
@ -60,11 +67,19 @@ globals:
- id: my_tag
type: std::string
restore_value: no
- id: may_switch_output
type: int
restore_value: no
rc522_spi:
cs_pin: GPIO15
on_tag:
then:
- light.addressable_set:
id: status_led
red: 100%
green: 100%
blue: 0%
# store the tag id into global variable
- lambda: |-
id(my_tag) = x;
@ -81,24 +96,18 @@ rc522_spi:
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;
id(my_token) = (const char*) root["auth"]["client_token"];
});
# 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();
X-Vault-Token: !lambda return id(my_token).c_str();
on_response:
# when found, check if machine is allowed, turn on output
# when found, check if machine is allowed, turn on output or blink LED red
then:
- if:
condition:
@ -106,28 +115,60 @@ rc522_spi:
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") {
int foobar = root["data"]["mlock-$name_of_board"];
if(foobar == 1) {
id(mlock_${name_of_board}_switch).turn_on();
id(may_switch_output) = 1;
} else {
id(may_switch_output) = 0;
}
});
- if:
condition:
lambda: 'return id(may_switch_output);'
then:
- light.addressable_set: { id: status_led, red: 0%, green: 100%, blue: 0% }
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
- if: # return LED to old state before blinking
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: # 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 old state before blinking
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% }
# 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
- platform: gpio
pin: D1
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
@ -138,6 +179,8 @@ binary_sensor:
id: ${name_of_board}gpio_input_ausschalter
on_press:
- switch.turn_off: mlock_${name_of_board}_switch
- light.addressable_set: { id: status_led, red: 100%, green: 0%, blue: 0% }
# a template sensor for showing the current switch state (read only)
- platform: template
id: mlock_${name_of_board}_state_output
@ -154,8 +197,19 @@ button:
id: ${name_of_board}_btn_ausschalter
on_press:
- switch.turn_off: mlock_${name_of_board}_switch
- light.addressable_set: { id: status_led, red: 100%, green: 0%, blue: 0% }
light:
- platform: neopixelbus
type: GRB
variant: WS2812
pin: D4
name: status_led
id: status_led
num_leds: 1
internal: true