more fixes

This commit is contained in:
J. Nick Koston
2026-02-27 16:22:59 -10:00
parent 3bca31ba9f
commit 39df0f6e51
12 changed files with 62 additions and 53 deletions
@@ -80,7 +80,7 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff,
return false;
}
auto ibeacon = device.get_ibeacon().value();
auto ibeacon = *device.get_ibeacon();
if (this->ibeacon_uuid_ != ibeacon.get_uuid()) {
return false;
@@ -78,7 +78,7 @@ class BLERSSISensor : public sensor::Sensor, public esp32_ble_tracker::ESPBTDevi
return false;
}
auto ibeacon = device.get_ibeacon().value();
auto ibeacon = *device.get_ibeacon();
if (this->ibeacon_uuid_ != ibeacon.get_uuid()) {
return false;
@@ -29,10 +29,11 @@ class DemoAlarmControlPanel : public AlarmControlPanel, public Component {
protected:
void control(const AlarmControlPanelCall &call) override {
auto state = call.get_state().value_or(ACP_STATE_DISARMED);
auto code = call.get_code();
switch (state) {
case ACP_STATE_ARMED_AWAY:
if (this->get_requires_code_to_arm() && call.get_code().has_value()) {
if (call.get_code().value() != "1234") {
if (this->get_requires_code_to_arm() && code.has_value()) {
if (*code != "1234") {
this->status_momentary_error("invalid_code", 5000);
return;
}
@@ -40,8 +41,8 @@ class DemoAlarmControlPanel : public AlarmControlPanel, public Component {
this->publish_state(ACP_STATE_ARMED_AWAY);
break;
case ACP_STATE_DISARMED:
if (this->get_requires_code() && call.get_code().has_value()) {
if (call.get_code().value() != "1234") {
if (this->get_requires_code() && code.has_value()) {
if (*code != "1234") {
this->status_momentary_error("invalid_code", 5000);
return;
}
+23 -25
View File
@@ -45,33 +45,31 @@ class DemoClimate : public climate::Climate, public Component {
protected:
void control(const climate::ClimateCall &call) override {
if (call.get_mode().has_value()) {
this->mode = *call.get_mode();
}
if (call.get_target_temperature().has_value()) {
this->target_temperature = *call.get_target_temperature();
}
if (call.get_target_temperature_low().has_value()) {
this->target_temperature_low = *call.get_target_temperature_low();
}
if (call.get_target_temperature_high().has_value()) {
this->target_temperature_high = *call.get_target_temperature_high();
}
if (call.get_fan_mode().has_value()) {
this->set_fan_mode_(*call.get_fan_mode());
}
if (call.get_swing_mode().has_value()) {
this->swing_mode = *call.get_swing_mode();
}
if (call.has_custom_fan_mode()) {
auto mode = call.get_mode();
if (mode.has_value())
this->mode = *mode;
auto target_temperature = call.get_target_temperature();
if (target_temperature.has_value())
this->target_temperature = *target_temperature;
auto target_temperature_low = call.get_target_temperature_low();
if (target_temperature_low.has_value())
this->target_temperature_low = *target_temperature_low;
auto target_temperature_high = call.get_target_temperature_high();
if (target_temperature_high.has_value())
this->target_temperature_high = *target_temperature_high;
auto fan_mode = call.get_fan_mode();
if (fan_mode.has_value())
this->set_fan_mode_(*fan_mode);
auto swing_mode = call.get_swing_mode();
if (swing_mode.has_value())
this->swing_mode = *swing_mode;
if (call.has_custom_fan_mode())
this->set_custom_fan_mode_(call.get_custom_fan_mode());
}
if (call.get_preset().has_value()) {
this->set_preset_(*call.get_preset());
}
if (call.has_custom_preset()) {
auto preset = call.get_preset();
if (preset.has_value())
this->set_preset_(*preset);
if (call.has_custom_preset())
this->set_custom_preset_(call.get_custom_preset());
}
this->publish_state();
}
climate::ClimateTraits traits() override {
+6 -4
View File
@@ -38,8 +38,9 @@ class DemoCover : public cover::Cover, public Component {
protected:
void control(const cover::CoverCall &call) override {
if (call.get_position().has_value()) {
float target = *call.get_position();
auto pos = call.get_position();
if (pos.has_value()) {
float target = *pos;
this->current_operation =
target > this->position ? cover::COVER_OPERATION_OPENING : cover::COVER_OPERATION_CLOSING;
@@ -49,8 +50,9 @@ class DemoCover : public cover::Cover, public Component {
this->publish_state();
});
}
if (call.get_tilt().has_value()) {
this->tilt = *call.get_tilt();
auto tilt = call.get_tilt();
if (tilt.has_value()) {
this->tilt = *tilt;
}
if (call.get_stop()) {
this->cancel_timeout("move");
+12 -8
View File
@@ -47,14 +47,18 @@ class DemoFan : public fan::Fan, public Component {
protected:
void control(const fan::FanCall &call) override {
if (call.get_state().has_value())
this->state = *call.get_state();
if (call.get_oscillating().has_value())
this->oscillating = *call.get_oscillating();
if (call.get_speed().has_value())
this->speed = *call.get_speed();
if (call.get_direction().has_value())
this->direction = *call.get_direction();
auto state = call.get_state();
if (state.has_value())
this->state = *state;
auto oscillating = call.get_oscillating();
if (oscillating.has_value())
this->oscillating = *oscillating;
auto speed = call.get_speed();
if (speed.has_value())
this->speed = *speed;
auto direction = call.get_direction();
if (direction.has_value())
this->direction = *direction;
this->publish_state();
}
+3 -2
View File
@@ -8,8 +8,9 @@ namespace demo {
class DemoLock : public lock::Lock {
protected:
void control(const lock::LockCall &call) override {
auto state = *call.get_state();
this->publish_state(state);
auto state = call.get_state();
if (state.has_value())
this->publish_state(*state);
}
};
+7 -4
View File
@@ -26,12 +26,15 @@ class DemoValve : public valve::Valve {
protected:
void control(const valve::ValveCall &call) override {
if (call.get_position().has_value()) {
this->position = *call.get_position();
auto pos = call.get_position();
if (pos.has_value()) {
this->position = *pos;
this->publish_state();
return;
} else if (call.get_toggle().has_value()) {
if (call.get_toggle().value()) {
}
auto toggle = call.get_toggle();
if (toggle.has_value()) {
if (*toggle) {
if (this->position == valve::VALVE_OPEN) {
this->position = valve::VALVE_CLOSED;
this->publish_state();
@@ -107,7 +107,7 @@ class ESPBTDevice {
for (auto &it : this->manufacturer_datas_) {
auto res = ESPBLEiBeacon::from_manufacturer_data(it);
if (res.has_value())
return *res;
return res;
}
return {};
}
+1 -1
View File
@@ -252,7 +252,7 @@ void log_entity_unit_of_measurement(const char *tag, const char *prefix, const E
template<typename T> class StatefulEntityBase : public EntityBase {
public:
virtual bool has_state() const { return this->state_.has_value(); }
virtual const T &get_state() const { return this->state_.value(); }
virtual const T &get_state() const { return *this->state_; }
virtual T get_state_default(T default_value) const { return this->state_.value_or(default_value); }
void invalidate_state() { this->set_new_state({}); }
+1 -1
View File
@@ -66,5 +66,5 @@ def test_text_config_lamda_is_set(generate_main):
main_cpp = generate_main("tests/component_tests/text/test_text.yaml")
# Then
assert "it_4->set_template([]() -> esphome::optional<std::string> {" in main_cpp
assert "it_4->set_template([]() -> std::optional<std::string> {" in main_cpp
assert 'return std::string{"Hello"};' in main_cpp
+1 -1
View File
@@ -28,7 +28,7 @@ esphome:
# Test C++ API: set_template() with stateless lambda (no captures)
# NOTE: set_template() is not intended to be a public API, but we test it to ensure it doesn't break.
- lambda: |-
id(template_sens).set_template([]() -> esphome::optional<float> {
id(template_sens).set_template([]() -> std::optional<float> {
return 123.0f;
});