Skip to content

Commit bfe05e7

Browse files
committed
Validate Command Code Inputs
1 parent e7ee382 commit bfe05e7

File tree

2 files changed

+108
-43
lines changed

2 files changed

+108
-43
lines changed

docs/firmware/additions.md

+17-8
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,26 @@ globals: // [!code ++] // [!code focus]
102102
103103
text: // [!code ++] // [!code focus]
104104
- platform: template // [!code ++] // [!code focus]
105+
id: custom_command_input // [!code ++] // [!code focus]
105106
name: Custom Command // [!code ++] // [!code focus]
106-
optimistic: true // [!code ++] // [!code focus]
107+
entity_category: CONFIG // [!code ++] // [!code focus]
108+
icon: "mdi:console-network" // [!code ++] // [!code focus]
107109
mode: text // [!code ++] // [!code focus]
108-
restore_value: true // [!code ++] // [!code focus]
109-
initial_value: '3b8f9a00' // [!code ++] // [!code focus]
110-
on_value: // [!code ++] // [!code focus]
110+
lambda: |- // [!code ++] // [!code focus]
111+
unsigned long number = id(custom_command); // [!code ++] // [!code focus]
112+
return str_upper_case(format_hex(number)); // [!code ++] // [!code focus]
113+
set_action: // [!code ++] // [!code focus]
111114
then: // [!code ++] // [!code focus]
112115
- lambda: |- // [!code ++] // [!code focus]
113-
id(custom_command) = std::stoi(x.c_str(), nullptr, 16); // [!code ++] // [!code focus]
114-
entity_category: CONFIG // [!code ++] // [!code focus]
115-
icon: "mdi:console-network" // [!code ++] // [!code focus]
116+
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end()); // [!code ++] // [!code focus]
117+
x.erase(0, x.find_first_not_of('0')); // [!code ++] // [!code focus]
118+
x.resize(8); // [!code ++] // [!code focus]
119+
unsigned long number = 0; // [!code ++] // [!code focus]
120+
if(!x.empty()) { // [!code ++] // [!code focus]
121+
number = std::stoul(x.c_str(), nullptr, 16); // [!code ++] // [!code focus]
122+
} // [!code ++] // [!code focus]
123+
id(custom_command) = number; // [!code ++] // [!code focus]
124+
id(custom_command_input)->publish_state(str_upper_case(format_hex(number))); // [!code ++] // [!code focus]
116125
117126
binary_sensor: // [!code ++] // [!code focus]
118127
- platform: tcs_intercom // [!code ++] // [!code focus]
@@ -136,7 +145,7 @@ sensor: // [!code ++] // [!code focus]
136145
number: GPIO9 // [!code ++] // [!code focus]
137146
allow_other_uses: true // [!code ++] // [!code focus]
138147
update_interval: 500ms // [!code ++] // [!code focus]
139-
attenuation: 11dB // [!code ++] // [!code focus]
148+
attenuation: 12dB // [!code ++] // [!code focus]
140149
141150
# Extend tcs_intercom component // [!code ++] // [!code focus]
142151
# Allow RX pin to be used for other cases as well // [!code ++] // [!code focus]

firmware/base.yaml

+91-35
Original file line numberDiff line numberDiff line change
@@ -510,101 +510,157 @@ text:
510510
- platform: template
511511
id: open_entrance_door_command_input
512512
name: "Command: Open Entrance Door"
513-
optimistic: true
514513
mode: text
515-
restore_value: true
516-
initial_value: '00001100'
517-
on_value:
514+
lambda: |-
515+
unsigned long number = id(first_door_opener_command);
516+
return str_upper_case(format_hex(number));
517+
set_action:
518518
then:
519519
- lambda: |-
520-
id(first_door_opener_command) = std::stoi(x.c_str(), nullptr, 16);
520+
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
521+
x.erase(0, x.find_first_not_of('0'));
522+
x.resize(8);
523+
unsigned long number = 0;
524+
if(!x.empty()) {
525+
number = std::stoul(x.c_str(), nullptr, 16);
526+
}
527+
id(first_door_opener_command) = number;
528+
id(open_entrance_door_command_input)->publish_state(str_upper_case(format_hex(number)));
521529
entity_category: CONFIG
522530
icon: "mdi:console-network"
523531

524532
- platform: template
525533
id: open_second_door_command_input
526534
name: "Command: Open Second Door"
527-
optimistic: true
528535
mode: text
529-
restore_value: true
530-
initial_value: '00001100'
531-
on_value:
536+
lambda: |-
537+
unsigned long number = id(second_door_opener_command);
538+
return str_upper_case(format_hex(number));
539+
set_action:
532540
then:
533541
- lambda: |-
534-
id(second_door_opener_command) = std::stoi(x.c_str(), nullptr, 16);
542+
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
543+
x.erase(0, x.find_first_not_of('0'));
544+
x.resize(8);
545+
unsigned long number = 0;
546+
if(!x.empty()) {
547+
number = std::stoul(x.c_str(), nullptr, 16);
548+
}
549+
id(second_door_opener_command) = number;
550+
id(open_second_door_command_input)->publish_state(str_upper_case(format_hex(number)));
535551
disabled_by_default: true
536552
entity_category: CONFIG
537553
icon: "mdi:console-network"
538554

539555
- platform: template
540556
id: entrance_doorbell_command_input
541557
name: "Command: Entrance Doorbell"
542-
optimistic: true
543558
mode: text
544-
restore_value: true
545-
initial_value: '00000000'
546-
on_value:
559+
lambda: |-
560+
unsigned long number = id(entrance_doorbell_command);
561+
return str_upper_case(format_hex(number));
562+
set_action:
547563
then:
548564
- lambda: |-
549-
id(entrance_doorbell_command) = std::stoi(x.c_str(), nullptr, 16);
565+
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
566+
x.erase(0, x.find_first_not_of('0'));
567+
x.resize(8);
568+
unsigned long number = 0;
569+
if(!x.empty()) {
570+
number = std::stoul(x.c_str(), nullptr, 16);
571+
}
572+
id(entrance_doorbell_command) = number;
573+
id(entrance_doorbell_command_input)->publish_state(str_upper_case(format_hex(number)));
550574
entity_category: CONFIG
551575
icon: "mdi:console-network"
552576

553577
- platform: template
554578
id: apartment_doorbell_command_input
555579
name: "Command: Apartment Doorbell"
556-
optimistic: true
557580
mode: text
558-
restore_value: true
559-
initial_value: '00000000'
560-
on_value:
581+
lambda: |-
582+
unsigned long number = id(apartment_doorbell_command);
583+
return str_upper_case(format_hex(number));
584+
set_action:
561585
then:
562586
- lambda: |-
563-
id(apartment_doorbell_command) = std::stoi(x.c_str(), nullptr, 16);
587+
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
588+
x.erase(0, x.find_first_not_of('0'));
589+
x.resize(8);
590+
unsigned long number = 0;
591+
if(!x.empty()) {
592+
number = std::stoul(x.c_str(), nullptr, 16);
593+
}
594+
id(apartment_doorbell_command) = number;
595+
id(apartment_doorbell_command_input)->publish_state(str_upper_case(format_hex(number)));
564596
entity_category: CONFIG
565597
icon: "mdi:console-network"
566598

567599
- platform: template
568600
id: pick_up_phone_command_input
569601
name: "Command: Pick up phone"
570-
optimistic: true
571602
mode: text
572-
restore_value: true
573-
initial_value: '00000000'
574-
on_value:
603+
lambda: |-
604+
unsigned long number = id(pick_up_phone_command);
605+
return str_upper_case(format_hex(number));
606+
set_action:
575607
then:
576608
- lambda: |-
577-
id(pick_up_phone_command) = std::stoi(x.c_str(), nullptr, 16);
609+
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
610+
x.erase(0, x.find_first_not_of('0'));
611+
x.resize(8);
612+
unsigned long number = 0;
613+
if(!x.empty()) {
614+
number = std::stoul(x.c_str(), nullptr, 16);
615+
}
616+
id(pick_up_phone_command) = number;
617+
id(pick_up_phone_command_input)->publish_state(str_upper_case(format_hex(number)));
578618
disabled_by_default: true
579619
entity_category: CONFIG
580620
icon: "mdi:console-network"
581621

582622
- platform: template
583623
id: hang_up_command_input
584624
name: "Command: Hang up phone"
585-
optimistic: true
586625
mode: text
587-
restore_value: true
588-
initial_value: '00003000'
589-
on_value:
626+
lambda: |-
627+
unsigned long number = id(hang_up_phone_command);
628+
return str_upper_case(format_hex(number));
629+
set_action:
590630
then:
591631
- lambda: |-
592-
id(hang_up_phone_command) = std::stoi(x.c_str(), nullptr, 16);
632+
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
633+
x.erase(0, x.find_first_not_of('0'));
634+
x.resize(8);
635+
unsigned long number = 0;
636+
if(!x.empty()) {
637+
number = std::stoul(x.c_str(), nullptr, 16);
638+
}
639+
id(hang_up_phone_command) = number;
640+
id(hang_up_command_input)->publish_state(str_upper_case(format_hex(number)));
593641
disabled_by_default: true
594642
entity_category: CONFIG
595643
icon: "mdi:console-network"
596644

597645
- platform: template
598646
id: ring_to_open_toggle_command_input
599647
name: "Command: Toggle Ring To Open"
600-
optimistic: true
601648
mode: text
602-
restore_value: true
603-
initial_value: '00000000'
604-
on_value:
649+
lambda: |-
650+
unsigned long number = id(ring_to_open_toggle_command);
651+
return str_upper_case(format_hex(number));
652+
set_action:
605653
then:
606654
- lambda: |-
607-
id(ring_to_open_toggle_command) = std::stoi(x.c_str(), nullptr, 16);
655+
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
656+
x.erase(0, x.find_first_not_of('0'));
657+
x.resize(8);
658+
unsigned long number = 0;
659+
if(!x.empty()) {
660+
number = std::stoul(x.c_str(), nullptr, 16);
661+
}
662+
id(ring_to_open_toggle_command) = number;
663+
id(ring_to_open_toggle_command_input)->publish_state(str_upper_case(format_hex(number)));
608664
disabled_by_default: true
609665
entity_category: CONFIG
610666
icon: "mdi:console-network"

0 commit comments

Comments
 (0)