30constexpr size_t EXECUTE_ACTION_BUF_SIZE = 40;
34constexpr uint8_t FORCE_OPEN_WIRE_POSITION = 0;
35constexpr uint8_t FORCE_OPEN_WIRE_POSITION_INVERTED = 100;
41const char *position_command_action(
const IoDevice &dev, uint8_t position) {
46 return "set position";
52 return active_state ?
"turn on" :
"turn off";
54 return active_state ?
"unlock" :
"lock";
61 return active_state ?
"open" :
"close";
70const char *operation_profile_name(
const IoDevice &dev) {
72 return "dimmable_light";
80const char *position_rejection_profile(
const IoDevice &dev, uint8_t position) {
100 bool (*accepts)(
const IoDevice &dev);
101 const char *rejection_noun;
102 const char *expected;
105constexpr QueueGuard QUEUE_GUARD_COVER{
107 "queued cover command",
"cover entity"};
109 "queued tilt command",
"tilt-capable cover"};
110constexpr QueueGuard QUEUE_GUARD_POSITION_AND_TILT{
112 "tilt-capable cover"};
113constexpr QueueGuard QUEUE_GUARD_LIGHT{
115 "queued light command",
"light entity"};
116constexpr QueueGuard QUEUE_GUARD_LOCK{
118 "queued lock command",
"lock entity"};
119constexpr QueueGuard QUEUE_GUARD_SWITCH{
121 "queued switch command",
"switch entity"};
122constexpr QueueGuard QUEUE_GUARD_STATUS{
124 "status-capable actuator"};
130bool queue_guard_rejects(
IOHomeControlComponent *hub,
const std::string &device_id,
const QueueGuard &guard) {
131 const IoDevice *dev = hub->get_device(device_id);
132 if (dev !=
nullptr && !guard.accepts(*dev)) {
142 uint32_t
const existing = this->
poll_policy_.get_next_update(device_id);
145 if (existing != 0 && existing < millis() + delay_ms)
146 this->
poll_policy_.set_next_update(device_id, existing);
153 bool warn_on_no_response, uint32_t retry_after_fail_ms,
164 const bool unconfirmed_counts_as_success = request.
cmd ==
CMD_EXECUTE;
172 if (retry_after_fail_ms != 0)
175 if (warn_on_no_response) {
176 ESP_LOGW(
detail::TAG,
"Command 0x%02X failed for device %s: no valid response (stage=%s tries=%u)", request.
cmd,
177 device_id.c_str(), dbg.stage, dbg.tries);
188 if (retry_after_fail_ms != 0)
200 if (retry_after_fail_ms != 0)
212 const IoFrame &response, uint32_t retry_after_fail_ms) {
221 }
else if (dev !=
nullptr) {
228 if (retry_after_fail_ms != 0)
234 const std::function<
bool(
const IoDevice &)> &accepts,
235 const char *rejection_profile,
244 this->
registry_.rollback_optimistic(device_id);
249 const std::function<
bool(
const IoDevice &)> &accepts,
250 const char *rejection_profile,
259 if (!accepts(*dev)) {
266 ESP_LOGI(
detail::TAG,
"Sending %s to device %s (profile=%s)", spec.
action, device_id.c_str(),
267 operation_profile_name(*dev));
270 if (!build(request, *dev)) {
283 const auto *dev = this->
get_device(device_id);
289 device_id, {position_command_action(*dev, position), position ==
POS_STOP},
291 position_rejection_profile(*dev, position),
308 d.inverted ? FORCE_OPEN_WIRE_POSITION_INVERTED : FORCE_OPEN_WIRE_POSITION)
314 char action[EXECUTE_ACTION_BUF_SIZE];
315 snprintf(action,
sizeof(action),
"tilt=%u%%", tilt_percent);
318 "tilt-capable cover",
325 uint8_t tilt_percent) {
326 char action[EXECUTE_ACTION_BUF_SIZE];
327 snprintf(action,
sizeof(action),
"position=%u%% tilt=%u%%", position, tilt_percent);
330 "tilt-capable cover",
360 const bool scheduler_managed = this->
poll_policy_.is_tracking_active(device_id, millis());
362 const uint8_t max_tries =
364 this->poll_policy_.get_auth_poll_failures(device_id))
441 if (payload_len == 0) {
442 ESP_LOGW(
detail::TAG,
"Heating command %s for device %s rejected: value %.2f out of range",
473 }
else if (acknowledged) {
481 ESP_LOGW(
detail::TAG,
"Heating command %s not acknowledged by device %s (no CMD_WRITE_PRIVATE_ACK)",
499 if (queue_guard_rejects(
this, device_id, QUEUE_GUARD_COVER)) {
503 this->
registry_.rollback_optimistic(device_id);
508 uint8_t pending_tilt = 0;
511 pending_tilt = op.position;
515 if (this->op_queue_.enqueue_set_position(device_id, position)) {
517 "Coalesced SET_POSITION (pos=%u) + pending SET_TILT (tilt=%u) → SET_POSITION_AND_TILT for "
519 position, pending_tilt, device_id.c_str());
528 this->
registry_.rollback_optimistic(device_id);
535 this->
registry_.rollback_optimistic(device_id);
538 this->
op_queue_.enqueue_device_command(device_id, cmd);
543 if (queue_guard_rejects(
this, device_id, QUEUE_GUARD_TILT)) {
545 this->
registry_.rollback_optimistic(device_id);
550 uint8_t pending_pos = 0;
553 pending_pos = op.position;
557 if (this->op_queue_.enqueue_set_tilt(device_id, tilt_percent)) {
559 "Coalesced pending SET_POSITION (pos=%u) + SET_TILT (tilt=%u) → "
560 "SET_POSITION_AND_TILT for device %s",
561 pending_pos, tilt_percent, device_id.c_str());
566 uint8_t tilt_percent) {
567 if (queue_guard_rejects(
this, device_id, QUEUE_GUARD_POSITION_AND_TILT)) {
569 this->
registry_.rollback_optimistic(device_id);
572 this->
op_queue_.enqueue_set_position_and_tilt(device_id, position, tilt_percent);
576 if (queue_guard_rejects(
this, device_id, QUEUE_GUARD_STATUS))
580 this->
op_queue_.enqueue_request_status(device_id);
586 this->
op_queue_.enqueue_request_name(device_id);
607 result.
action =
"scan_paired_devices";
608 result.
message =
"a radio exchange was already in progress; press the button again once it finishes";
616 if (queue_guard_rejects(
this, device_id, QUEUE_GUARD_LIGHT))
618 this->
op_queue_.enqueue_set_light_position(device_id, position);
626 if (queue_guard_rejects(
this, device_id, QUEUE_GUARD_LOCK))
628 this->
op_queue_.enqueue_set_lock_state(device_id, locked);
632 if (queue_guard_rejects(
this, device_id, QUEUE_GUARD_SWITCH))
634 this->
op_queue_.enqueue_set_switch_state(device_id, on);
662 if (!opt.has_value())
666 switch (operation.
type) {
The main IO-Homecontrol component.
std::string describe_last_commander(const IoDevice &dev) const
Render a device's "last commanded by" string, resolving this hub's own node ID.
virtual bool set_lock_state(const std::string &device_id, bool locked)
Semantic lock helper for lock entities.
bool execute_request_and_update_(const std::string &device_id, const IoFrame &request, bool warn_on_no_response, uint32_t retry_after_fail_ms=0, uint8_t max_tries=EXCHANGE_RETRY_COUNT)
Shared request/response helper for high-level operations.
void execute_oneway_position_(const std::string &controller_id, uint8_t position)
Send a queued 1W numeric position.
virtual bool set_device_position_and_tilt(const std::string &device_id, uint8_t position, uint8_t tilt_percent)
Set both position and tilt of a tilt-capable cover in one atomic command.
virtual bool set_device_tilt(const std::string &device_id, uint8_t tilt_percent)
Send a tilt command to a tilt‑capable cover.
void execute_oneway_enroll_(const std::string &controller_id)
Send a queued 1W enrollment (add-controller).
virtual bool send_heating_command(const std::string &device_id, HeatingFunction fn, float value)
The single hub-side transmit path for 2W heating/climate control (CMD_WRITE_PRIVATE 0x20).
virtual bool set_switch_state(const std::string &device_id, bool on)
Semantic binary helper for switch entities.
bool run_execute_operation_(const std::string &device_id, const ExecuteRequestSpec &spec, const std::function< bool(const IoDevice &)> &accepts, const char *rejection_profile, const std::function< bool(IoFrame &, const IoDevice &)> &build)
Funnel for the four execute-family operations: runs try_execute_operation_() and, on any false return...
void arm_execute_confirmation_poll_(const std::string &device_id, bool for_stop)
Arm the confirming poll that follows a command, because a CMD_EXECUTE reply is never trusted for posi...
virtual void queue_set_device_tilt(const std::string &device_id, uint8_t tilt_percent)
Queue an async tilt update; returns immediately, executed in loop().
void execute_oneway_(F &&send)
Shared bookkeeping for every 1W transmit: mark the radio busy for the duration of send,...
void begin_status_poll_tracking_(const std::string &device_id, uint32_t initial_delay_ms)
Begin bounded follow-up polling for a device after a command or overheard remote activity.
bool handle_error_response_(const std::string &device_id, const IoFrame &request, const IoFrame &response, uint32_t retry_after_fail_ms)
Handle an explicit CMD_ERROR_RESP refusal from the device: record the result code,...
virtual IoDevice * get_device(const std::string &device_id)
Retrieve a device by ID; returns nullptr if not found.
virtual void queue_request_device_status(const std::string &device_id)
Queue an async status request; returns immediately, executed in loop().
ExchangeOutcome send_and_receive_(const IoFrame &request, IoFrame &response, uint32_t freq, uint8_t max_tries=EXCHANGE_RETRY_COUNT)
Main request/response exchange with retry and automatic authentication.
void process_pending_operation_()
Pop next pending operation from the queue and execute it (set position, request status,...
StatusPollPolicy poll_policy_
void execute_oneway_unenroll_(const std::string &controller_id)
Send a queued 1W un-enrollment (remove-controller).
void update_device_status_(const IoFrame &frame, bool trust_position=true)
Extract supported position or metadata info from a response frame and merge it into the device record...
virtual void queue_request_device_name(const std::string &device_id)
Queue an async device-name request; returns immediately, executed in loop().
virtual void queue_set_lock_state(const std::string &device_id, bool locked)
Async form of set_lock_state() that keeps radio work serialized on the main loop.
virtual void queue_set_light_position(const std::string &device_id, uint8_t position)
Async form of set_light_position() that keeps radio work serialized on the main loop.
bool try_execute_operation_(const std::string &device_id, const ExecuteRequestSpec &spec, const std::function< bool(const IoDevice &)> &accepts, const char *rejection_profile, const std::function< bool(IoFrame &, const IoDevice &)> &build)
Shared skeleton for the four execute-family operations (position, named command, tilt,...
void log_exchange_debug_(const char *device_id) const
Log the last exchange debug snapshot (delegates to exchange_engine_).
void schedule_background_poll_backoff_(const std::string &device_id, bool auth_like)
Apply backoff after a failed background status poll and log the result.
OneWayTransmitter oneway_transmitter_
Owns the 1W controller identities, their rolling-sequence counters and the transmit burst.
virtual void queue_set_device_position_and_tilt(const std::string &device_id, uint8_t position, uint8_t tilt_percent)
Queue an async combined position+tilt update; returns immediately, executed in loop().
virtual bool queue_device_command(const std::string &device_id, CoverCommand cmd)
Queue an async named command (STOP, FAVORITE, VENT, FORCE_OPEN); returns immediately,...
virtual bool discover_and_pair()
Discover and pair a device that is in pairing mode.
ExchangeEngine exchange_engine_
Owns all authenticated exchange and LBT/hop logic.
virtual bool set_light_state(const std::string &device_id, bool on)
Semantic binary helper for light entities.
esphome::home_io_control::ManagementActionResult ManagementActionResult
Result payload used by hub-level management actions such as rename.
ManagementActions management_actions_
Owns rename, identify, force-open, scan_paired_devices, and other hub-level HA actions.
virtual void queue_set_light_state(const std::string &device_id, bool on)
Async form of set_light_state() that keeps radio work serialized on the main loop.
void notify_device_update_(const std::string &id)
Fire all registered device update callbacks for the given device ID.
virtual void queue_set_device_position(const std::string &device_id, uint8_t position)
Queue an async position update; returns immediately, executed in loop().
virtual void queue_set_switch_state(const std::string &device_id, bool on)
Async form of set_switch_state() that keeps radio work serialized on the main loop.
virtual bool set_device_position(const std::string &device_id, uint8_t position)
Send a position command to a device.
uint8_t node_id_[NODE_ID_SIZE]
virtual bool request_device_name(const std::string &device_id)
Request the stored device name from a device.
bool execute_device_command_(const std::string &device_id, CoverCommand cmd)
Execute a named device command (STOP, FAVORITE, VENT, FORCE_OPEN) via the authenticated exchange.
void trigger_scan_paired_devices()
Entry point for the "Scan Paired Devices" button: run the roll-call and publish its report to the log...
virtual void queue_discover_and_pair()
Queue a pairing operation; executed in loop() when radio idle.
virtual bool request_device_status(const std::string &device_id)
Request current status from a device.
virtual bool set_light_position(const std::string &device_id, uint8_t position)
Send an arbitrary IO position (0-100) to a light entity.
void execute_oneway_command_(const std::string &controller_id, CoverCommand cmd)
Send a queued 1W named command.
Pure transition helpers for hub-owned exchange and pairing frame decisions.
Internal helpers shared by the hub implementation .cpp files.
uint8_t scheduled_poll_max_tries(uint8_t status_poll_failures, uint8_t auth_poll_failures)
Transmit-attempt budget for a scheduler-owned status poll, by backoff-ladder position.
bool known_device_accepts_execute_tilt(const IoDevice &dev)
Can this device accept a tilt command?
bool known_device_matches_entity_class(const IoDevice &dev, DeviceCapabilityClass expected)
Does the device's type match the expected HA entity class?
void log_rejected_operation(const std::string &device_id, const IoDevice &dev, const char *operation, const char *expected)
Log a rejected operation with capability mismatch details.
bool known_device_accepts_execute_position(const IoDevice &dev, uint8_t position)
Can this device accept an execute (position) command?
constexpr const char * TAG
Shared log tag for hub-level messages.
bool known_device_supports_status_requests(const IoDevice &dev)
Does the device support status requests?
void record_exchange_timeout(IoDevice &dev, uint8_t tries)
Record that an outbound exchange to this device timed out (no valid response).
void update_link_health(IoDevice &dev, RadioDriver *radio)
Update per-device link-health stats from the radio's last capture.
bool is_binary_entity_position(uint8_t position)
Is the given position value an on/off binary encoding?
std::string describe_last_commander(const IoDevice &dev, const uint8_t *hub_node_id)
Render the "Last Commanded By" sensor string.
void log_frame_issue(IOHomeControlComponent *component, const char *direction, const char *reason, const IoFrame &frame, uint8_t len)
Log a frame‑level issue (unregistered endpoints, unsupported commands).
void clear_command_result(IoDevice &dev)
Clear a previously recorded CMD_ERROR_RESP result, if any.
void log_command_result(const std::string &id, uint8_t result, uint8_t request_cmd=0, bool include_request_cmd=false)
Log a decoded CMD_ERROR_RESP result with optional request-command context.
void record_command_result(IoDevice &dev, const std::string &id, uint8_t result, uint8_t request_cmd=0, bool include_request_cmd=false)
Store a decoded CMD_ERROR_RESP result on the device and log it.
bool create_force_open(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, uint8_t open_position)
Build a force-open execute frame (0x00): an ordinary position command to the device's wire-scale "ful...
const char * device_operation_profile_name(DeviceType type)
Human‑readable operation profile name for a device type.
bool create_get_name(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power)
Build a get-name request (0x50).
uint32_t settle_delay_ms(uint32_t interval_ms, uint32_t hint_delay_ms, bool cap_for_stop)
Resolve the follow-up settle-poll delay while a device may still be moving.
void bytes_to_hex(const uint8_t *data, uint8_t len, char *out, size_t out_size)
bool create_get_status(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power)
Build a get-status request (0x03). The device responds with its current position.
constexpr size_t HEATING_PAYLOAD_MAX_SIZE
Largest payload any function produces — SET_TEMPERATURE's 6-byte form (iohcCozyDevice2W....
bool create_write_private(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, const uint8_t *payload, size_t payload_len)
Build a generic CMD_WRITE_PRIVATE (0x20) frame around a caller-supplied payload — the one builder beh...
@ SET_LOCK_STATE
set_lock_state call (locked/unlocked).
@ SET_TILT
set_device_tilt call (tilt percentage 0–100).
@ SET_POSITION_AND_TILT
Combined set_device_position_and_tilt call.
@ SET_LIGHT_STATE
set_light_state call (binary on/off).
@ SET_SWITCH_STATE
set_switch_state call (binary on/off).
@ REQUEST_NAME
request_device_name call (poll for stored device name).
@ ONEWAY_COMMAND
1W named command sent as a controller identity.
@ DISCOVER_AND_PAIR
discover_and_pair call (starts 3-phase pairing flow).
@ REQUEST_STATUS
request_device_status call (poll for current position).
@ ONEWAY_UNENROLL
1W remove-controller (0x39) un-registering an identity.
@ DEVICE_COMMAND
Named device command (STOP, FAVORITE, VENT).
@ SET_POSITION
set_device_position call (position 0–100 or special values).
@ ONEWAY_ENROLL
1W add-controller (0x30) registering an identity.
@ ONEWAY_POSITION
1W numeric position sent as a controller identity.
static constexpr uint8_t CMD_ERROR_RESP
Error response to any command.
size_t encode_heating_payload(HeatingFunction fn, float value, uint8_t out[HEATING_PAYLOAD_MAX_SIZE])
Encode one heating function into a CMD_WRITE_PRIVATE (0x20) payload.
HeatingFunction
Heating functions, one per user-pressable radiator button in the reference.
DeviceCapabilityClass device_capability_class(DeviceType type)
Map a raw IO‑Homecontrol type to the closest ESPHome/Home Assistant entity family.
CoverCommand
Named device commands for cover-type actuators.
@ STOP
Stop movement immediately.
@ FORCE_OPEN
Move to fully open at elevated priority; intended to bypass soft locks and environmental limits (conf...
bool device_supports_climate_control(DeviceType type)
Does this device type support 2W climate/heating control (CMD_WRITE_PRIVATE 0x20)?
bool create_execute_position_and_tilt(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, uint8_t position, uint8_t tilt_percent)
Build a combined position-and-tilt execute command (0x00) — setClosureAndOrientation.
static constexpr uint8_t CMD_WRITE_PRIVATE
Write private register (climate/heating devices).
const char * heating_function_name(HeatingFunction fn)
Stable lowercase name for a heating function ("power_on", "set_temperature", ...).
uint8_t frame_length(const IoFrame &f)
Get total frame length from ctrl0.
static constexpr uint8_t CMD_WRITE_PRIVATE_ACK
Acknowledgment to CMD_WRITE_PRIVATE.
bool create_execute_tilt(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, uint8_t tilt_percent)
Build a tilt execute command (0x00) for devices that support slat angle control.
bool create_execute_position(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, uint8_t position, bool silent)
Build a position execute command (0x00) to move a device to a numeric position.
const char * cover_command_name(CoverCommand cmd)
Get a human-readable name for a CoverCommand.
bool create_execute_command(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power, CoverCommand cmd, bool silent)
Build a named-command execute frame (0x00) for STOP, FAVORITE, or VENT.
ExchangeOutcome
Authenticated exchange engine — outbound and inbound protocol flows.
@ SUCCESS_WITH_RESPONSE
Device replied; the caller's response frame is populated.
@ SUCCESS_UNCONFIRMED
Device authenticated the request — so it received and accepted it — but sent no final response.
@ FAILED
No usable reply; the device may never have heard the request.
constexpr size_t FRAME_LOG_HEX_BUFFER_SIZE
Fits a full 32-byte frame rendered as spaced hex text.
static constexpr uint32_t STATUS_RETRY_AFTER_FAIL_MS
First retry after a silent failure.
static constexpr uint8_t BINARY_ENTITY_ON_POSITION
Position value written for binary ON commands (light on, switch on, lock unlock).
static constexpr uint8_t EXCHANGE_RETRY_COUNT
Attempts per command before reporting failure.
static constexpr uint8_t CMD_EXECUTE
Set position/open/close/stop — requires authentication.
static constexpr uint32_t FREQ_CH2
Channel 2: 868.95 MHz (1W and 2W, TX channel).
bool create_get_status_tilt(IoFrame &f, const uint8_t *own, const uint8_t *dst, bool low_power)
Build a tilt-aware get-status request (0x03) that returns the extended 16-byte tilt payload.
@ CLIMATE
Climate device (heating/cooling).
@ SWITCH
Binary on/off switch.
@ UNKNOWN
Unknown capability.
@ COVER
Position‑controlled cover (shutter/blind/awning).
@ LIGHT
Binary on/off light.
static constexpr uint8_t POS_STOP
Position values in the IO protocol.
static constexpr uint8_t BINARY_ENTITY_OFF_POSITION
Position value written for binary OFF commands (light off, switch off, lock lock).
bool device_supports_tilt(DeviceType type)
Does this device type support tilt (slat angle) control?
Command builders for the IO‑Homecontrol protocol.
Everything one execute-family operation needs beyond its own guard and frame builder.
const char * action
Verb/phrase for the "Sending ..." and rejection logs.
bool settle_as_stop
Passed through to arm_execute_confirmation_poll_().
Runtime state of a paired IO‑Homecontrol device.
Parsed IO‑Homecontrol frame (CTRL0/1 + addresses + command + data).
uint8_t data[FRAME_MAX_DATA_SIZE]
Command parameters (0–23 bytes). Never includes mac.
uint8_t data_len
Actual length of data.
std::string action
Action name, e.g. "rename_device".
std::string message
Human-readable outcome summary.
A single queued operation to be dispatched from loop().
CoverCommand command
Named command for DEVICE_COMMAND operations.
std::string device_id
Target device ID (hex string, e.g., "123ABC") — except for the ONEWAY_* types, where it carries the c...
uint8_t tilt
Tilt value for SET_POSITION_AND_TILT (0–100).
uint8_t position
Position/tilt value (0–100) or binary state (ON=0, OFF=100).
PendingOperationType type
Operation type (determines which handler to invoke).