Skip to content

Commit

Permalink
Add programming mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Sep 18, 2024
1 parent 76191c6 commit 62998a2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
35 changes: 31 additions & 4 deletions components/tc_bus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"TCBusSendAction", automation.Action
)

TCBusProgrammingModeAction = tc_bus_ns.class_(
"TCBusProgrammingModeAction", automation.Action
)

ReceivedCommandTrigger = tc_bus_ns.class_("ReceivedCommandTrigger", automation.Trigger.template())
CommandData = tc_bus_ns.struct(f"CommandData")

Expand All @@ -40,7 +44,8 @@
"select_device_group_reset": CommandType.COMMAND_TYPE_SELECT_DEVICE_GROUP_RESET,
"search_devices": CommandType.COMMAND_TYPE_SEARCH_DEVICES,
"found_device": CommandType.COMMAND_TYPE_FOUND_DEVICE,
"found_device_subsystem": CommandType.COMMAND_TYPE_FOUND_DEVICE_SUBSYSTEM
"found_device_subsystem": CommandType.COMMAND_TYPE_FOUND_DEVICE_SUBSYSTEM,
"programming_mode": CommandType.COMMAND_TYPE_PROGRAMMING_MODE,
}

CONF_TC_ID = "tc_bus"
Expand All @@ -61,6 +66,7 @@
CONF_DOOR_READINESS = "door_readiness"

CONF_ON_COMMAND = "on_command_action"
CONF_SET_PROGRAMMING_MODE = "set_programming_mode"

MULTI_CONF = False

Expand Down Expand Up @@ -148,7 +154,7 @@ def validate(config):

return config

tc_bus_SEND_SCHEMA = cv.All(
TC_BUS_SEND_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.use_id(TCBus),
Expand All @@ -161,7 +167,9 @@ def validate(config):
)

@automation.register_action(
"tc_bus.send", TCBusSendAction, tc_bus_SEND_SCHEMA
"tc_bus.send",
TCBusSendAction,
TC_BUS_SEND_SCHEMA
)
async def tc_bus_send_to_code(config, action_id, template_args, args):
parent = await cg.get_variable(config[CONF_ID])
Expand All @@ -183,4 +191,23 @@ async def tc_bus_send_to_code(config, action_id, template_args, args):
serial_number_template_ = await cg.templatable(config[CONF_SERIAL_NUMBER], args, cg.uint32)
cg.add(var.set_serial_number(serial_number_template_))

return var
return var


TC_BUS_PROGRAMMING_MODE_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.use_id(TCBus),
cv.Required(CONF_SET_PROGRAMMING_MODE): cv.templatable(cv.boolean)
}),
validate
)

@automation.register_action(
"tc_bus.set_programming_mode",
TCBusProgrammingModeAction,
TC_BUS_PROGRAMMING_MODE_SCHEMA
)
async def tc_bus_set_programming_mode_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)
12 changes: 12 additions & 0 deletions components/tc_bus/automation.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ namespace esphome
TCBusComponent *parent_;
};

template<typename... Ts> class TCBusProgrammingModeAction : public Action<Ts...>
{
public:
TCBusProgrammingModeAction(TCBusComponent *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(bool, programming_mode)

void play(Ts... x) { this->parent_->set_programming_mode(this->programming_mode_.value(x...)); }

protected:
TCBusComponent *parent_;
};

class ReceivedCommandTrigger : public Trigger<CommandData> {
public:
explicit ReceivedCommandTrigger(TCBusComponent *parent) {
Expand Down
13 changes: 13 additions & 0 deletions components/tc_bus/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ namespace esphome
command |= (address & 0xFF); // 0
break;

case COMMAND_TYPE_PROGRAMMING_MODE:
command |= (5 << 12); // 5
command |= 0x0040; // 04
command |= (address & 0x01); // 0/1
break;

default:
break;
}
Expand Down Expand Up @@ -227,6 +233,13 @@ namespace esphome
{
switch(second)
{
case 0:
if (((command >> 6) & 0xFF) == 0x40 || ((command >> 6) & 0xFF) == 0x41)
{
data.type = COMMAND_TYPE_PROGRAMMING_MODE;
}
break;

case 1:
data.type = COMMAND_TYPE_RESET;
break;
Expand Down
3 changes: 2 additions & 1 deletion components/tc_bus/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace esphome
COMMAND_TYPE_SELECT_DEVICE_GROUP_RESET,
COMMAND_TYPE_SEARCH_DEVICES,
COMMAND_TYPE_FOUND_DEVICE,
COMMAND_TYPE_FOUND_DEVICE_SUBSYSTEM
COMMAND_TYPE_FOUND_DEVICE_SUBSYSTEM,
COMMAND_TYPE_PROGRAMMING_MODE
};

struct CommandData {
Expand Down
16 changes: 16 additions & 0 deletions components/tc_bus/tc_bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ namespace esphome
if (this->door_readiness_ != nullptr) {
this->door_readiness_->publish_state(false);
}
} else if (cmd_data.type == COMMAND_TYPE_PROGRAMMING_MODE) {
ESP_LOGD(TAG, "Programming Mode: %s", YESNO(cmd_data.address == 1));
}

// Publish Command to Last Bus Command Sensor
Expand Down Expand Up @@ -506,5 +508,19 @@ namespace esphome
this->received_command_callback_.add(std::move(callback));
}

void TCBusComponent::set_programming_mode(bool enabled)
{
this->programming_mode_ = enabled;

if(enabled)
{
send_command_generate(COMMAND_TYPE_PROGRAMMING_MODE, 1, 0);
}
else
{
send_command_generate(COMMAND_TYPE_PROGRAMMING_MODE, 0, 0);
}
}

} // namespace tc_bus
} // namespace esphome
4 changes: 4 additions & 0 deletions components/tc_bus/tc_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ namespace esphome
void add_received_command_callback(std::function<void(CommandData)> &&callback);
CallbackManager<void(CommandData)> received_command_callback_{};

void set_programming_mode(bool enabled);

bool sending;

protected:
Expand All @@ -103,6 +105,8 @@ namespace esphome
text_sensor::TextSensor *hardware_version_{nullptr};
binary_sensor::BinarySensor *door_readiness_{nullptr};
std::string hardware_version_str_ = "Generic";

bool programming_mode_ = false;
};

} // namespace tc_bus
Expand Down

0 comments on commit 62998a2

Please sign in to comment.