Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions esphome/components/duco/select/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,48 @@
"MAN3x3",
]

CONF_BYPASS_MODE = "bypass_mode"

DUCO_BYPASS_MODE_OPTIONS = [
"AUTO",
"CLOSED",
"OPEN",
]

duco_ns = cg.esphome_ns.namespace("duco")
DucoSelect = duco_ns.class_("DucoSelect", cg.PollingComponent, select.Select)
DucoBypassModeSelect = duco_ns.class_(
"DucoBypassModeSelect", cg.PollingComponent, select.Select
)

CONFIG_SCHEMA = cv.All(
select.select_schema(DucoSelect)
.extend(cv.COMPONENT_SCHEMA)
.extend(cv.polling_component_schema("3s"))
.extend(DUCO_COMPONENT_SCHEMA)
.extend({cv.GenerateID(): cv.declare_id(DucoSelect)})
.extend(
{
cv.GenerateID(): cv.declare_id(DucoSelect),
cv.Optional(CONF_BYPASS_MODE): select.select_schema(DucoBypassModeSelect)
.extend(cv.COMPONENT_SCHEMA)
.extend(cv.polling_component_schema("60s"))
.extend({cv.GenerateID(): cv.declare_id(DucoBypassModeSelect)}),
}
)
)


async def to_code(config):
parent = await cg.get_variable(config[CONF_DUCO_ID])

var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)

await select.register_select(var, config, options=DUCO_MODE_OPTIONS)

parent = await cg.get_variable(config[CONF_DUCO_ID])
cg.add(var.set_parent(parent))

if CONF_BYPASS_MODE in config:
bypass_mode_config = config[CONF_BYPASS_MODE]
var = cg.new_Pvariable(bypass_mode_config[CONF_ID])
await cg.register_component(var, bypass_mode_config)
await select.register_select(var, bypass_mode_config, options=DUCO_BYPASS_MODE_OPTIONS)
cg.add(var.set_parent(parent))
59 changes: 59 additions & 0 deletions esphome/components/duco/select/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,64 @@ void DucoSelect::control(const std::string &value) {
this->parent_->send(message, this);
}

const std::string DucoBypassModeSelect::MODE_AUTO = "AUTO";
const std::string DucoBypassModeSelect::MODE_CLOSED = "CLOSED";
const std::string DucoBypassModeSelect::MODE_OPEN = "OPEN";

void DucoBypassModeSelect::setup() {}

void DucoBypassModeSelect::update() {
DucoMessage message;
message.function = 0x24;
message.data = {0x00, 0x10, 0x0a};
this->parent_->send(message, this);
}

float DucoBypassModeSelect::get_setup_priority() const {
// After DUCO
return setup_priority::BUS - 2.0f;
}

std::string bypass_mode_code_to_string(uint8_t mode) {
switch (mode) {
case DucoBypassModeSelect::MODE_CODE_CLOSED:
return DucoBypassModeSelect::MODE_CLOSED;
case DucoBypassModeSelect::MODE_CODE_OPEN:
return DucoBypassModeSelect::MODE_OPEN;
case DucoBypassModeSelect::MODE_CODE_AUTO:
default:
return DucoBypassModeSelect::MODE_AUTO;
}
}

uint8_t bypass_mode_string_to_code(const std::string &mode) {
if (mode == DucoBypassModeSelect::MODE_CLOSED) {
return DucoBypassModeSelect::MODE_CODE_CLOSED;
}
if (mode == DucoBypassModeSelect::MODE_OPEN) {
return DucoBypassModeSelect::MODE_CODE_OPEN;
}
return DucoBypassModeSelect::MODE_CODE_AUTO;
}

void DucoBypassModeSelect::receive_response(const DucoMessage &message) {
if (message.function == 0x26 && message.data.size() >= 4) {
uint8_t mode = message.data[3];
if (mode <= MODE_CODE_OPEN) {
publish_state(bypass_mode_code_to_string(mode));
ESP_LOGD(TAG, "Current bypass mode: %s", bypass_mode_code_to_string(mode).c_str());
}

this->parent_->stop_waiting(message.id);
}
}

void DucoBypassModeSelect::control(const std::string &value) {
DucoMessage message;
message.function = 0x24;
message.data = {0x01, 0x10, 0x0a, bypass_mode_string_to_code(value), 0x00, 0x00, 0x00};
this->parent_->send(message, this);
}

} // namespace duco
} // namespace esphome
20 changes: 20 additions & 0 deletions esphome/components/duco/select/select.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,25 @@ class DucoSelect : public DucoDevice, public PollingComponent, public select::Se
void control(const std::string &value) override;
};

class DucoBypassModeSelect : public DucoDevice, public PollingComponent, public select::Select {
public:
static const std::string MODE_AUTO;
static const std::string MODE_CLOSED;
static const std::string MODE_OPEN;

static const uint8_t MODE_CODE_AUTO = 0x00;
static const uint8_t MODE_CODE_CLOSED = 0x01;
static const uint8_t MODE_CODE_OPEN = 0x02;

void setup() override;
void update() override;

float get_setup_priority() const override;

void receive_response(const DucoMessage &message) override;

void control(const std::string &value) override;
};

} // namespace duco
} // namespace esphome