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:
Joris S
2023-11-03 07:54:47 +01:00
committed by GitHub
parent 22cdb8dfc3
commit 13994d9bd1
20 changed files with 1134 additions and 0 deletions
@@ -0,0 +1,110 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import number
from esphome.const import (
DEVICE_CLASS_TEMPERATURE,
UNIT_CELSIUS,
CONF_STEP,
)
from .. import (
MicroNova,
MicroNovaFunctions,
CONF_MICRONOVA_ID,
CONF_MEMORY_LOCATION,
CONF_MEMORY_ADDRESS,
MICRONOVA_LISTENER_SCHEMA,
micronova_ns,
)
ICON_FLASH = "mdi:flash"
CONF_THERMOSTAT_TEMPERATURE = "thermostat_temperature"
CONF_POWER_LEVEL = "power_level"
CONF_MEMORY_WRITE_LOCATION = "memory_write_location"
MicroNovaNumber = micronova_ns.class_("MicroNovaNumber", number.Number, cg.Component)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_MICRONOVA_ID): cv.use_id(MicroNova),
cv.Optional(CONF_THERMOSTAT_TEMPERATURE): number.number_schema(
MicroNovaNumber,
unit_of_measurement=UNIT_CELSIUS,
device_class=DEVICE_CLASS_TEMPERATURE,
)
.extend(
MICRONOVA_LISTENER_SCHEMA(
default_memory_location=0x20, default_memory_address=0x7D
)
)
.extend(
{
cv.Optional(
CONF_MEMORY_WRITE_LOCATION, default=0xA0
): cv.hex_int_range(),
cv.Optional(CONF_STEP, default=1.0): cv.float_range(min=0.1, max=10.0),
}
),
cv.Optional(CONF_POWER_LEVEL): number.number_schema(
MicroNovaNumber,
icon=ICON_FLASH,
)
.extend(
MICRONOVA_LISTENER_SCHEMA(
default_memory_location=0x20, default_memory_address=0x7F
)
)
.extend(
{cv.Optional(CONF_MEMORY_WRITE_LOCATION, default=0xA0): cv.hex_int_range()}
),
}
)
async def to_code(config):
mv = await cg.get_variable(config[CONF_MICRONOVA_ID])
if thermostat_temperature_config := config.get(CONF_THERMOSTAT_TEMPERATURE):
numb = await number.new_number(
thermostat_temperature_config,
min_value=0,
max_value=40,
step=thermostat_temperature_config.get(CONF_STEP),
)
cg.add(numb.set_micronova_object(mv))
cg.add(mv.register_micronova_listener(numb))
cg.add(
numb.set_memory_location(
thermostat_temperature_config[CONF_MEMORY_LOCATION]
)
)
cg.add(
numb.set_memory_address(thermostat_temperature_config[CONF_MEMORY_ADDRESS])
)
cg.add(
numb.set_memory_write_location(
thermostat_temperature_config.get(CONF_MEMORY_WRITE_LOCATION)
)
)
cg.add(
numb.set_function(MicroNovaFunctions.STOVE_FUNCTION_THERMOSTAT_TEMPERATURE)
)
if power_level_config := config.get(CONF_POWER_LEVEL):
numb = await number.new_number(
power_level_config,
min_value=1,
max_value=5,
step=1,
)
cg.add(numb.set_micronova_object(mv))
cg.add(mv.register_micronova_listener(numb))
cg.add(numb.set_memory_location(power_level_config[CONF_MEMORY_LOCATION]))
cg.add(numb.set_memory_address(power_level_config[CONF_MEMORY_ADDRESS]))
cg.add(
numb.set_memory_write_location(
power_level_config.get(CONF_MEMORY_WRITE_LOCATION)
)
)
cg.add(numb.set_function(MicroNovaFunctions.STOVE_FUNCTION_POWER_LEVEL))
@@ -0,0 +1,45 @@
#include "micronova_number.h"
namespace esphome {
namespace micronova {
void MicroNovaNumber::process_value_from_stove(int value_from_stove) {
float new_sensor_value = 0;
if (value_from_stove == -1) {
this->publish_state(NAN);
return;
}
switch (this->get_function()) {
case MicroNovaFunctions::STOVE_FUNCTION_THERMOSTAT_TEMPERATURE:
new_sensor_value = ((float) value_from_stove) * this->traits.get_step();
break;
case MicroNovaFunctions::STOVE_FUNCTION_POWER_LEVEL:
new_sensor_value = (float) value_from_stove;
break;
default:
break;
}
this->publish_state(new_sensor_value);
}
void MicroNovaNumber::control(float value) {
uint8_t new_number = 0;
switch (this->get_function()) {
case MicroNovaFunctions::STOVE_FUNCTION_THERMOSTAT_TEMPERATURE:
new_number = (uint8_t) (value / this->traits.get_step());
break;
case MicroNovaFunctions::STOVE_FUNCTION_POWER_LEVEL:
new_number = (uint8_t) value;
break;
default:
break;
}
this->micronova_->write_address(this->memory_write_location_, this->memory_address_, new_number);
this->micronova_->update();
}
} // namespace micronova
} // namespace esphome
@@ -0,0 +1,28 @@
#pragma once
#include "esphome/components/micronova/micronova.h"
#include "esphome/components/number/number.h"
namespace esphome {
namespace micronova {
class MicroNovaNumber : public number::Number, public MicroNovaSensorListener {
public:
MicroNovaNumber() {}
MicroNovaNumber(MicroNova *m) : MicroNovaSensorListener(m) {}
void dump_config() override { LOG_NUMBER("", "Micronova number", this); }
void control(float value) override;
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_write_location(uint8_t l) { this->memory_write_location_ = l; }
uint8_t get_memory_write_location() { return this->memory_write_location_; }
protected:
uint8_t memory_write_location_ = 0;
};
} // namespace micronova
} // namespace esphome