mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-06 04:48:27 +02:00
Add Micronova component (#4760)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: Graham Brown <grahambrown11@gmail.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import text_sensor
|
||||
|
||||
from .. import (
|
||||
MicroNova,
|
||||
MicroNovaFunctions,
|
||||
CONF_MICRONOVA_ID,
|
||||
CONF_MEMORY_LOCATION,
|
||||
CONF_MEMORY_ADDRESS,
|
||||
MICRONOVA_LISTENER_SCHEMA,
|
||||
micronova_ns,
|
||||
)
|
||||
|
||||
CONF_STOVE_STATE = "stove_state"
|
||||
|
||||
MicroNovaTextSensor = micronova_ns.class_(
|
||||
"MicroNovaTextSensor", text_sensor.TextSensor, cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_MICRONOVA_ID): cv.use_id(MicroNova),
|
||||
cv.Optional(CONF_STOVE_STATE): text_sensor.text_sensor_schema(
|
||||
MicroNovaTextSensor
|
||||
).extend(
|
||||
MICRONOVA_LISTENER_SCHEMA(
|
||||
default_memory_location=0x00, default_memory_address=0x21
|
||||
)
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
mv = await cg.get_variable(config[CONF_MICRONOVA_ID])
|
||||
|
||||
if stove_state_config := config.get(CONF_STOVE_STATE):
|
||||
sens = await text_sensor.new_text_sensor(stove_state_config, mv)
|
||||
cg.add(mv.register_micronova_listener(sens))
|
||||
cg.add(sens.set_memory_location(stove_state_config[CONF_MEMORY_LOCATION]))
|
||||
cg.add(sens.set_memory_address(stove_state_config[CONF_MEMORY_ADDRESS]))
|
||||
cg.add(sens.set_function(MicroNovaFunctions.STOVE_FUNCTION_STOVE_STATE))
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "micronova_text_sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace micronova {
|
||||
|
||||
void MicroNovaTextSensor::process_value_from_stove(int value_from_stove) {
|
||||
if (value_from_stove == -1) {
|
||||
this->publish_state("unknown");
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace micronova
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/micronova/micronova.h"
|
||||
#include "esphome/components/text_sensor/text_sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace micronova {
|
||||
|
||||
class MicroNovaTextSensor : public text_sensor::TextSensor, public MicroNovaSensorListener {
|
||||
public:
|
||||
MicroNovaTextSensor(MicroNova *m) : MicroNovaSensorListener(m) {}
|
||||
void dump_config() override { LOG_TEXT_SENSOR("", "Micronova text sensor", this); }
|
||||
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;
|
||||
};
|
||||
|
||||
} // namespace micronova
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user