1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-18 09:43:47 +01:00

Merge branch 'fixed_vector_HomeassistantServiceMap' into integration

This commit is contained in:
J. Nick Koston
2025-10-13 19:28:21 -10:00
6 changed files with 28 additions and 34 deletions

View File

@@ -776,9 +776,9 @@ message HomeassistantActionRequest {
option (ifdef) = "USE_API_HOMEASSISTANT_SERVICES"; option (ifdef) = "USE_API_HOMEASSISTANT_SERVICES";
string service = 1; string service = 1;
repeated HomeassistantServiceMap data = 2; repeated HomeassistantServiceMap data = 2 [(fixed_vector) = true];
repeated HomeassistantServiceMap data_template = 3; repeated HomeassistantServiceMap data_template = 3 [(fixed_vector) = true];
repeated HomeassistantServiceMap variables = 4; repeated HomeassistantServiceMap variables = 4 [(fixed_vector) = true];
bool is_event = 5; bool is_event = 5;
uint32 call_id = 6 [(field_ifdef) = "USE_API_HOMEASSISTANT_ACTION_RESPONSES"]; uint32 call_id = 6 [(field_ifdef) = "USE_API_HOMEASSISTANT_ACTION_RESPONSES"];
bool wants_response = 7 [(field_ifdef) = "USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON"]; bool wants_response = 7 [(field_ifdef) = "USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON"];

View File

@@ -1110,9 +1110,9 @@ class HomeassistantActionRequest final : public ProtoMessage {
#endif #endif
StringRef service_ref_{}; StringRef service_ref_{};
void set_service(const StringRef &ref) { this->service_ref_ = ref; } void set_service(const StringRef &ref) { this->service_ref_ = ref; }
std::vector<HomeassistantServiceMap> data{}; FixedVector<HomeassistantServiceMap> data{};
std::vector<HomeassistantServiceMap> data_template{}; FixedVector<HomeassistantServiceMap> data_template{};
std::vector<HomeassistantServiceMap> variables{}; FixedVector<HomeassistantServiceMap> variables{};
bool is_event{false}; bool is_event{false};
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
uint32_t call_id{0}; uint32_t call_id{0};

View File

@@ -201,9 +201,9 @@ class CustomAPIDevice {
void call_homeassistant_service(const std::string &service_name, const std::map<std::string, std::string> &data) { void call_homeassistant_service(const std::string &service_name, const std::map<std::string, std::string> &data) {
HomeassistantActionRequest resp; HomeassistantActionRequest resp;
resp.set_service(StringRef(service_name)); resp.set_service(StringRef(service_name));
resp.data.init(data.size());
for (auto &it : data) { for (auto &it : data) {
resp.data.emplace_back(); auto &kv = resp.data.emplace_back();
auto &kv = resp.data.back();
kv.set_key(StringRef(it.first)); kv.set_key(StringRef(it.first));
kv.value = it.second; kv.value = it.second;
} }
@@ -244,9 +244,9 @@ class CustomAPIDevice {
HomeassistantActionRequest resp; HomeassistantActionRequest resp;
resp.set_service(StringRef(service_name)); resp.set_service(StringRef(service_name));
resp.is_event = true; resp.is_event = true;
resp.data.init(data.size());
for (auto &it : data) { for (auto &it : data) {
resp.data.emplace_back(); auto &kv = resp.data.emplace_back();
auto &kv = resp.data.back();
kv.set_key(StringRef(it.first)); kv.set_key(StringRef(it.first));
kv.value = it.second; kv.value = it.second;
} }

View File

@@ -122,29 +122,24 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
Trigger<std::string, Ts...> *get_error_trigger() const { return this->error_trigger_; } Trigger<std::string, Ts...> *get_error_trigger() const { return this->error_trigger_; }
#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES #endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
template<typename VectorType, typename SourceType>
static void populate_service_map_(VectorType &dest, SourceType &source, Ts... x) {
dest.init(source.size());
for (auto &it : source) {
auto &kv = dest.emplace_back();
kv.set_key(StringRef(it.key));
kv.value = it.value.value(x...);
}
}
void play(Ts... x) override { void play(Ts... x) override {
HomeassistantActionRequest resp; HomeassistantActionRequest resp;
std::string service_value = this->service_.value(x...); std::string service_value = this->service_.value(x...);
resp.set_service(StringRef(service_value)); resp.set_service(StringRef(service_value));
resp.is_event = this->flags_.is_event; resp.is_event = this->flags_.is_event;
for (auto &it : this->data_) { populate_service_map_(resp.data, this->data_, x...);
resp.data.emplace_back(); populate_service_map_(resp.data_template, this->data_template_, x...);
auto &kv = resp.data.back(); populate_service_map_(resp.variables, this->variables_, x...);
kv.set_key(StringRef(it.key));
kv.value = it.value.value(x...);
}
for (auto &it : this->data_template_) {
resp.data_template.emplace_back();
auto &kv = resp.data_template.back();
kv.set_key(StringRef(it.key));
kv.value = it.value.value(x...);
}
for (auto &it : this->variables_) {
resp.variables.emplace_back();
auto &kv = resp.variables.back();
kv.set_key(StringRef(it.key));
kv.value = it.value.value(x...);
}
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
if (this->flags_.wants_status) { if (this->flags_.wants_status) {

View File

@@ -90,13 +90,12 @@ void HomeassistantNumber::control(float value) {
api::HomeassistantActionRequest resp; api::HomeassistantActionRequest resp;
resp.set_service(SERVICE_NAME); resp.set_service(SERVICE_NAME);
resp.data.emplace_back(); resp.data.init(2);
auto &entity_id = resp.data.back(); auto &entity_id = resp.data.emplace_back();
entity_id.set_key(ENTITY_ID_KEY); entity_id.set_key(ENTITY_ID_KEY);
entity_id.value = this->entity_id_; entity_id.value = this->entity_id_;
resp.data.emplace_back(); auto &entity_value = resp.data.emplace_back();
auto &entity_value = resp.data.back();
entity_value.set_key(VALUE_KEY); entity_value.set_key(VALUE_KEY);
entity_value.value = to_string(value); entity_value.value = to_string(value);

View File

@@ -51,8 +51,8 @@ void HomeassistantSwitch::write_state(bool state) {
resp.set_service(SERVICE_OFF); resp.set_service(SERVICE_OFF);
} }
resp.data.emplace_back(); resp.data.init(1);
auto &entity_id_kv = resp.data.back(); auto &entity_id_kv = resp.data.emplace_back();
entity_id_kv.set_key(ENTITY_ID_KEY); entity_id_kv.set_key(ENTITY_ID_KEY);
entity_id_kv.value = this->entity_id_; entity_id_kv.value = this->entity_id_;