[micronova] Make stove switch entity independent (#12355)

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Robert Resch
2025-12-08 17:18:44 +01:00
committed by GitHub
parent 801d1135ab
commit 9f60aed9b0
5 changed files with 44 additions and 50 deletions

View File

@@ -75,17 +75,6 @@ class MicroNovaListener : public MicroNovaBaseListener, public PollingComponent
bool needs_update_ = false;
};
class MicroNovaSwitchListener : public MicroNovaBaseListener {
public:
MicroNovaSwitchListener(MicroNova *m) : MicroNovaBaseListener(m) {}
virtual void set_stove_state(bool v) = 0;
virtual bool get_stove_state() = 0;
protected:
uint8_t memory_data_on_ = 0;
uint8_t memory_data_off_ = 0;
};
class MicroNovaButtonListener : public MicroNovaBaseListener {
public:
MicroNovaButtonListener(MicroNova *m) : MicroNovaBaseListener(m) {}
@@ -112,15 +101,7 @@ class MicroNova : public Component, public uart::UARTDevice {
void set_enable_rx_pin(GPIOPin *enable_rx_pin) { this->enable_rx_pin_ = enable_rx_pin; }
void set_current_stove_state(uint8_t s) { this->current_stove_state_ = s; }
uint8_t get_current_stove_state() { return this->current_stove_state_; }
void set_stove(MicroNovaSwitchListener *s) { this->stove_switch_ = s; }
MicroNovaSwitchListener *get_stove_switch() { return this->stove_switch_; }
protected:
uint8_t current_stove_state_ = 0;
GPIOPin *enable_rx_pin_{nullptr};
struct MicroNovaSerialTransmission {
@@ -135,7 +116,6 @@ class MicroNova : public Component, public uart::UARTDevice {
MicroNovaSerialTransmission current_transmission_;
std::vector<MicroNovaListener *> micronova_listeners_{};
MicroNovaSwitchListener *stove_switch_{nullptr};
};
} // namespace esphome::micronova

View File

@@ -4,20 +4,22 @@ import esphome.config_validation as cv
from esphome.const import ICON_POWER
from .. import (
CONF_MEMORY_ADDRESS,
CONF_MEMORY_LOCATION,
CONF_MICRONOVA_ID,
MICRONOVA_ADDRESS_SCHEMA,
MicroNova,
MicroNovaFunctions,
MicroNovaListener,
micronova_ns,
to_code_micronova_listener,
)
CONF_STOVE = "stove"
CONF_MEMORY_DATA_ON = "memory_data_on"
CONF_MEMORY_DATA_OFF = "memory_data_off"
MicroNovaSwitch = micronova_ns.class_("MicroNovaSwitch", switch.Switch, cg.Component)
MicroNovaSwitch = micronova_ns.class_(
"MicroNovaSwitch", switch.Switch, MicroNovaListener
)
CONFIG_SCHEMA = cv.Schema(
{
@@ -30,7 +32,7 @@ CONFIG_SCHEMA = cv.Schema(
MICRONOVA_ADDRESS_SCHEMA(
default_memory_location=0x00,
default_memory_address=0x21,
is_polling_component=False,
is_polling_component=True,
)
)
.extend(
@@ -48,9 +50,8 @@ async def to_code(config):
if stove_config := config.get(CONF_STOVE):
sw = await switch.new_switch(stove_config, mv)
cg.add(mv.set_stove(sw))
cg.add(sw.set_memory_location(stove_config[CONF_MEMORY_LOCATION]))
cg.add(sw.set_memory_address(stove_config[CONF_MEMORY_ADDRESS]))
await to_code_micronova_listener(
mv, sw, stove_config, MicroNovaFunctions.STOVE_FUNCTION_SWITCH
)
cg.add(sw.set_memory_data_on(stove_config[CONF_MEMORY_DATA_ON]))
cg.add(sw.set_memory_data_off(stove_config[CONF_MEMORY_DATA_OFF]))
cg.add(sw.set_function(MicroNovaFunctions.STOVE_FUNCTION_SWITCH))

View File

@@ -4,27 +4,46 @@ namespace esphome::micronova {
void MicroNovaSwitch::write_state(bool state) {
switch (this->get_function()) {
case MicroNovaFunctions::STOVE_FUNCTION_SWITCH:
case MicroNovaFunctions::STOVE_FUNCTION_SWITCH: {
if (state) {
// Only send power-on when current state is Off
if (this->micronova_->get_current_stove_state() == 0) {
if (this->raw_state_ == 0) {
this->micronova_->write_address(this->memory_location_, this->memory_address_, this->memory_data_on_);
this->publish_state(true);
} else {
ESP_LOGW(TAG, "Unable to turn stove on, invalid state: %d", micronova_->get_current_stove_state());
ESP_LOGW(TAG, "Unable to turn stove on, invalid state: %d", this->raw_state_);
}
} else {
// don't send power-off when status is Off or Final cleaning
if (this->micronova_->get_current_stove_state() != 0 && micronova_->get_current_stove_state() != 6) {
if (this->raw_state_ != 0 && this->raw_state_ != 6) {
this->micronova_->write_address(this->memory_location_, this->memory_address_, this->memory_data_off_);
this->publish_state(false);
} else {
ESP_LOGW(TAG, "Unable to turn stove off, invalid state: %d", micronova_->get_current_stove_state());
ESP_LOGW(TAG, "Unable to turn stove off, invalid state: %d", this->raw_state_);
}
}
this->micronova_->request_update_listeners();
this->set_needs_update(true);
break;
}
default:
break;
}
}
void MicroNovaSwitch::process_value_from_stove(int value_from_stove) {
this->raw_state_ = value_from_stove;
if (value_from_stove == -1) {
ESP_LOGE(TAG, "Error reading stove state");
return;
}
switch (this->get_function()) {
case MicroNovaFunctions::STOVE_FUNCTION_SWITCH: {
// set the stove switch to on for any value but 0
bool state = value_from_stove != 0;
this->publish_state(state);
break;
}
default:
break;
}

View File

@@ -6,25 +6,28 @@
namespace esphome::micronova {
class MicroNovaSwitch : public Component, public switch_::Switch, public MicroNovaSwitchListener {
class MicroNovaSwitch : public switch_::Switch, public MicroNovaListener {
public:
MicroNovaSwitch(MicroNova *m) : MicroNovaSwitchListener(m) {}
MicroNovaSwitch(MicroNova *m) : MicroNovaListener(m) {}
void dump_config() override {
LOG_SWITCH("", "Micronova switch", this);
this->dump_base_config();
}
void set_stove_state(bool v) override { this->publish_state(v); }
bool get_stove_state() override { return this->state; }
void request_value_from_stove() override {
this->micronova_->request_address(this->memory_location_, this->memory_address_, this);
}
void process_value_from_stove(int value_from_stove) override;
void set_memory_data_on(uint8_t f) { this->memory_data_on_ = f; }
uint8_t get_memory_data_on() { return this->memory_data_on_; }
void set_memory_data_off(uint8_t f) { this->memory_data_off_ = f; }
uint8_t get_memory_data_off() { return this->memory_data_off_; }
protected:
void write_state(bool state) override;
uint8_t memory_data_on_ = 0;
uint8_t memory_data_off_ = 0;
uint8_t raw_state_ = 0;
};
} // namespace esphome::micronova

View File

@@ -10,16 +10,7 @@ void MicroNovaTextSensor::process_value_from_stove(int value_from_stove) {
switch (this->get_function()) {
case MicroNovaFunctions::STOVE_FUNCTION_STOVE_STATE:
this->micronova_->set_current_stove_state(value_from_stove);
this->publish_state(STOVE_STATES[value_from_stove]);
// set the stove switch to on for any value but 0
if (value_from_stove != 0 && this->micronova_->get_stove_switch() != nullptr &&
!this->micronova_->get_stove_switch()->get_stove_state()) {
this->micronova_->get_stove_switch()->set_stove_state(true);
} else if (value_from_stove == 0 && this->micronova_->get_stove_switch() != nullptr &&
this->micronova_->get_stove_switch()->get_stove_state()) {
this->micronova_->get_stove_switch()->set_stove_state(false);
}
break;
default:
break;