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} name: mlock-${name_of_board}
platform: ESP8266 platform: ESP8266
board: d1_mini 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 # Enable logging
logger: logger:
@ -60,11 +67,19 @@ globals:
- id: my_tag - id: my_tag
type: std::string type: std::string
restore_value: no restore_value: no
- id: may_switch_output
type: int
restore_value: no
rc522_spi: rc522_spi:
cs_pin: GPIO15 cs_pin: GPIO15
on_tag: on_tag:
then: then:
- light.addressable_set:
id: status_led
red: 100%
green: 100%
blue: 0%
# store the tag id into global variable # store the tag id into global variable
- lambda: |- - lambda: |-
id(my_tag) = x; id(my_tag) = x;
@ -81,24 +96,18 @@ rc522_spi:
on_response: on_response:
# fetch token from response, store into my_token # fetch token from response, store into my_token
then: then:
# - logger.log:
# format: "Data: %s"
# args:
# - id(http_request_data).get_string()
- lambda: |- - lambda: |-
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) { json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
const char* foobar = root["auth"]["client_token"]; id(my_token) = (const char*) root["auth"]["client_token"];
id(my_token) = foobar;
}); });
# use the token to get json of scanned tag from vault # use the token to get json of scanned tag from vault
- http_request.get: - http_request.get:
url: !lambda |- url: !lambda |-
return ((std::string) "https://vault.ctdo.de/v1/maschinenlock/" + id(my_tag)); return ((std::string) "https://vault.ctdo.de/v1/maschinenlock/" + id(my_tag));
headers: headers:
X-Vault-Token: !lambda |- X-Vault-Token: !lambda return id(my_token).c_str();
return id(my_token).c_str();
on_response: 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: then:
- if: - if:
condition: condition:
@ -106,28 +115,60 @@ rc522_spi:
then: then:
- lambda: |- - lambda: |-
json::parse_json(id(http_request_data).get_string(), [](JsonObject root) { json::parse_json(id(http_request_data).get_string(), [](JsonObject root) {
std::string foobar = root["data"]["mlock-$name_of_board"]; int foobar = root["data"]["mlock-$name_of_board"];
if(foobar == "True") { if(foobar == 1) {
id(mlock_${name_of_board}_switch).turn_on(); 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 component for the output state
switch: switch:
- platform: output - platform: gpio
pin: D1
name: "Relais Output" name: "Relais Output"
id: mlock_${name_of_board}_switch 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 internal: true # hide from Homeassistant, so no one can turn it on without Tag-Scanning
binary_sensor: binary_sensor:
# sensor input for Turning Device off # sensor input for Turning Device off
- platform: gpio - platform: gpio
@ -138,6 +179,8 @@ binary_sensor:
id: ${name_of_board}gpio_input_ausschalter id: ${name_of_board}gpio_input_ausschalter
on_press: on_press:
- switch.turn_off: mlock_${name_of_board}_switch - 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) # a template sensor for showing the current switch state (read only)
- platform: template - platform: template
id: mlock_${name_of_board}_state_output id: mlock_${name_of_board}_state_output
@ -154,8 +197,19 @@ button:
id: ${name_of_board}_btn_ausschalter id: ${name_of_board}_btn_ausschalter
on_press: on_press:
- switch.turn_off: mlock_${name_of_board}_switch - 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