Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
operation_queue.h
Go to the documentation of this file.
1#pragma once
2
3/// @file operation_queue.h
4/// @brief Pending-operation queue with per-type coalescing and deduplication.
5/// @ingroup hioc_hub
6///
7/// Owns the PendingOperationType / PendingOperation types and the coalescing
8/// rules applied when operations are enqueued. Pure data and logic — no ESPHome
9/// dependencies, fully host-testable.
10
11#include "proto_device_model.h"
12
13#include <cstdint>
14#include <deque>
15#include <optional>
16#include <string>
17
18namespace esphome {
19namespace home_io_control {
20
21/// Position value written for binary ON commands (light on, switch on, lock unlock).
22static constexpr uint8_t BINARY_ENTITY_ON_POSITION = 0;
23/// Position value written for binary OFF commands (light off, switch off, lock lock).
24static constexpr uint8_t BINARY_ENTITY_OFF_POSITION = 100;
25
26/// @brief Discriminator for entries in the pending-operation deque.
27enum class PendingOperationType : uint8_t {
28 SET_POSITION, ///< set_device_position call (position 0–100 or special values).
29 SET_TILT, ///< set_device_tilt call (tilt percentage 0–100).
30 SET_POSITION_AND_TILT, ///< Combined set_device_position_and_tilt call.
31 DEVICE_COMMAND, ///< Named device command (STOP, FAVORITE, VENT).
32 SET_LIGHT_STATE, ///< set_light_state call (binary on/off).
33 SET_LOCK_STATE, ///< set_lock_state call (locked/unlocked).
34 SET_SWITCH_STATE, ///< set_switch_state call (binary on/off).
35 SET_FAN_POSITION, ///< set_fan_position call (heat level of an exterior heater as an IO position).
36 ONEWAY_COMMAND, ///< 1W named command sent as a controller identity.
37 ONEWAY_POSITION, ///< 1W numeric position sent as a controller identity.
38 ONEWAY_ENROLL, ///< 1W add-controller (0x30) registering an identity.
39 ONEWAY_UNENROLL, ///< 1W remove-controller (0x39) un-registering an identity.
40 REQUEST_STATUS, ///< request_device_status call (poll for current position).
41 REQUEST_NAME, ///< request_device_name call (poll for stored device name).
42 REQUEST_LIMITATION, ///< request_device_limitation call (read the device's minimum limitation).
43 DISCOVER_AND_PAIR, ///< discover_and_pair call (starts 3-phase pairing flow).
44};
45
46/// @brief A single queued operation to be dispatched from loop().
48 PendingOperationType type; ///< Operation type (determines which handler to invoke).
49 /// Target device ID (hex string, e.g., "123ABC") — **except** for the ONEWAY_* types, where it
50 /// carries the controller-identity handle instead. 1W addresses a device class, not a node, so
51 /// there is no device ID to put here; the identity is what the operation is bound to. The field
52 /// is reused rather than duplicated because every queue entry would otherwise grow a second
53 /// string for the benefit of two operation types.
54 std::string device_id;
55 uint8_t position{0}; ///< Position/tilt value (0–100) or binary state (ON=0, OFF=100).
56 uint8_t tilt{0}; ///< Tilt value for SET_POSITION_AND_TILT (0–100).
57 CoverCommand command{CoverCommand::STOP}; ///< Named command for DEVICE_COMMAND operations.
58};
59
60/// @brief Serialized pending-operation queue with coalescing, deduplication, and two-band ordering.
61///
62/// **Ordering:** DISCOVER_AND_PAIR > control operations > background polls.
63/// Control operations (SET_*, DEVICE_COMMAND) are inserted before any queued background polls
64/// (REQUEST_STATUS, REQUEST_NAME, REQUEST_LIMITATION) so user-visible commands execute with minimum queue latency.
65/// DISCOVER_AND_PAIR always front-inserts ahead of all other entries.
66///
67/// **Stale-poll drop:** enqueueing a control operation for device X drops any queued
68/// REQUEST_STATUS for X, since the command reply supersedes it and re-arms tracking. A queued
69/// REQUEST_LIMITATION is kept: a status reply does not answer it.
70///
71/// Coalescing rules (both directions):
72/// - SET_POSITION arriving while SET_TILT is pending for the same device → SET_POSITION_AND_TILT.
73/// - SET_TILT arriving while SET_POSITION is pending for the same device → SET_POSITION_AND_TILT.
74///
75/// Deduplication rules:
76/// - At most one REQUEST_STATUS per device.
77/// - At most one REQUEST_NAME per device.
78/// - At most one REQUEST_LIMITATION per device.
79/// - At most one DISCOVER_AND_PAIR (every pending background poll is flushed
80/// and the discover entry is pushed to the front to minimize pairing-window latency).
81///
82/// All coalescing and deduplication log messages are suppressed here — callers are responsible
83/// for emitting them because OperationQueue must remain free of ESPHome logging dependencies.
85 public:
86 // --- Position / tilt (with coalescing) ---
87
88 /// Enqueue SET_POSITION, or coalesce with a pending SET_TILT into SET_POSITION_AND_TILT.
89 /// @return true if coalesced (caller should log the merge), false if a new entry was pushed.
90 bool enqueue_set_position(const std::string &device_id, uint8_t position);
91
92 /// Enqueue SET_TILT, or coalesce with a pending SET_POSITION into SET_POSITION_AND_TILT.
93 /// @return true if coalesced (caller should log the merge), false if a new entry was pushed.
94 bool enqueue_set_tilt(const std::string &device_id, uint8_t tilt_percent);
95
96 /// Enqueue SET_POSITION_AND_TILT directly (no coalescing needed).
97 void enqueue_set_position_and_tilt(const std::string &device_id, uint8_t position, uint8_t tilt_percent);
98
99 // --- Named cover command ---
100 void enqueue_device_command(const std::string &device_id, CoverCommand cmd);
101
102 // --- Binary entity commands (no coalescing) ---
103 /// Enqueue SET_LIGHT_STATE with an arbitrary IO position (0-100), for dimmable lights.
104 /// enqueue_set_light_state() is a thin binary-position wrapper around this.
105 void enqueue_set_light_position(const std::string &device_id, uint8_t position);
106 void enqueue_set_light_state(const std::string &device_id, bool on);
107 /// Enqueue SET_FAN_POSITION with an IO position (0-100; 0 = full heat, 100 = off).
108 void enqueue_set_fan_position(const std::string &device_id, uint8_t position);
109 void enqueue_set_lock_state(const std::string &device_id, bool locked);
110 void enqueue_set_switch_state(const std::string &device_id, bool on);
111
112 // --- 1W commands (no coalescing, no deduplication) ---
113
114 /// Enqueue a 1W named command for a controller identity.
115 ///
116 /// Deliberately neither coalesced nor deduplicated, unlike their 2W counterparts. A 2W command
117 /// can be superseded because the device reports back what it did; a 1W command cannot be
118 /// confirmed at all, so dropping one is dropping a press the user made with nothing to notice
119 /// it. Each press also consumes its own sequence, and merging two would leave a gap that looks
120 /// like a lost frame to a device tracking the counter.
121 /// @param controller_id Controller-identity handle (see PendingOperation::device_id).
122 /// @param cmd Named command to send.
123 void enqueue_oneway_command(const std::string &controller_id, CoverCommand cmd);
124
125 /// Enqueue a 1W numeric position for a controller identity.
126 /// @param controller_id Controller-identity handle (see PendingOperation::device_id).
127 /// @param position Target position 0–100.
128 void enqueue_oneway_position(const std::string &controller_id, uint8_t position);
129
130 /// Enqueue a 1W enrollment (add-controller) for a controller identity. Same no-coalesce,
131 /// no-dedup contract as the other 1W ops, for the same reason.
132 /// @param controller_id Controller-identity handle (see PendingOperation::device_id).
133 void enqueue_oneway_enroll(const std::string &controller_id);
134
135 /// Enqueue a 1W un-enrollment (remove-controller) for a controller identity.
136 /// @param controller_id Controller-identity handle (see PendingOperation::device_id).
137 void enqueue_oneway_unenroll(const std::string &controller_id);
138
139 // --- Background polls (with deduplication) ---
140
141 /// Enqueue REQUEST_STATUS, suppressing duplicates.
142 /// @return true if pushed, false if a duplicate was already pending.
143 bool enqueue_request_status(const std::string &device_id);
144
145 /// Enqueue REQUEST_NAME, suppressing duplicates.
146 /// @return true if pushed, false if a duplicate was already pending.
147 bool enqueue_request_name(const std::string &device_id);
148
149 /// Enqueue REQUEST_LIMITATION, suppressing duplicates.
150 /// @return true if pushed, false if a duplicate was already pending.
151 bool enqueue_request_limitation(const std::string &device_id);
152
153 // --- Pairing (dedup + priority front-push) ---
154
155 /// Enqueue DISCOVER_AND_PAIR with elevated priority.
156 /// Flushes every pending background poll (see is_background_op()) and pushes to front.
157 /// @return true if pushed, false if a DISCOVER_AND_PAIR was already pending.
159
160 // --- Queue access ---
161 [[nodiscard]] std::optional<PendingOperation> pop();
162 [[nodiscard]] bool empty() const;
163 [[nodiscard]] std::size_t size() const;
164
165 // --- Read-only inspection ---
166 [[nodiscard]] const PendingOperation &front() const;
167 [[nodiscard]] const PendingOperation &back() const;
168 [[nodiscard]] const PendingOperation &operator[](std::size_t index) const;
169 [[nodiscard]] std::deque<PendingOperation>::const_iterator begin() const;
170 [[nodiscard]] std::deque<PendingOperation>::const_iterator end() const;
171
172 /// True for background poll types (REQUEST_STATUS, REQUEST_NAME, REQUEST_LIMITATION) that yield to control
173 /// operations. Public because the dispatch gate in loop() defers *only* background work when a 1W remote is
174 /// transmitting; a user command must never be held back for it.
175 [[nodiscard]] static bool is_background_op(PendingOperationType t);
176
177 private:
178 std::deque<PendingOperation> queue_;
179 /// Insert a control operation before the first background entry; also drops any queued
180 /// REQUEST_STATUS for the same device since the incoming command supersedes it.
181 void push_control_(PendingOperation op);
182 /// Append a background operation of @p type for @p device_id unless an identical one is already
183 /// queued. @return true if pushed, false if a duplicate was already pending.
184 bool enqueue_background_unique_(PendingOperationType type, const std::string &device_id);
185};
186
187} // namespace home_io_control
188} // namespace esphome
Serialized pending-operation queue with coalescing, deduplication, and two-band ordering.
std::deque< PendingOperation >::const_iterator begin() const
const PendingOperation & front() const
void enqueue_oneway_unenroll(const std::string &controller_id)
Enqueue a 1W un-enrollment (remove-controller) for a controller identity.
void enqueue_set_lock_state(const std::string &device_id, bool locked)
void enqueue_set_position_and_tilt(const std::string &device_id, uint8_t position, uint8_t tilt_percent)
Enqueue SET_POSITION_AND_TILT directly (no coalescing needed).
void enqueue_oneway_command(const std::string &controller_id, CoverCommand cmd)
Enqueue a 1W named command for a controller identity.
std::optional< PendingOperation > pop()
void enqueue_device_command(const std::string &device_id, CoverCommand cmd)
bool enqueue_request_status(const std::string &device_id)
Enqueue REQUEST_STATUS, suppressing duplicates.
bool enqueue_request_name(const std::string &device_id)
Enqueue REQUEST_NAME, suppressing duplicates.
bool enqueue_request_limitation(const std::string &device_id)
Enqueue REQUEST_LIMITATION, suppressing duplicates.
void enqueue_set_fan_position(const std::string &device_id, uint8_t position)
Enqueue SET_FAN_POSITION with an IO position (0-100; 0 = full heat, 100 = off).
void enqueue_set_switch_state(const std::string &device_id, bool on)
void enqueue_oneway_position(const std::string &controller_id, uint8_t position)
Enqueue a 1W numeric position for a controller identity.
bool enqueue_discover_and_pair()
Enqueue DISCOVER_AND_PAIR with elevated priority.
static bool is_background_op(PendingOperationType t)
True for background poll types (REQUEST_STATUS, REQUEST_NAME, REQUEST_LIMITATION) that yield to contr...
std::deque< PendingOperation >::const_iterator end() const
bool enqueue_set_position(const std::string &device_id, uint8_t position)
Enqueue SET_POSITION, or coalesce with a pending SET_TILT into SET_POSITION_AND_TILT.
const PendingOperation & back() const
void enqueue_oneway_enroll(const std::string &controller_id)
Enqueue a 1W enrollment (add-controller) for a controller identity.
void enqueue_set_light_state(const std::string &device_id, bool on)
void enqueue_set_light_position(const std::string &device_id, uint8_t position)
Enqueue SET_LIGHT_STATE with an arbitrary IO position (0-100), for dimmable lights.
const PendingOperation & operator[](std::size_t index) const
bool enqueue_set_tilt(const std::string &device_id, uint8_t tilt_percent)
Enqueue SET_TILT, or coalesce with a pending SET_POSITION into SET_POSITION_AND_TILT.
PendingOperationType
Discriminator for entries in the pending-operation deque.
@ 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_FAN_POSITION
set_fan_position call (heat level of an exterior heater as an IO position).
@ 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).
@ REQUEST_LIMITATION
request_device_limitation call (read the device's minimum limitation).
@ ONEWAY_ENROLL
1W add-controller (0x30) registering an identity.
@ ONEWAY_POSITION
1W numeric position sent as a controller identity.
CoverCommand
Named device commands for cover-type actuators.
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 BINARY_ENTITY_OFF_POSITION
Position value written for binary OFF commands (light off, switch off, lock lock).
IO-Homecontrol device-type model, capabilities and runtime device state.
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).