Skip to content

Commit

Permalink
ewh: add error code reporting sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
dentra committed Jun 24, 2024
1 parent 7821c82 commit 7aac35b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
27 changes: 21 additions & 6 deletions components/ewh/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import switch
from esphome.const import CONF_ICON, CONF_ID, CONF_TIME_ID, ENTITY_CATEGORY_CONFIG
from esphome.components import sensor, switch
from esphome.const import (
CONF_ICON,
CONF_ID,
CONF_TIME_ID,
ENTITY_CATEGORY_CONFIG,
ENTITY_CATEGORY_DIAGNOSTIC,
)

from .. import rka_api # pylint: disable=relative-beyond-top-level

CODEOWNERS = ["@dentra"]
AUTO_LOAD = ["rka_api", "switch"]
AUTO_LOAD = ["rka_api", "switch", "sensor"]

CONF_BST = "bst"
CONF_EWH_ID = "ewh_id"
CONF_ERROR_CODE = "error_code"

# CONF_IDLE_TEMP_DROP = "idle_temp_drop"

Expand Down Expand Up @@ -42,6 +49,11 @@
BSTSwitch, entity_category=ENTITY_CATEGORY_CONFIG, block_inverted=True
),
# cv.Optional(CONF_IDLE_TEMP_DROP, default=5): cv.uint8_t,
cv.Optional(CONF_ERROR_CODE): sensor.sensor_schema(
icon="mdi:water-boiler-alert",
accuracy_decimals=0,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
).extend(cv.COMPONENT_SCHEMA)

Expand All @@ -52,13 +64,15 @@ async def new_ewh(config):
await cg.register_component(var, config)

if CONF_BST in config:
conf = config[CONF_BST]
sens = cg.new_Pvariable(conf[CONF_ID], api)
await switch.register_switch(sens, conf)
sens = await switch.new_switch(config[CONF_BST], api)
cg.add(var.set_bst(sens))

# cg.add(var.set_idle_temp_drop(config[CONF_IDLE_TEMP_DROP]))

if CONF_ERROR_CODE in config:
sens = await sensor.new_sensor(config[CONF_ERROR_CODE])
cg.add(var.set_error_code(sens))

return var


Expand All @@ -67,3 +81,4 @@ async def to_code(config):
if CONF_TIME_ID in config:
time_ = await cg.get_variable(config[CONF_TIME_ID])
cg.add(var.set_time_id(time_))

8 changes: 7 additions & 1 deletion components/ewh/ewh_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ void EWHComponent::on_state(const ewh_state_t &state) {
this->api_->set_clock(time.hour, time.minute);
}
#endif

#ifdef USE_SWITCH
if (this->bst_ != nullptr) {
this->bst_->publish_state(state.bst.state != ewh_bst_t::STATE_OFF);
}
#endif
#ifdef USE_SENSOR
if (this->error_code_) {
this->error_code_->publish_state(state.error);
}
#endif
}

// bool EWHComponent::is_idle_(const ewh_state_t &state) {
Expand Down
25 changes: 23 additions & 2 deletions components/ewh/ewh_component.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
#pragma once

#include "esphome/core/defines.h"
#include "esphome/core/component.h"

#ifdef USE_TIME
#include "esphome/components/time/real_time_clock.h"
#endif

#include "esphome/core/component.h"
#ifdef USE_SWITCH
#include "esphome/components/switch/switch.h"
#endif
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif

#include "../rka_api/rka_component.h"
#include "ewh_api.h"

namespace esphome {
namespace ewh {

#ifdef USE_SWITCH
#define LOG_EWH() LOG_SWITCH(" ", "BST Swtich", this->bst_)
#else
#define LOG_EWH()
#endif

using EWHComponentBase = rka_api::RKAComponent<rka_api::rka_dev_type_t::EWH, EWHListener, EWHApi>;

class EWHComponent : public EWHComponentBase {
public:
#ifdef USE_SWITCH
// BST (Bacteria Stop Technology) Switch.
class BSTSwitch : public switch_::Switch {
public:
Expand All @@ -29,6 +40,7 @@ class EWHComponent : public EWHComponentBase {
protected:
EWHApi *api_;
};
#endif

explicit EWHComponent(EWHApi *api) : EWHComponentBase(api) {}

Expand All @@ -37,15 +49,24 @@ class EWHComponent : public EWHComponentBase {
#ifdef USE_TIME
void set_time_id(esphome::time::RealTimeClock *time) { this->time_ = time; }
#endif

#ifdef USE_SWITCH
void set_bst(switch_::Switch *bst) { this->bst_ = bst; }
#endif
#ifdef USE_SENSOR
void set_error_code(sensor::Sensor *error_code) { this->error_code_ = error_code; }
#endif

void on_state(const ewh_state_t &state) override;

// void set_idle_temp_drop(uint8_t idle_temp_drop) { this->idle_temp_drop_ = idle_temp_drop; }

protected:
#ifdef USE_SWITCH
switch_::Switch *bst_{};
#endif
#ifdef USE_SENSOR
sensor::Sensor *error_code_{};
#endif
#ifdef USE_TIME
esphome::time::RealTimeClock *time_{};
#endif
Expand Down
8 changes: 6 additions & 2 deletions components/rka_api/uart_frame_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "esphome/core/helpers.h"
#include "esphome/core/hal.h"

#ifndef RKA_DUMP
#define RKA_DUMP ESP_LOGV
#endif

namespace esphome {
namespace rka_api {

Expand Down Expand Up @@ -85,7 +89,7 @@ class UartFrameIO {
if (uart->read_array(&frame->data[this->rx_.size], sizeof(crc_type))) {
ESP_LOGVV(TAG, "Read CRC: 0x%02X", frame->data[this->rx_.size] & 0xFF);
if (this->check_crc(frame)) {
ESP_LOGV(TAG, "RX: %s", format_hex_pretty(this->rx_.data, this->rx_.size + sizeof(rx_frame_t)).c_str());
RKA_DUMP(TAG, "RX: %s", format_hex_pretty(this->rx_.data, this->rx_.size + sizeof(rx_frame_t)).c_str());
if (this->reader_) {
this->reader_(frame->data, frame->size);
}
Expand Down Expand Up @@ -135,7 +139,7 @@ class UartFrameIO {
}
}
s += format_hex_pretty(reinterpret_cast<uint8_t *>(&crc), sizeof(crc));
ESP_LOGV(TAG, "TX: %s (%zu)", s.c_str(), sizeof(rx_frame_t) + size);
RKA_DUMP(TAG, "TX: %s (%zu)", s.c_str(), sizeof(rx_frame_t) + size);
#endif
}

Expand Down

0 comments on commit 7aac35b

Please sign in to comment.