Skip to content

Commit

Permalink
fix NAN handling and add a pair of charger registers (m3_vedirect)
Browse files Browse the repository at this point in the history
  • Loading branch information
krahabb committed Nov 21, 2024
1 parent e77355e commit f90b348
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
6 changes: 4 additions & 2 deletions esphome/components/m3_vedirect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,10 @@ async def to_code(self, config: dict):
await self.register_entity(entity, _entity_config)
continue
if entity_key in self.custom_entities:
for define in self.custom_entities[entity_key].define.split(","):
define_symbol(define)
define = self.custom_entities[entity_key].define
if define:
for _define in define.split(","):
define_symbol(_define)

entity = await self.new_base_entity(entity_config)
cg.add(getattr(manager, f"set_{entity_key}")(entity))
Expand Down
5 changes: 4 additions & 1 deletion esphome/components/m3_vedirect/number/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ void Number::parse_hex_default_(HexRegister *hex_register, const RxHexFrame *hex
value = hex_frame->data_t<uint32_t>() * number->hex_scale_;
break;
default:
value = NAN;
if (!std::isnan(number->state)) {
number->publish_state(NAN);
}
return;
}
if (number->state != value) {
number->publish_state(value);
Expand Down
19 changes: 14 additions & 5 deletions esphome/components/m3_vedirect/sensor/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ void Sensor::parse_hex_default_(HexRegister *hex_register, const RxHexFrame *hex
value = hex_frame->data_t<uint32_t>() * sensor->hex_scale_;
break;
default:
value = NAN;
if (!std::isnan(sensor->raw_state)) {
sensor->publish_state(NAN);
}
return;
}
if (sensor->raw_state != value) {
sensor->publish_state(value);
Expand Down Expand Up @@ -124,10 +127,16 @@ void Sensor::parse_text_default_(HexRegister *hex_register, const char *text_val
Sensor *sensor = static_cast<Sensor *>(hex_register);
char *endptr;
float value = strtof(text_value, &endptr) * sensor->text_scale_;
if (*endptr != 0)
value = NAN;
if (sensor->raw_state != value)
sensor->publish_state(value);
if (*endptr) {
// failed conversion
if (!std::isnan(sensor->raw_state)) {
sensor->publish_state(NAN);
}
} else {
if (sensor->raw_state != value) {
sensor->publish_state(value);
}
}
}
#endif // defined(VEDIRECT_USE_TEXTFRAME)

Expand Down
4 changes: 2 additions & 2 deletions esphome/components/m3_vedirect/ve_reg_flavor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#pragma once
#include "ve_reg_macro.h"

#if 1
#if 0
// debug/testing preset environment: remove on release
#define VEDIRECT_FLAVOR_ALL
//#define VEDIRECT_FLAVOR_ALL
//#define VEDIRECT_FLAVOR_MPPT_RS
//#define VEDIRECT_FLAVOR_INV_PHNX
//#define VEDIRECT_FLAVOR_BMV
Expand Down
2 changes: 2 additions & 0 deletions esphome/components/m3_vedirect/ve_reg_register.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
MACRO(MPPT, NUMERIC, 0xEDD1, YIELD_YESTERDAY, READ_ONLY, uint16_t, kWh, S_0_01, S_0_01) \
MACRO(MPPT, NUMERIC, 0xEDD2, MAXIMUM_POWER_TODAY, READ_ONLY, uint16_t, W, S_1, S_1) \
MACRO(MPPT, NUMERIC, 0xEDD3, YIELD_TODAY, READ_ONLY, uint16_t, kWh, S_0_01, S_0_01) \
MACRO(CHG, NUMERIC, 0xEDD5, CHARGER_VOLTAGE, READ_ONLY, uint16_t, V, S_0_01, S_0_001) \
MACRO(CHG, NUMERIC, 0xEDD7, CHARGER_CURRENT, READ_ONLY, uint16_t, A, S_0_1, S_0_001) \
MACRO(CHG, ENUM, 0xEDDA, CHR_ERROR_CODE, READ_ONLY) \
MACRO(MPPT, NUMERIC, 0xEDDC, USER_YIELD, READ_ONLY, uint32_t, kWh, S_0_01, S_0_01) \
MACRO(MPPT, NUMERIC, 0xEDDD, SYSTEM_YIELD, READ_ONLY, uint32_t, kWh, S_0_01, S_0_01) \
Expand Down

0 comments on commit f90b348

Please sign in to comment.