Formatted.

This commit is contained in:
Victor Chang
2024-06-06 22:01:06 -07:00
parent 2e0c6f853a
commit b3d1a1fda4
3 changed files with 779 additions and 748 deletions

View File

@@ -1,154 +1,160 @@
#include "esphome/core/log.h"
#include "emporia_vue_utility.h" #include "emporia_vue_utility.h"
#include "esphome/core/log.h"
namespace esphome { namespace esphome {
namespace emporia_vue_utility { namespace emporia_vue_utility {
void EmporiaVueUtility::setup() { void EmporiaVueUtility::setup() {
#if USE_LED_PINS #if USE_LED_PINS
pinMode(LED_PIN_LINK, OUTPUT); pinMode(LED_PIN_LINK, OUTPUT);
pinMode(LED_PIN_WIFI, OUTPUT); pinMode(LED_PIN_WIFI, OUTPUT);
#endif #endif
led_link(false); led_link(false);
led_wifi(false); led_wifi(false);
clear_serial_input(); clear_serial_input();
} }
void EmporiaVueUtility::update() { void EmporiaVueUtility::update() {
// This seems to be called incessantly instead of at the set update interval... // This seems to be called incessantly instead of at the set update
// ESP_LOGD(TAG, "Got update call with an instructed interval of %d sec", this->update_interval_); // interval... ESP_LOGD(TAG, "Got update call with an instructed interval of
// %d sec", this->update_interval_);
} }
void EmporiaVueUtility::loop() { void EmporiaVueUtility::loop() {
static time_t next_meter_request; static time_t next_meter_request;
static time_t next_meter_join; static time_t next_meter_join;
static time_t next_version_request = 0; static time_t next_version_request = 0;
static uint8_t startup_step; static uint8_t startup_step;
char msg_type = 0; char msg_type = 0;
size_t msg_len = 0; size_t msg_len = 0;
byte inb; byte inb;
msg_len = read_msg(); msg_len = read_msg();
now = ::time(&now); now = ::time(&now);
/* sanity checks! */ /* sanity checks! */
if (next_meter_request > if (next_meter_request >
now + (INITIAL_STARTUP_DELAY + METER_REJOIN_INTERVAL)) { now + (INITIAL_STARTUP_DELAY + METER_REJOIN_INTERVAL)) {
ESP_LOGD(TAG, ESP_LOGD(TAG, "Time jumped back (%lld > %lld + %lld); resetting",
"Time jumped back (%lld > %lld + %lld); resetting", (long long)next_meter_request, (long long)now,
(long long) next_meter_request, (long long)(INITIAL_STARTUP_DELAY + METER_REJOIN_INTERVAL));
(long long) now, next_meter_request = next_meter_join = 0;
(long long) (INITIAL_STARTUP_DELAY + }
METER_REJOIN_INTERVAL));
next_meter_request = next_meter_join = 0;
}
if (msg_len != 0) { if (msg_len != 0) {
msg_type = input_buffer.data[2]; msg_type = input_buffer.data[2];
switch (msg_type) { switch (msg_type) {
case 'r': // Meter reading case 'r': // Meter reading
led_link(true); led_link(true);
if (now < last_meter_reading + int(update_interval_ / 4)) { if (now < last_meter_reading + int(update_interval_ / 4)) {
// Sometimes a duplicate message is sent in quick succession. // Sometimes a duplicate message is sent in quick succession.
// Ignoring the duplicate. // Ignoring the duplicate.
ESP_LOGD(TAG, "Got extra message %ds after the previous message.", now - last_meter_reading); ESP_LOGD(TAG, "Got extra message %ds after the previous message.",
break; now - last_meter_reading);
} break;
last_reading_has_error = 0;
handle_resp_meter_reading();
if (last_reading_has_error) {
ask_for_bug_report();
} else {
last_meter_reading = now;
next_meter_join = now + METER_REJOIN_INTERVAL;
}
break;
case 'j': // Meter join
handle_resp_meter_join();
led_wifi(true);
if (startup_step == 3) {
send_meter_request();
startup_step++;
}
break;
case 'f':
if (!handle_resp_firmware_ver()) {
led_wifi(true);
if (startup_step == 0) {
startup_step++;
send_mac_req();
next_meter_request = now + update_interval_;
}
}
break;
case 'm': // Mac address
if (!handle_resp_mac_address()) {
led_wifi(true);
if (startup_step == 1) {
startup_step++;
send_install_code_req();
next_meter_request = now + update_interval_;
}
}
break;
case 'i':
if (!handle_resp_install_code()) {
led_wifi(true);
if (startup_step == 2) {
startup_step++;
send_meter_request();
next_meter_request = now + update_interval_;
}
}
break;
case 'e':
// Unknown response type, but we can ignore.
ESP_LOGI(TAG, "Got 'e'-type message with value: %d", input_buffer.data[4]);
break;
default:
ESP_LOGE(TAG, "Unhandled response type '%c'", msg_type);
ESP_LOG_BUFFER_HEXDUMP(TAG, input_buffer.data, msg_len, ESP_LOG_ERROR);
break;
} }
pos = 0; last_reading_has_error = 0;
} handle_resp_meter_reading();
if (last_reading_has_error) {
if (mgm_firmware_ver < 1 && now >= next_version_request) { ask_for_bug_report();
// Something's wrong, do the startup sequence again. } else {
startup_step = 0; last_meter_reading = now;
send_version_req(); next_meter_join = now + METER_REJOIN_INTERVAL;
next_version_request = now + 1; // Wait a second.
}
if (now >= next_meter_request) {
// Handle initial startup delay
if (next_meter_request == 0) {
next_meter_request = now + INITIAL_STARTUP_DELAY;
next_meter_join = next_meter_request + METER_REJOIN_INTERVAL;
return;
} }
break;
// Schedule the next MGM message case 'j': // Meter join
next_meter_request = now + update_interval_; handle_resp_meter_join();
led_wifi(true);
if (now > next_meter_join) { if (startup_step == 3) {
startup_step = 9; // Cancel startup messages send_meter_request();
send_meter_join(); startup_step++;
next_meter_join = now + METER_REJOIN_INTERVAL;
return;
} }
break;
if (startup_step == 0) send_version_req(); case 'f':
else if (startup_step == 1) send_mac_req(); if (!handle_resp_firmware_ver()) {
else if (startup_step == 2) send_install_code_req(); led_wifi(true);
else if (startup_step == 3) send_meter_join(); if (startup_step == 0) {
else send_meter_request(); startup_step++;
send_mac_req();
next_meter_request = now + update_interval_;
}
}
break;
case 'm': // Mac address
if (!handle_resp_mac_address()) {
led_wifi(true);
if (startup_step == 1) {
startup_step++;
send_install_code_req();
next_meter_request = now + update_interval_;
}
}
break;
case 'i':
if (!handle_resp_install_code()) {
led_wifi(true);
if (startup_step == 2) {
startup_step++;
send_meter_request();
next_meter_request = now + update_interval_;
}
}
break;
case 'e':
// Unknown response type, but we can ignore.
ESP_LOGI(TAG, "Got 'e'-type message with value: %d",
input_buffer.data[4]);
break;
default:
ESP_LOGE(TAG, "Unhandled response type '%c'", msg_type);
ESP_LOG_BUFFER_HEXDUMP(TAG, input_buffer.data, msg_len, ESP_LOG_ERROR);
break;
} }
pos = 0;
}
if (mgm_firmware_ver < 1 && now >= next_version_request) {
// Something's wrong, do the startup sequence again.
startup_step = 0;
send_version_req();
next_version_request = now + 1; // Wait a second.
}
if (now >= next_meter_request) {
// Handle initial startup delay
if (next_meter_request == 0) {
next_meter_request = now + INITIAL_STARTUP_DELAY;
next_meter_join = next_meter_request + METER_REJOIN_INTERVAL;
return;
}
// Schedule the next MGM message
next_meter_request = now + update_interval_;
if (now > next_meter_join) {
startup_step = 9; // Cancel startup messages
send_meter_join();
next_meter_join = now + METER_REJOIN_INTERVAL;
return;
}
if (startup_step == 0)
send_version_req();
else if (startup_step == 1)
send_mac_req();
else if (startup_step == 2)
send_install_code_req();
else if (startup_step == 3)
send_meter_join();
else
send_meter_request();
}
} }
void EmporiaVueUtility::dump_config() { void EmporiaVueUtility::dump_config() {
ESP_LOGCONFIG(TAG, "Emporia Vue Utility config dump WIP"); ESP_LOGCONFIG(TAG, "Emporia Vue Utility config dump WIP");
} }
} // namespace emporia_vue_utility } // namespace emporia_vue_utility
} // namespace esphome } // namespace esphome

File diff suppressed because it is too large Load Diff

View File

@@ -10,7 +10,7 @@ from esphome.const import (
DEVICE_CLASS_POWER, DEVICE_CLASS_POWER,
DEVICE_CLASS_ENERGY, DEVICE_CLASS_ENERGY,
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING STATE_CLASS_TOTAL_INCREASING,
) )
DEPENDENCIES = ["uart"] DEPENDENCIES = ["uart"]
@@ -29,8 +29,10 @@ ENERGY_SENSOR_TYPES = {
ALL_SENSOR_TYPES = {**POWER_SENSOR_TYPES, **ENERGY_SENSOR_TYPES} ALL_SENSOR_TYPES = {**POWER_SENSOR_TYPES, **ENERGY_SENSOR_TYPES}
emporia_vue_utility_ns = cg.esphome_ns.namespace('emporia_vue_utility') emporia_vue_utility_ns = cg.esphome_ns.namespace("emporia_vue_utility")
EmporiaVueUtility = emporia_vue_utility_ns.class_('EmporiaVueUtility', cg.PollingComponent, uart.UARTDevice) EmporiaVueUtility = emporia_vue_utility_ns.class_(
"EmporiaVueUtility", cg.PollingComponent, uart.UARTDevice
)
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
cv.Schema( cv.Schema(
@@ -53,13 +55,14 @@ CONFIG_SCHEMA = cv.All(
accuracy_decimals=0, accuracy_decimals=0,
) )
for name in ENERGY_SENSOR_TYPES for name in ENERGY_SENSOR_TYPES
} },
} }
) )
.extend(cv.polling_component_schema('30s')) .extend(cv.polling_component_schema("30s"))
.extend(uart.UART_DEVICE_SCHEMA) .extend(uart.UART_DEVICE_SCHEMA)
) )
async def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config) await cg.register_component(var, config)
@@ -68,4 +71,4 @@ async def to_code(config):
for key, funcName in ALL_SENSOR_TYPES.items(): for key, funcName in ALL_SENSOR_TYPES.items():
if key in config: if key in config:
sens = await sensor.new_sensor(config[key]) sens = await sensor.new_sensor(config[key])
cg.add(getattr(var, funcName)(sens)) cg.add(getattr(var, funcName)(sens))