mirror of
https://github.com/esphome/esphome.git
synced 2025-10-06 20:03:46 +01:00
Mqtt topics to support numeric fan speed (#1859)
* numeric speed added * when dumping config for MQTT components log a note when skipped due to is_internal * added new topics to paython code validation/generation * reformatted with black * formatting corrected * use dump_config_ mechanism to skip internal components * use dump_config_ mechanism to skip internal components * style issues resolved * do_dump_config removed * formatting fixed * formatting fixed * Drop parent dump_config() calls Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
@@ -16,6 +16,7 @@ MQTTFanComponent::MQTTFanComponent(FanState *state) : MQTTComponent(), state_(st
|
||||
|
||||
FanState *MQTTFanComponent::get_state() const { return this->state_; }
|
||||
std::string MQTTFanComponent::component_type() const { return "fan"; }
|
||||
|
||||
void MQTTFanComponent::setup() {
|
||||
this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
|
||||
auto val = parse_on_off(payload.c_str());
|
||||
@@ -64,6 +65,26 @@ void MQTTFanComponent::setup() {
|
||||
});
|
||||
}
|
||||
|
||||
if (this->state_->get_traits().supports_speed()) {
|
||||
this->subscribe(this->get_speed_level_command_topic(),
|
||||
[this](const std::string &topic, const std::string &payload) {
|
||||
optional<int> speed_level_opt = parse_int(payload);
|
||||
if (speed_level_opt.has_value()) {
|
||||
const int speed_level = speed_level_opt.value();
|
||||
if (speed_level >= 0 && speed_level <= this->state_->get_traits().supported_speed_count()) {
|
||||
ESP_LOGD(TAG, "New speed level %d", speed_level);
|
||||
this->state_->make_call().set_speed(speed_level).perform();
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Invalid speed level %d", speed_level);
|
||||
this->status_momentary_warning("speed", 5000);
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Invalid speed level %s (int expected)", payload.c_str());
|
||||
this->status_momentary_warning("speed", 5000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (this->state_->get_traits().supports_speed()) {
|
||||
this->subscribe(this->get_speed_command_topic(), [this](const std::string &topic, const std::string &payload) {
|
||||
this->state_->make_call()
|
||||
@@ -75,6 +96,22 @@ void MQTTFanComponent::setup() {
|
||||
auto f = std::bind(&MQTTFanComponent::publish_state, this);
|
||||
this->state_->add_on_state_callback([this, f]() { this->defer("send", f); });
|
||||
}
|
||||
|
||||
void MQTTFanComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "MQTT Fan '%s': ", this->state_->get_name().c_str());
|
||||
LOG_MQTT_COMPONENT(true, true);
|
||||
if (this->state_->get_traits().supports_oscillation()) {
|
||||
ESP_LOGCONFIG(TAG, " Oscillation State Topic: '%s'", this->get_oscillation_state_topic().c_str());
|
||||
ESP_LOGCONFIG(TAG, " Oscillation Command Topic: '%s'", this->get_oscillation_command_topic().c_str());
|
||||
}
|
||||
if (this->state_->get_traits().supports_speed()) {
|
||||
ESP_LOGCONFIG(TAG, " Speed Level State Topic: '%s'", this->get_speed_level_state_topic().c_str());
|
||||
ESP_LOGCONFIG(TAG, " Speed Level Command Topic: '%s'", this->get_speed_level_command_topic().c_str());
|
||||
ESP_LOGCONFIG(TAG, " Speed State Topic: '%s'", this->get_speed_state_topic().c_str());
|
||||
ESP_LOGCONFIG(TAG, " Speed Command Topic: '%s'", this->get_speed_command_topic().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool MQTTFanComponent::send_initial_state() { return this->publish_state(); }
|
||||
std::string MQTTFanComponent::friendly_name() const { return this->state_->get_name(); }
|
||||
void MQTTFanComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
|
||||
@@ -83,6 +120,8 @@ void MQTTFanComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfi
|
||||
root["oscillation_state_topic"] = this->get_oscillation_state_topic();
|
||||
}
|
||||
if (this->state_->get_traits().supports_speed()) {
|
||||
root["speed_level_command_topic"] = this->get_speed_level_command_topic();
|
||||
root["speed_level_state_topic"] = this->get_speed_level_state_topic();
|
||||
root["speed_command_topic"] = this->get_speed_command_topic();
|
||||
root["speed_state_topic"] = this->get_speed_state_topic();
|
||||
}
|
||||
@@ -99,6 +138,11 @@ bool MQTTFanComponent::publish_state() {
|
||||
failed = failed || !success;
|
||||
}
|
||||
auto traits = this->state_->get_traits();
|
||||
if (traits.supports_speed()) {
|
||||
std::string payload = to_string(this->state_->speed);
|
||||
bool success = this->publish(this->get_speed_level_state_topic(), payload);
|
||||
failed = failed || !success;
|
||||
}
|
||||
if (traits.supports_speed()) {
|
||||
const char *payload;
|
||||
// NOLINTNEXTLINE(clang-diagnostic-deprecated-declarations)
|
||||
|
Reference in New Issue
Block a user