Skip to content

Commit

Permalink
simplify HexRegister default initialization (m3_vedirect)
Browse files Browse the repository at this point in the history
  • Loading branch information
krahabb committed Nov 18, 2024
1 parent 48f5a14 commit 6002eb4
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 7 deletions.
4 changes: 2 additions & 2 deletions esphome/components/m3_vedirect/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def generate():
"import enum\n",
"from functools import cached_property\n",
"\nimport esphome.codegen as cg\n",
'\n\nns = cg.global_ns.namespace("m3_ve_reg")\n',
'\nns = cg.global_ns.namespace("m3_ve_reg")\n',
"\n\nclass MockEnum(enum.IntEnum):\n",
f"\n{INDENT}@cached_property\n",
f"{INDENT}@cached_property\n",
f"{INDENT}def ns(self):\n",
f"{INDENT}{INDENT}pass\n",
f"\n{INDENT}@cached_property\n",
Expand Down
2 changes: 1 addition & 1 deletion esphome/components/m3_vedirect/hexregister.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HexRegister {
parse_text_func_t parse_text_;

HexRegister(parse_hex_func_t parse_hex_func = parse_hex_empty_, parse_text_func_t parse_text_func = parse_text_empty_)
: reg_def_(&REG_DEF::DEFS[REG_DEF::TYPE::UNDEFINED]), parse_hex_(parse_hex_func), parse_text_(parse_text_func) {}
: reg_def_(nullptr), parse_hex_(parse_hex_func), parse_text_(parse_text_func) {}

// called by the Manager when VEDirect timeouts (we'll send 'unknown' to APIServer)
virtual void link_disconnected_(){};
Expand Down
2 changes: 1 addition & 1 deletion esphome/components/m3_vedirect/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void Manager::init_entity(Entity *entity, REG_DEF::TYPE register_type) {
void Manager::init_entity(Entity *entity, const char *label) {
auto text_def = TEXT_DEF::find_label(label);
if (text_def) {
if (entity->reg_def_ == &REG_DEF::DEFS[REG_DEF::TYPE::UNDEFINED]) {
if (!entity->reg_def_) {
// only set reg_def from our presets (if any) if the yaml generated code
// didn't set a custom configuration
auto reg_def = REG_DEF::find_type(text_def->register_type);
Expand Down
1 change: 1 addition & 0 deletions esphome/components/m3_vedirect/select/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void Select::parse_string_(const char *string_value) {
}

void Select::control(const std::string &value) {
// TODO: are we 100% sure enum_def is defined ? check yaml init code
auto lookup_def = this->reg_def_->enum_def->lookup_value(value.c_str());
if (lookup_def)
this->manager->send_register_set(this->reg_def_->register_id, lookup_def->value);
Expand Down
2 changes: 0 additions & 2 deletions esphome/components/m3_vedirect/ve_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class Flavor(enum.StrEnum):


class TYPE(MockEnum):
UNDEFINED = enum.auto()
BLE_MODE = enum.auto()
PRODUCT_ID = enum.auto()
SERIAL_NUMBER = enum.auto()
Expand Down Expand Up @@ -231,7 +230,6 @@ def ns(self):

REG_DEF = namedtuple("REG_DEF", ["flavor", "cls", "register_id", "access"])
REG_DEFS = {
TYPE.UNDEFINED.name: REG_DEF("ANY", CLASS.UNKNOWN, 0x0000, ACCESS.READ_ONLY),
TYPE.BLE_MODE.name: REG_DEF("ANY", CLASS.BITMASK, 0x0090, ACCESS.READ_WRITE),
TYPE.PRODUCT_ID.name: REG_DEF("ANY", CLASS.UNKNOWN, 0x0100, ACCESS.READ_ONLY),
TYPE.SERIAL_NUMBER.name: REG_DEF("ANY", CLASS.STRING, 0x010A, ACCESS.READ_ONLY),
Expand Down
1 change: 0 additions & 1 deletion esphome/components/m3_vedirect/ve_reg_register.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// clang-format off

#define REGISTERS_COMMON(MACRO) \
MACRO(ANY, UNKNOWN, 0x0000, UNDEFINED, READ_ONLY) \
MACRO(ANY, BITMASK, 0x0090, BLE_MODE, READ_WRITE, uint8_t) \
MACRO(ANY, UNKNOWN, 0x0100, PRODUCT_ID, READ_ONLY) \
MACRO(ANY, STRING, 0x010A, SERIAL_NUMBER, READ_ONLY) \
Expand Down

0 comments on commit 6002eb4

Please sign in to comment.