This commit is contained in:
J. Nick Koston 2025-07-21 21:46:37 -10:00
parent b8e326eb01
commit 0534d1bfcf
No known key found for this signature in database
3 changed files with 21 additions and 10 deletions

View File

@ -83,17 +83,21 @@ void HomeassistantNumber::control(float value) {
this->publish_state(value);
static constexpr auto SERVICE_NAME = StringRef::from_lit("number.set_value");
static constexpr auto ENTITY_ID_KEY = StringRef::from_lit("entity_id");
static constexpr auto VALUE_KEY = StringRef::from_lit("value");
api::HomeassistantServiceResponse resp;
resp.set_service(StringRef("number.set_value"));
resp.set_service(SERVICE_NAME);
resp.data.emplace_back();
auto &entity_id = resp.data.back();
entity_id.set_key(StringRef("entity_id"));
entity_id.set_key(ENTITY_ID_KEY);
entity_id.set_value(StringRef(this->entity_id_));
resp.data.emplace_back();
auto &entity_value = resp.data.back();
entity_value.set_key(StringRef("value"));
entity_value.set_key(VALUE_KEY);
std::string value_str = to_string(value);
entity_value.set_value(StringRef(value_str));

View File

@ -40,16 +40,20 @@ void HomeassistantSwitch::write_state(bool state) {
return;
}
static constexpr auto SERVICE_ON = StringRef::from_lit("homeassistant.turn_on");
static constexpr auto SERVICE_OFF = StringRef::from_lit("homeassistant.turn_off");
static constexpr auto ENTITY_ID_KEY = StringRef::from_lit("entity_id");
api::HomeassistantServiceResponse resp;
if (state) {
resp.set_service(StringRef("homeassistant.turn_on"));
resp.set_service(SERVICE_ON);
} else {
resp.set_service(StringRef("homeassistant.turn_off"));
resp.set_service(SERVICE_OFF);
}
resp.data.emplace_back();
auto &entity_id_kv = resp.data.back();
entity_id_kv.set_key(StringRef("entity_id"));
entity_id_kv.set_key(ENTITY_ID_KEY);
entity_id_kv.set_value(StringRef(this->entity_id_));
api::global_api_server->send_homeassistant_service_call(resp);

View File

@ -55,10 +55,11 @@ class EntityBase {
std::string get_icon() const;
void set_icon(const char *icon);
StringRef get_icon_ref() const {
static constexpr auto EMPTY_STRING = StringRef::from_lit("");
#ifdef USE_ENTITY_ICON
return this->icon_c_str_ == nullptr ? StringRef("") : StringRef(this->icon_c_str_);
return this->icon_c_str_ == nullptr ? EMPTY_STRING : StringRef(this->icon_c_str_);
#else
return StringRef("");
return EMPTY_STRING;
#endif
}
@ -114,7 +115,8 @@ class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming)
void set_device_class(const char *device_class);
/// Get the device class as StringRef
StringRef get_device_class_ref() const {
return this->device_class_ == nullptr ? StringRef("") : StringRef(this->device_class_);
static constexpr auto EMPTY_STRING = StringRef::from_lit("");
return this->device_class_ == nullptr ? EMPTY_STRING : StringRef(this->device_class_);
}
protected:
@ -129,7 +131,8 @@ class EntityBase_UnitOfMeasurement { // NOLINT(readability-identifier-naming)
void set_unit_of_measurement(const char *unit_of_measurement);
/// Get the unit of measurement as StringRef
StringRef get_unit_of_measurement_ref() const {
return this->unit_of_measurement_ == nullptr ? StringRef("") : StringRef(this->unit_of_measurement_);
static constexpr auto EMPTY_STRING = StringRef::from_lit("");
return this->unit_of_measurement_ == nullptr ? EMPTY_STRING : StringRef(this->unit_of_measurement_);
}
protected: