forked from esphome/esphome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add number entity to support vedirect configuration for numeric regis…
…ters (m3_vedirect)
- Loading branch information
Showing
12 changed files
with
226 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
#include "entity.h" | ||
#include "esphome/core/application.h" | ||
#include "esphome/core/log.h" | ||
#include "esphome/components/api/api_server.h" | ||
|
||
#include "manager.h" | ||
|
||
namespace esphome { | ||
namespace m3_vedirect {} // namespace m3_vedirect | ||
namespace m3_vedirect { | ||
|
||
const char *NumericEntity::UNIT_TO_DEVICE_CLASS[REG_DEF::UNIT::UNIT_COUNT] = { | ||
nullptr, "current", "voltage", "apparent_power", "power", nullptr, "energy", "battery", "duration", "temperature", | ||
}; | ||
|
||
} // namespace m3_vedirect | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from esphome.components import number | ||
import esphome.config_validation as cv | ||
import esphome.const as ec | ||
|
||
from .. import ( | ||
CONF_VEDIRECT_ENTITIES, | ||
m3_vedirect_ns, | ||
new_vedirect_entity, | ||
ve_reg, | ||
vedirect_entity_schema, | ||
vedirect_platform_schema, | ||
vedirect_platform_to_code, | ||
) | ||
|
||
VEDirectNumber = m3_vedirect_ns.class_("Number", number.Number) | ||
VEDIRECT_NUMBER_SCHEMA = ( | ||
number.number_schema(VEDirectNumber) | ||
.extend(vedirect_entity_schema((ve_reg.CLASS.NUMERIC,), False)) | ||
.extend( | ||
{ | ||
cv.Required(ec.CONF_MIN_VALUE): cv.float_, | ||
cv.Required(ec.CONF_MAX_VALUE): cv.float_, | ||
cv.Required(ec.CONF_STEP): cv.positive_float, | ||
} | ||
) | ||
) | ||
|
||
PLATFORM_ENTITIES = { | ||
CONF_VEDIRECT_ENTITIES: cv.ensure_list(VEDIRECT_NUMBER_SCHEMA), | ||
} | ||
|
||
CONFIG_SCHEMA = vedirect_platform_schema(PLATFORM_ENTITIES) | ||
|
||
|
||
async def new_vedirect_number(config, manager): | ||
var = await new_vedirect_entity(config, manager) | ||
await number.register_number( | ||
var, | ||
config, | ||
min_value=config[ec.CONF_MIN_VALUE], | ||
max_value=config[ec.CONF_MAX_VALUE], | ||
step=config[ec.CONF_STEP], | ||
) | ||
return var | ||
|
||
|
||
async def to_code(config: dict): | ||
await vedirect_platform_to_code( | ||
config, | ||
PLATFORM_ENTITIES, | ||
new_vedirect_number, | ||
number.new_number, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "number.h" | ||
#include "esphome/core/application.h" | ||
#include "esphome/components/api/api_server.h" | ||
#include "../manager.h" | ||
|
||
namespace esphome { | ||
namespace m3_vedirect { | ||
|
||
void Number::dynamic_register_() { | ||
App.register_number(this); | ||
if (api::global_api_server) | ||
add_on_state_callback([this](float state) { api::global_api_server->on_number_update(this, state); }); | ||
} | ||
|
||
void Number::link_disconnected_() { this->publish_state(NAN); } | ||
|
||
void Number::init_reg_def_() { | ||
auto reg_def = this->reg_def_; | ||
// Whatever the CLASS, number will just extract any meaningful numeric value | ||
// from the HEX payload eventually scaling by hex_scale | ||
this->hex_scale_ = REG_DEF::SCALE_TO_SCALE[reg_def->scale]; | ||
this->traits.set_unit_of_measurement(REG_DEF::UNITS[reg_def->unit]); | ||
this->traits.set_device_class(UNIT_TO_DEVICE_CLASS[reg_def->unit]); | ||
this->traits.set_step(this->hex_scale_); | ||
|
||
switch (reg_def->unit) { | ||
case REG_DEF::UNIT::CELSIUS: | ||
// special treatment for 'temperature' registers which are expected to carry un16 kelvin degrees | ||
this->parse_hex_ = parse_hex_temperature_; | ||
break; | ||
default: | ||
this->parse_hex_ = DATA_TYPE_TO_PARSE_HEX_FUNC_[reg_def->data_type]; | ||
} | ||
} | ||
|
||
void Number::parse_hex_default_(HexRegister *hex_register, const RxHexFrame *hex_frame) { | ||
Number *number = static_cast<Number *>(hex_register); | ||
float value; | ||
switch (hex_frame->data_size()) { | ||
case 1: | ||
value = hex_frame->data_t<uint8_t>() * number->hex_scale_; | ||
break; | ||
case 2: | ||
// it might be signed though | ||
value = hex_frame->data_t<uint16_t>() * number->hex_scale_; | ||
break; | ||
case 4: | ||
value = hex_frame->data_t<uint32_t>() * number->hex_scale_; | ||
break; | ||
default: | ||
value = NAN; | ||
} | ||
if (number->state != value) { | ||
number->publish_state(value); | ||
} | ||
} | ||
|
||
void Number::parse_hex_temperature_(HexRegister *hex_register, const RxHexFrame *hex_frame) { | ||
Number *number = static_cast<Number *>(hex_register); | ||
// hoping the operands are int-promoted and the result is an int | ||
float value = (hex_frame->data_t<uint16_t>() - 27316) * number->hex_scale_; | ||
if (number->state != value) { | ||
number->publish_state(value); | ||
} | ||
} | ||
|
||
template<typename T> void Number::parse_hex_t_(HexRegister *hex_register, const RxHexFrame *hex_frame) { | ||
static_assert(RxHexFrame::ALLOCATED_DATA_SIZE >= 4, "HexFrame storage might lead to access overflow"); | ||
Number *number = static_cast<Number *>(hex_register); | ||
float value = hex_frame->data_t<T>() * number->hex_scale_; | ||
if (number->state != value) { | ||
number->publish_state(value); | ||
} | ||
} | ||
|
||
const Number::parse_hex_func_t Number::DATA_TYPE_TO_PARSE_HEX_FUNC_[REG_DEF::DATA_TYPE::_COUNT] = { | ||
Number::parse_hex_default_, Number::parse_hex_t_<uint8_t>, Number::parse_hex_t_<uint16_t>, | ||
Number::parse_hex_t_<uint32_t>, Number::parse_hex_t_<int8_t>, Number::parse_hex_t_<int16_t>, | ||
Number::parse_hex_t_<int32_t>, | ||
}; | ||
|
||
void Number::control(float value) { | ||
// Assuming 'value' is not out of range of the underlying data type, this code | ||
// should work for both signed/unsigned quantities | ||
int native_value = value / this->hex_scale_; | ||
this->manager->send_register_set(this->reg_def_->register_id, &native_value, this->reg_def_->data_type); | ||
}; | ||
|
||
} // namespace m3_vedirect | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
#include "esphome/components/number/number.h" | ||
|
||
#include "../entity.h" | ||
|
||
namespace esphome { | ||
namespace m3_vedirect { | ||
|
||
class Number final : public ConfigEntity, public NumericEntity, public Entity, public esphome::number::Number { | ||
public: | ||
Number(Manager *manager) : ConfigEntity(manager), Entity(parse_hex_default_, parse_text_empty_) {} | ||
|
||
protected: | ||
friend class Manager; | ||
void dynamic_register_() override; | ||
void link_disconnected_() override; | ||
|
||
void init_reg_def_() override; | ||
|
||
static void parse_hex_default_(HexRegister *hex_register, const RxHexFrame *hex_frame); | ||
static void parse_hex_temperature_(HexRegister *hex_register, const RxHexFrame *hex_frame); | ||
template<typename T> static void parse_hex_t_(HexRegister *hex_register, const RxHexFrame *hex_frame); | ||
static const parse_hex_func_t DATA_TYPE_TO_PARSE_HEX_FUNC_[]; | ||
|
||
// interface esphome::number::Number | ||
|
||
void control(float value) override; | ||
}; | ||
|
||
} // namespace m3_vedirect | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters