refactored code - json mqtt handling implemented

This commit is contained in:
Philipp Wo 2023-04-16 23:47:46 +02:00
parent 788af9b17d
commit c7940f8a50
3 changed files with 150 additions and 265 deletions

View File

@ -58,16 +58,16 @@ esphome run fingerprintdoor.yaml --device COM4
## FHEM MQTT2 device ## FHEM MQTT2 device
```shell ```shell
defmod myFingerprint MQTT2_DEVICE fingerprintdoor defmod myFingerDevice MQTT2_DEVICE fingerprintdoor
attr myFingerprint event-on-change-reading .* attr myFingerDevice event-on-change-reading .*
attr myFingerprint room MQTT2_DEVICE attr myFingerDevice setList GREEN:ALWAYS_ON,ALWAYS_OFF,BREATHING,FLASHING,GRADUAL_ON,GRADUAL_OFF fingerprintdoor/led/command {"color":"$EVTPART0","effect":"$EVTPART1","speed":120, "count":0}\
attr myFingerprint setList GREEN:on,off,breathe,flash fingerprintdoor/led green_$EVTPART1\ BLUE:ALWAYS_ON,ALWAYS_OFF,BREATHING,FLASHING,GRADUAL_ON,GRADUAL_OFF fingerprintdoor/led/command {"color":"$EVTPART0","effect":"$EVTPART1","speed":120, "count":0}\
BLUE:on,off,breathe,flash fingerprintdoor/led blue_$EVTPART1\ RED:ALWAYS_ON,ALWAYS_OFF,BREATHING,FLASHING,GRADUAL_ON,GRADUAL_OFF fingerprintdoor/led/command {"color":"$EVTPART0","effect":"$EVTPART1","speed":120, "count":0}\
RED:on,off,breathe,flash fingerprintdoor/led red_$EVTPART1\ PURPLE:ALWAYS_ON,ALWAYS_OFF,BREATHING,FLASHING,GRADUAL_ON,GRADUAL_OFF fingerprintdoor/led/command {"color":"$EVTPART0","effect":"$EVTPART1","speed":120, "count":0}\
PURPLE:on,off,breathe,flash fingerprintdoor/led purple_$EVTPART1\ WHITE:ALWAYS_ON,ALWAYS_OFF,BREATHING,FLASHING,GRADUAL_ON,GRADUAL_OFF fingerprintdoor/led/command {"color":"$EVTPART0","effect":"$EVTPART1","speed":120, "count":0}\
WHITE:on,off,breathe,flash fingerprintdoor/led white_$EVTPART1\ CYAN:ALWAYS_ON,ALWAYS_OFF,BREATHING,FLASHING,GRADUAL_ON,GRADUAL_OFF fingerprintdoor/led/command {"color":"$EVTPART0","effect":"$EVTPART1","speed":120, "count":0}\
CYAN:on,off,breathe,flash fingerprintdoor/led cyan_$EVTPART1\ YELLOW:ALWAYS_ON,ALWAYS_OFF,BREATHING,FLASHING,GRADUAL_ON,GRADUAL_OFF fingerprintdoor/led/command {"color":"$EVTPART0","effect":"$EVTPART1","speed":120, "count":0}\
YELLOW:on,off,breathe,flash fingerprintdoor/led yellow_$EVTPART1\ custom fingerprintdoor/led/command {"color":"$EVTPART1","effect":"$EVTPART2","speed":$EVTPART3, "count":$EVTPART4}\
cancel:noArg fingerprintdoor/cancel_enroll\ cancel:noArg fingerprintdoor/cancel_enroll\
enroll fingerprintdoor/enroll $EVTPART1\ enroll fingerprintdoor/enroll $EVTPART1\
delete fingerprintdoor/delete $EVTPART1\ delete fingerprintdoor/delete $EVTPART1\
@ -108,14 +108,15 @@ set myFingerprint delete 1
### Set Led color and effect ### Set Led color and effect
Syntax: Syntax:
```shell ```shell
set <fhem device name> <COLOR> <effect> set <fhem device name> <color> <effect> <speed> <count>
``` ```
Example: Example:
```shell ```shell
set myFingerprint BLUE flash set myFingerprint blue flash 120 3
``` ```
>Note: If `<count>=0` for effect "flashing" or "breathing" will run continously and can be interrupted
## Credits ## Credits

View File

@ -53,8 +53,131 @@ mqtt:
client_id: $devicename client_id: $devicename
id: mqtt_client id: mqtt_client
discovery: false discovery: false
on_message: !include "includes/led_effects.yaml" on_json_message:
topic: $devicename/led/command
then:
- if:
condition:
not:
# todo check json length
text_sensor.state:
id: fingerprint_state
state: "cancelling enrollment"
then:
# default values speed count
- text_sensor.template.publish:
id: mqtt_json
state: !lambda |-
std::string color = x["color"];
std::string effect = x["effect"];
std::string speed = x["speed"];
std::string count = x["count"];
return "Color:" + color + " Effekt:" + effect + " Speed:" + speed + " Count:" + count;
- fingerprint_grow.aura_led_control:
state: !lambda |-
std::string str = x["effect"];
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
uint8_t eff = 0x03;
if(str == "BREATHING")
{
eff = 0x01;
return eff;
}
else if (str == "FLASHING"){
eff = 0x02;
return eff;
}
else if (str == "ALWAYS_ON"){
eff = 0x03;
return eff;
}
else if (str == "ALWAYS_OFF"){
eff = 0x04;
return eff;
}
else if (str == "GRADUAL_ON"){
eff = 0x05;
return eff;
}
else if (str == "GRADUAL_OFF"){
eff = 0x06;
return eff;
}
return 0;
speed: !lambda return x["speed"];
color: !lambda |-
std::string str = x["color"];
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
uint8_t color = 0x01;
if(str == "RED")
{
color = 0x01;
return color;
}
else if (str == "BLUE"){
color = 0x02;
return color;
}
else if (str == "PURPLE"){
color = 0x03;
return color;
}
else if (str == "GREEN"){
color = 0x04;
return color;
}
else if (str == "YELLOW"){
color = 0x05;
return color;
}
else if (str == "CYAN"){
color = 0x06;
return color;
}
else if (str == "WHITE"){
color = 0x07;
return color;
}
return color;
count: !lambda return x["count"];
- text_sensor.template.publish:
id: led_color
state: !lambda |-
std::string str = x["effect"];
std::string color = x["color"];
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
if(str == "ALWAYS_OFF" || str == "GRADUAL_OFF")
{
return "off";
}
else
{
return color;
}
# show on webserver
- text_sensor.template.publish:
id: led_state
state: !lambda |-
std::string str = x["effect"];
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
if(str == "ALWAYS_OFF" || str == "GRADUAL_OFF")
{
return "off";
}
else
{
return "on";
}
- mqtt.publish_json:
topic: $devicename/led/
payload: |-
root["led_state"] = id(led_state).state;
root["color"] = x["color"];
# Fingerpint functions # Fingerpint functions
text_sensor: text_sensor:
@ -139,7 +262,19 @@ text_sensor:
- platform: template - platform: template
name: "Log Message" name: "Log Message"
id: fingerprint_state id: fingerprint_state
- platform: template
name: "MQTT Message"
id: mqtt_json
- platform: template
name: "LED color"
id: led_color
- platform: template
name: "LED state"
id: led_state
######################### #########################

View File

@ -1,251 +0,0 @@
- topic: $devicename/led
payload: green_on
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_ON
speed: $speed_gradual_on
color: GREEN
count: 0
- topic: $devicename/led
payload: green_off
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_OFF
speed: $speed_gradual_off
color: GREEN
count: 0
- topic: $devicename/led
payload: green_breathe
then:
- fingerprint_grow.aura_led_control:
state: BREATHING
speed: $speed_breath
color: GREEN
count: $count_breath
- topic: $devicename/led
payload: green_flash
then:
- fingerprint_grow.aura_led_control:
state: FLASHING
speed: $speed_flash
color: GREEN
count: $count_flash
- topic: $devicename/led
payload: red_on
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_ON
speed: $speed_gradual_on
color: RED
count: 0
- topic: $devicename/led
payload: red_off
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_OFF
speed: $speed_gradual_off
color: RED
count: 0
- topic: $devicename/led
payload: red_breathe
then:
- fingerprint_grow.aura_led_control:
state: BREATHING
speed: $speed_breath
color: RED
count: $count_breath
- topic: $devicename/led
payload: red_flash
then:
- fingerprint_grow.aura_led_control:
state: FLASHING
speed: $speed_flash
color: RED
count: $count_flash
- topic: $devicename/led
payload: purple_on
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_ON
speed: $speed_gradual_on
color: PURPLE
count: 0
- topic: $devicename/led
payload: purple_off
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_OFF
speed: $speed_breath
color: PURPLE
count: 0
- topic: $devicename/led
payload: purple_breathe
then:
- fingerprint_grow.aura_led_control:
state: BREATHING
speed: $speed_breath
color: PURPLE
count: $count_breath
- topic: $devicename/led
payload: purple_flash
then:
- fingerprint_grow.aura_led_control:
state: FLASHING
speed: $speed_flash
color: PURPLE
count: $count_flash
- topic: $devicename/led
payload: yellow_on
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_ON
speed: $speed_gradual_on
color: YELLOW
count: 0
- topic: $devicename/led
payload: yellow_off
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_OFF
speed: $speed_breath
color: YELLOW
count: 0
- topic: $devicename/led
payload: yellow_breathe
then:
- fingerprint_grow.aura_led_control:
state: BREATHING
speed: $speed_breath
color: YELLOW
count: $count_breath
- topic: $devicename/led
payload: yellow_flash
then:
- fingerprint_grow.aura_led_control:
state: FLASHING
speed: $speed_flash
color: YELLOW
count: $count_flash
- topic: $devicename/led
payload: cyan_on
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_ON
speed: $speed_gradual_on
color: CYAN
count: 0
- topic: $devicename/led
payload: cyan_off
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_OFF
speed: $speed_breath
color: CYAN
count: 0
- topic: $devicename/led
payload: cyan_breathe
then:
- fingerprint_grow.aura_led_control:
state: BREATHING
speed: $speed_breath
color: CYAN
count: $count_breath
- topic: $devicename/led
payload: cyan_flash
then:
- fingerprint_grow.aura_led_control:
state: FLASHING
speed: $speed_flash
color: CYAN
count: $count_flash
- topic: $devicename/led
payload: white_on
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_ON
speed: $speed_gradual_on
color: WHITE
count: 0
- topic: $devicename/led
payload: white_off
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_OFF
speed: $speed_breath
color: WHITE
count: 0
- topic: $devicename/led
payload: white_breathe
then:
- fingerprint_grow.aura_led_control:
state: BREATHING
speed: $speed_breath
color: WHITE
count: $count_breath
- topic: $devicename/led
payload: white_flash
then:
- fingerprint_grow.aura_led_control:
state: FLASHING
speed: $speed_flash
color: WHITE
count: $count_flash
- topic: $devicename/led
payload: blue_on
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_ON
speed: $speed_gradual_on
color: BLUE
count: 0
- topic: $devicename/led
payload: blue_off
then:
- fingerprint_grow.aura_led_control:
state: GRADUAL_OFF
speed: $speed_breath
color: BLUE
count: 0
- topic: $devicename/led
payload: blue_breathe
then:
- fingerprint_grow.aura_led_control:
state: BREATHING
speed: $speed_breath
color: BLUE
count: $count_breath
- topic: $devicename/led
payload: blue_flash
then:
- fingerprint_grow.aura_led_control:
state: FLASHING
speed: $speed_flash
color: BLUE
count: $count_flash