Skip to content

Commit

Permalink
Validate Command Code Inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Aug 13, 2024
1 parent e7ee382 commit bfe05e7
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 43 deletions.
25 changes: 17 additions & 8 deletions docs/firmware/additions.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,26 @@ globals: // [!code ++] // [!code focus]
text: // [!code ++] // [!code focus]
- platform: template // [!code ++] // [!code focus]
id: custom_command_input // [!code ++] // [!code focus]
name: Custom Command // [!code ++] // [!code focus]
optimistic: true // [!code ++] // [!code focus]
entity_category: CONFIG // [!code ++] // [!code focus]
icon: "mdi:console-network" // [!code ++] // [!code focus]
mode: text // [!code ++] // [!code focus]
restore_value: true // [!code ++] // [!code focus]
initial_value: '3b8f9a00' // [!code ++] // [!code focus]
on_value: // [!code ++] // [!code focus]
lambda: |- // [!code ++] // [!code focus]
unsigned long number = id(custom_command); // [!code ++] // [!code focus]
return str_upper_case(format_hex(number)); // [!code ++] // [!code focus]
set_action: // [!code ++] // [!code focus]
then: // [!code ++] // [!code focus]
- lambda: |- // [!code ++] // [!code focus]
id(custom_command) = std::stoi(x.c_str(), nullptr, 16); // [!code ++] // [!code focus]
entity_category: CONFIG // [!code ++] // [!code focus]
icon: "mdi:console-network" // [!code ++] // [!code focus]
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end()); // [!code ++] // [!code focus]
x.erase(0, x.find_first_not_of('0')); // [!code ++] // [!code focus]
x.resize(8); // [!code ++] // [!code focus]
unsigned long number = 0; // [!code ++] // [!code focus]
if(!x.empty()) { // [!code ++] // [!code focus]
number = std::stoul(x.c_str(), nullptr, 16); // [!code ++] // [!code focus]
} // [!code ++] // [!code focus]
id(custom_command) = number; // [!code ++] // [!code focus]
id(custom_command_input)->publish_state(str_upper_case(format_hex(number))); // [!code ++] // [!code focus]
binary_sensor: // [!code ++] // [!code focus]
- platform: tcs_intercom // [!code ++] // [!code focus]
Expand All @@ -136,7 +145,7 @@ sensor: // [!code ++] // [!code focus]
number: GPIO9 // [!code ++] // [!code focus]
allow_other_uses: true // [!code ++] // [!code focus]
update_interval: 500ms // [!code ++] // [!code focus]
attenuation: 11dB // [!code ++] // [!code focus]
attenuation: 12dB // [!code ++] // [!code focus]
# Extend tcs_intercom component // [!code ++] // [!code focus]
# Allow RX pin to be used for other cases as well // [!code ++] // [!code focus]
Expand Down
126 changes: 91 additions & 35 deletions firmware/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -510,101 +510,157 @@ text:
- platform: template
id: open_entrance_door_command_input
name: "Command: Open Entrance Door"
optimistic: true
mode: text
restore_value: true
initial_value: '00001100'
on_value:
lambda: |-
unsigned long number = id(first_door_opener_command);
return str_upper_case(format_hex(number));
set_action:
then:
- lambda: |-
id(first_door_opener_command) = std::stoi(x.c_str(), nullptr, 16);
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
x.erase(0, x.find_first_not_of('0'));
x.resize(8);
unsigned long number = 0;
if(!x.empty()) {
number = std::stoul(x.c_str(), nullptr, 16);
}
id(first_door_opener_command) = number;
id(open_entrance_door_command_input)->publish_state(str_upper_case(format_hex(number)));
entity_category: CONFIG
icon: "mdi:console-network"

- platform: template
id: open_second_door_command_input
name: "Command: Open Second Door"
optimistic: true
mode: text
restore_value: true
initial_value: '00001100'
on_value:
lambda: |-
unsigned long number = id(second_door_opener_command);
return str_upper_case(format_hex(number));
set_action:
then:
- lambda: |-
id(second_door_opener_command) = std::stoi(x.c_str(), nullptr, 16);
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
x.erase(0, x.find_first_not_of('0'));
x.resize(8);
unsigned long number = 0;
if(!x.empty()) {
number = std::stoul(x.c_str(), nullptr, 16);
}
id(second_door_opener_command) = number;
id(open_second_door_command_input)->publish_state(str_upper_case(format_hex(number)));
disabled_by_default: true
entity_category: CONFIG
icon: "mdi:console-network"

- platform: template
id: entrance_doorbell_command_input
name: "Command: Entrance Doorbell"
optimistic: true
mode: text
restore_value: true
initial_value: '00000000'
on_value:
lambda: |-
unsigned long number = id(entrance_doorbell_command);
return str_upper_case(format_hex(number));
set_action:
then:
- lambda: |-
id(entrance_doorbell_command) = std::stoi(x.c_str(), nullptr, 16);
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
x.erase(0, x.find_first_not_of('0'));
x.resize(8);
unsigned long number = 0;
if(!x.empty()) {
number = std::stoul(x.c_str(), nullptr, 16);
}
id(entrance_doorbell_command) = number;
id(entrance_doorbell_command_input)->publish_state(str_upper_case(format_hex(number)));
entity_category: CONFIG
icon: "mdi:console-network"

- platform: template
id: apartment_doorbell_command_input
name: "Command: Apartment Doorbell"
optimistic: true
mode: text
restore_value: true
initial_value: '00000000'
on_value:
lambda: |-
unsigned long number = id(apartment_doorbell_command);
return str_upper_case(format_hex(number));
set_action:
then:
- lambda: |-
id(apartment_doorbell_command) = std::stoi(x.c_str(), nullptr, 16);
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
x.erase(0, x.find_first_not_of('0'));
x.resize(8);
unsigned long number = 0;
if(!x.empty()) {
number = std::stoul(x.c_str(), nullptr, 16);
}
id(apartment_doorbell_command) = number;
id(apartment_doorbell_command_input)->publish_state(str_upper_case(format_hex(number)));
entity_category: CONFIG
icon: "mdi:console-network"

- platform: template
id: pick_up_phone_command_input
name: "Command: Pick up phone"
optimistic: true
mode: text
restore_value: true
initial_value: '00000000'
on_value:
lambda: |-
unsigned long number = id(pick_up_phone_command);
return str_upper_case(format_hex(number));
set_action:
then:
- lambda: |-
id(pick_up_phone_command) = std::stoi(x.c_str(), nullptr, 16);
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
x.erase(0, x.find_first_not_of('0'));
x.resize(8);
unsigned long number = 0;
if(!x.empty()) {
number = std::stoul(x.c_str(), nullptr, 16);
}
id(pick_up_phone_command) = number;
id(pick_up_phone_command_input)->publish_state(str_upper_case(format_hex(number)));
disabled_by_default: true
entity_category: CONFIG
icon: "mdi:console-network"

- platform: template
id: hang_up_command_input
name: "Command: Hang up phone"
optimistic: true
mode: text
restore_value: true
initial_value: '00003000'
on_value:
lambda: |-
unsigned long number = id(hang_up_phone_command);
return str_upper_case(format_hex(number));
set_action:
then:
- lambda: |-
id(hang_up_phone_command) = std::stoi(x.c_str(), nullptr, 16);
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
x.erase(0, x.find_first_not_of('0'));
x.resize(8);
unsigned long number = 0;
if(!x.empty()) {
number = std::stoul(x.c_str(), nullptr, 16);
}
id(hang_up_phone_command) = number;
id(hang_up_command_input)->publish_state(str_upper_case(format_hex(number)));
disabled_by_default: true
entity_category: CONFIG
icon: "mdi:console-network"

- platform: template
id: ring_to_open_toggle_command_input
name: "Command: Toggle Ring To Open"
optimistic: true
mode: text
restore_value: true
initial_value: '00000000'
on_value:
lambda: |-
unsigned long number = id(ring_to_open_toggle_command);
return str_upper_case(format_hex(number));
set_action:
then:
- lambda: |-
id(ring_to_open_toggle_command) = std::stoi(x.c_str(), nullptr, 16);
x.erase(std::remove_if(x.begin(), x.end(), [](char c) { return !std::isxdigit(c); }), x.end());
x.erase(0, x.find_first_not_of('0'));
x.resize(8);
unsigned long number = 0;
if(!x.empty()) {
number = std::stoul(x.c_str(), nullptr, 16);
}
id(ring_to_open_toggle_command) = number;
id(ring_to_open_toggle_command_input)->publish_state(str_upper_case(format_hex(number)));
disabled_by_default: true
entity_category: CONFIG
icon: "mdi:console-network"
Expand Down

0 comments on commit bfe05e7

Please sign in to comment.