HW button bar in all pages

Partially solves #1835
Partially solves #1517
Solves #1507
This commit is contained in:
Edward Firmo
2024-02-26 09:38:20 +01:00
parent abd55b4bc0
commit f2a1f27e0c
6 changed files with 939 additions and 1262 deletions

View File

@@ -0,0 +1,36 @@
// ha_components.h
#pragma once
#include <string>
namespace nspanel_ha_blueprint {
struct HomeAssistantEntity {
std::string domain;
std::string id;
};
/**
* Extracts the domain name and unique ID from a given Home Assistant entity string.
* Handles a special case where "alarm_control_panel" should be shortened to "alarm".
*
* @param entity_id The input string containing either the combined domain and unique ID or just the unique ID.
* @return A HomeAssistantEntity struct containing the extracted domain and the unique ID.
*/
HomeAssistantEntity extractHomeAssistantEntity(const std::string& entity_id) {
size_t dotPos = input.find(".");
HomeAssistantEntity result;
if (dotPos != std::string::npos) {
// Extract domain and id from the input string
result.domain = input.substr(0, dotPos);
result.id = input.substr(dotPos + 1);
} else {
// No dot found, the entire input is considered as id.
result.domain = "invalid";
result.id = input;
}
return result;
}
} // namespace nspanel_ha_blueprint

View File

@@ -1,10 +1,38 @@
// relays.h
// hardware.h
#pragma once
#include <cstdint>
namespace nspanel_ha_blueprint {
/**
* @enum ButtonsSettings
* Represents the settings for hardware buttons as individual bits within a uint8_t value.
* Each enum value corresponds to a specific bit position that represents a distinct setting
* for the buttons. This allows for efficient storage and manipulation of multiple buttons
* settings within a single byte.
*
* Bits are allocated as follows:
* - Bit 0: Left button - Enabled.
* - Bit 1: Left button - Current state (0 for `off` or 1 for `on`)
* - Bits 2-3: Reserved for future use.
* - Bit 4: Right button - Enabled.
* - Bit 5: Right button - Current state (0 for `off` or 1 for `on`)
* - Bits 6-7: Reserved for future use.
*
* Usage involves bitwise operations to set, clear, and check these settings within a
* uint8_t variable. This approach enables compact representation and easy manipulation
* of relay settings.
*/
enum ButtonSettings {
ButtonLeft_Enabled = 1 << 0, ///< Bit 0: Enables left button visualization on screen.
ButtonLeft_State = 1 << 1, ///< Bit 1: Current state of left button.
// Bits 2 and 3 are reserved for future expansion.
ButtonRight_Enabled = 1 << 4, ///< Bit 4: Enables right button visualization on screen.
ButtonRight_State = 1 << 5, ///< Bit 5: Current state of right button.
// Bits 6 and 7 are reserved for future expansion.
};
/**
* @enum RelaySettings
* Represents the settings for relays as individual bits within a uint8_t value. Each
@@ -33,7 +61,7 @@ namespace nspanel_ha_blueprint {
// Bits 6 and 7 are reserved for future expansion.
};
void update_relay_setting(uint8_t& settings, bool condition, RelaySettings flag) {
void update_bitwise_setting(uint8_t& settings, bool condition, RelaySettings flag) {
if (condition) {
settings |= flag; // Set bit
} else {

View File

@@ -4,7 +4,9 @@
#include <array>
#include <cstring>
#include <cstdint>
#include <utility> // For std::pair
#include <utility>
#include <string>
#include <initializer_list>
namespace nspanel_ha_blueprint {
@@ -65,4 +67,42 @@ namespace nspanel_ha_blueprint {
return UINT8_MAX; // Return UINT8_MAX if not found
}
} // namespace nspanel_ha_blueprint
/**
* Checks if a given string is present within a list of strings.
*
* This function provides a convenient way to search for the presence of a specific string
* within a variably sized list of strings. It iterates through each string in the provided
* list, comparing it against the target string for an exact match. This utility is particularly
* useful for validating if a certain value matches any item from a predefined set of allowed
* values.
*
* @param strToSearch The string to search for within the list. This is the target string
* that the function will attempt to find an exact match for within the
* provided list of strings.
* @param list An initializer list of strings to search within. This list contains the strings
* against which the target string will be compared. The list is flexible in size,
* allowing for a variable number of strings to be specified.
*
* @return Returns `true` if the target string is found within the list, indicating an exact
* match was encountered. Returns `false` if the target string is not present in the
* list, indicating no matches were found.
*
* Usage Example:
* ```cpp
* std::string page = "alarm";
* bool isPresent = isStringInList(page, {"alarm", "climate", "cover", "fan", "light", "media_player", "confirm", "keyb_num"});
* if (!isPresent) {
* // The string 'page' was not found in the list
* }
* ```
*/
bool isStringInList(const std::string& strToSearch, std::initializer_list<std::string> list) {
for (const auto& str : list) {
if (strToSearch == str) {
return true;
}
}
return false;
}
} // namespace nspanel_ha_blueprint