mirror of
https://github.com/esphome/esphome.git
synced 2025-07-27 05:36:38 +00:00
preen
This commit is contained in:
parent
b8e326eb01
commit
0534d1bfcf
@ -83,17 +83,21 @@ void HomeassistantNumber::control(float value) {
|
|||||||
|
|
||||||
this->publish_state(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;
|
api::HomeassistantServiceResponse resp;
|
||||||
resp.set_service(StringRef("number.set_value"));
|
resp.set_service(SERVICE_NAME);
|
||||||
|
|
||||||
resp.data.emplace_back();
|
resp.data.emplace_back();
|
||||||
auto &entity_id = resp.data.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_));
|
entity_id.set_value(StringRef(this->entity_id_));
|
||||||
|
|
||||||
resp.data.emplace_back();
|
resp.data.emplace_back();
|
||||||
auto &entity_value = resp.data.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);
|
std::string value_str = to_string(value);
|
||||||
entity_value.set_value(StringRef(value_str));
|
entity_value.set_value(StringRef(value_str));
|
||||||
|
|
||||||
|
@ -40,16 +40,20 @@ void HomeassistantSwitch::write_state(bool state) {
|
|||||||
return;
|
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;
|
api::HomeassistantServiceResponse resp;
|
||||||
if (state) {
|
if (state) {
|
||||||
resp.set_service(StringRef("homeassistant.turn_on"));
|
resp.set_service(SERVICE_ON);
|
||||||
} else {
|
} else {
|
||||||
resp.set_service(StringRef("homeassistant.turn_off"));
|
resp.set_service(SERVICE_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
resp.data.emplace_back();
|
resp.data.emplace_back();
|
||||||
auto &entity_id_kv = resp.data.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_));
|
entity_id_kv.set_value(StringRef(this->entity_id_));
|
||||||
|
|
||||||
api::global_api_server->send_homeassistant_service_call(resp);
|
api::global_api_server->send_homeassistant_service_call(resp);
|
||||||
|
@ -55,10 +55,11 @@ class EntityBase {
|
|||||||
std::string get_icon() const;
|
std::string get_icon() const;
|
||||||
void set_icon(const char *icon);
|
void set_icon(const char *icon);
|
||||||
StringRef get_icon_ref() const {
|
StringRef get_icon_ref() const {
|
||||||
|
static constexpr auto EMPTY_STRING = StringRef::from_lit("");
|
||||||
#ifdef USE_ENTITY_ICON
|
#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
|
#else
|
||||||
return StringRef("");
|
return EMPTY_STRING;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +115,8 @@ class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming)
|
|||||||
void set_device_class(const char *device_class);
|
void set_device_class(const char *device_class);
|
||||||
/// Get the device class as StringRef
|
/// Get the device class as StringRef
|
||||||
StringRef get_device_class_ref() const {
|
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:
|
protected:
|
||||||
@ -129,7 +131,8 @@ class EntityBase_UnitOfMeasurement { // NOLINT(readability-identifier-naming)
|
|||||||
void set_unit_of_measurement(const char *unit_of_measurement);
|
void set_unit_of_measurement(const char *unit_of_measurement);
|
||||||
/// Get the unit of measurement as StringRef
|
/// Get the unit of measurement as StringRef
|
||||||
StringRef get_unit_of_measurement_ref() const {
|
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:
|
protected:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user