Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
device_registry.cpp
Go to the documentation of this file.
1/// @file device_registry.cpp
2/// @brief DeviceRegistry implementation.
3/// @ingroup hioc_hub
4
5#include "device_registry.h"
6
7#include "proto_frame.h"
8#include "proto_sizes.h"
9
10#include "esphome/core/log.h"
11
12#include <cstring>
13
14namespace esphome {
15namespace home_io_control {
16
17static constexpr const char *TAG = "home_io_control";
18
19void DeviceRegistry::add(const std::string &device_id) { add(device_id, DeviceConfig{}); }
20
21void DeviceRegistry::add(const std::string &device_id, const DeviceConfig &cfg) {
22 if (devices_.count(device_id) != 0)
23 return;
24 IoDevice dev{};
25 if (!hex_to_bytes(device_id, dev.node_id, NODE_ID_SIZE)) {
26 ESP_LOGW(TAG, "Ignoring invalid device ID %s", device_id.c_str());
27 return;
28 }
29 dev.type = cfg.type;
30 dev.subtype = cfg.subtype;
31 if (cfg.inverted)
32 dev.inverted = true;
34 dev.silent = cfg.silent;
35 dev.low_power = cfg.low_power;
38 if (cfg.has_system_key)
39 memcpy(dev.system_key, cfg.system_key, AES_KEY_SIZE);
40 devices_[device_id] = dev;
41}
42
43void DeviceRegistry::put(const std::string &device_id, IoDevice device) { devices_[device_id] = device; }
44
45IoDevice *DeviceRegistry::get(const std::string &device_id) {
46 auto it = devices_.find(device_id);
47 return (it != devices_.end()) ? &it->second : nullptr;
48}
49
50IoDevice *DeviceRegistry::get_declared(const std::string &device_id) {
51 auto it = devices_.find(device_id);
52 return (it != devices_.end() && !it->second.registered_by_pairing) ? &it->second : nullptr;
53}
54
55bool DeviceRegistry::remove_registered_by_pairing(const std::string &device_id) {
56 auto it = devices_.find(device_id);
57 if (it == devices_.end() || !it->second.registered_by_pairing)
58 return false;
59 devices_.erase(it);
60 return true;
61}
62
63void DeviceRegistry::set_dimmable(const std::string &device_id, bool dimmable) {
64 if (IoDevice *dev = this->get(device_id))
65 dev->dimmable = dimmable;
66}
67
68void DeviceRegistry::set_silent(const std::string &device_id, bool silent) {
69 if (IoDevice *dev = this->get(device_id))
70 dev->silent = silent;
71}
72
73void DeviceRegistry::subscribe(DeviceUpdateCallback cb) { callbacks_.push_back(std::move(cb)); }
74
75void DeviceRegistry::notify(const std::string &device_id) {
76 auto it = devices_.find(device_id);
77 if (it == devices_.end())
78 return;
79 for (auto &cb : callbacks_)
80 cb(device_id, it->second);
81}
82
83void DeviceRegistry::add_linked_remote(const std::string &remote_id, const std::string &device_id) {
84 linked_remotes_[remote_id].push_back(device_id);
85}
86
87const std::vector<std::string> *DeviceRegistry::linked_devices(const std::string &remote_id) const {
88 auto it = linked_remotes_.find(remote_id);
89 return (it != linked_remotes_.end()) ? &it->second : nullptr;
90}
91
92void DeviceRegistry::add_linked_remote_class(DeviceType type, const std::string &device_id) {
93 linked_remote_classes_[type].push_back(device_id);
94}
95
96const std::vector<std::string> *DeviceRegistry::linked_devices_for_class(DeviceType type) const {
97 auto it = linked_remote_classes_.find(type);
98 return (it != linked_remote_classes_.end()) ? &it->second : nullptr;
99}
100
101bool DeviceRegistry::apply_optimistic_target(const std::string &device_id, float target_io_position) {
102 auto it = devices_.find(device_id);
103 if (it == devices_.end() || !it->second.optimistic_state)
104 return false;
105 // Logs the raw (non-inverted) IO-protocol values the entity layer will compare against —
106 // cross-check against platform_cover.cpp::on_device_update_()'s invert-aware direction math
107 // when verifying a specific device's optimistic behavior.
108 if (it->second.position == UNKNOWN_POSITION) {
109 ESP_LOGD(TAG, "Device %s: optimistic target=%.0f (inverted=%s, last reported position=unknown)", device_id.c_str(),
110 target_io_position, YESNO(it->second.inverted));
111 } else {
112 ESP_LOGD(TAG, "Device %s: optimistic target=%.0f (inverted=%s, last reported position=%.0f)", device_id.c_str(),
113 target_io_position, YESNO(it->second.inverted), it->second.position);
114 }
115 it->second.optimistic.target = target_io_position;
116 it->second.optimistic.motion = OptimisticState::Motion::MOVING;
117 it->second.optimistic.forget_replaced_movement(); // a new command supersedes any pending STOP's
118 notify(device_id);
119 return true;
120}
121
122bool DeviceRegistry::apply_optimistic_stop(const std::string &device_id, bool restorable) {
123 auto it = devices_.find(device_id);
124 if (it == devices_.end() || !it->second.optimistic_state)
125 return false;
126 OptimisticState &optimistic = it->second.optimistic;
127 ESP_LOGD(TAG, "Device %s: optimistic stop", device_id.c_str());
128 if (!restorable) {
129 optimistic.forget_replaced_movement();
130 } else if (optimistic.motion == OptimisticState::Motion::MOVING) {
131 // A repeated STOP (motion already STOPPED) keeps the movement the first one replaced.
132 optimistic.stop_replaced_movement = true;
133 optimistic.stop_replaced_target = optimistic.target;
134 }
135 optimistic.target = UNKNOWN_POSITION;
137 notify(device_id);
138 return true;
139}
140
141bool DeviceRegistry::apply_optimistic_tilt(const std::string &device_id, float tilt_percent) {
142 auto it = devices_.find(device_id);
143 if (it == devices_.end() || !it->second.optimistic_state)
144 return false;
145 // Type-gated here rather than at the call sites so every present and future caller is safe:
146 // showing an angle the device will never report back would be a lie the next poll cannot
147 // correct. This admits exactly the devices whose tilt command is accepted — the command gate
148 // known_device_accepts_execute_tilt() is this same predicate plus an UNKNOWN-type check that
149 // device_supports_tilt() already fails — so the two cannot disagree about a given device.
150 if (!device_supports_tilt(it->second.type))
151 return false;
152 if (it->second.tilt == UNKNOWN_POSITION) {
153 ESP_LOGD(TAG, "Device %s: optimistic tilt=%.0f (last reported tilt=unknown)", device_id.c_str(), tilt_percent);
154 } else {
155 ESP_LOGD(TAG, "Device %s: optimistic tilt=%.0f (last reported tilt=%.0f)", device_id.c_str(), tilt_percent,
156 it->second.tilt);
157 }
158 it->second.optimistic.tilt = tilt_percent;
159 notify(device_id);
160 return true;
161}
162
163bool DeviceRegistry::rollback_optimistic(const std::string &device_id, bool failed_stop) {
164 auto it = devices_.find(device_id);
165 if (it == devices_.end() || it->second.optimistic.empty())
166 return false;
167 OptimisticState &optimistic = it->second.optimistic;
168 if (failed_stop && optimistic.stop_replaced_movement) {
169 ESP_LOGD(TAG, "Device %s: optimistic rollback (stop failed, movement toward %.0f resumes)", device_id.c_str(),
170 optimistic.stop_replaced_target);
171 const float resumed_target = optimistic.stop_replaced_target;
172 optimistic.clear();
173 optimistic.target = resumed_target;
175 } else {
176 ESP_LOGD(TAG, "Device %s: optimistic rollback (command failed)", device_id.c_str());
177 optimistic.clear();
178 }
179 notify(device_id);
180 return true;
181}
182
183void DeviceRegistry::confirm_optimistic_stop(const std::string &device_id) {
184 auto it = devices_.find(device_id);
185 if (it != devices_.end())
186 it->second.optimistic.forget_replaced_movement();
187}
188
190 const std::function<void(const std::string &, const std::vector<std::string> &)> &fn) const {
191 for (const auto &pair : linked_remotes_)
192 fn(pair.first, pair.second);
193}
194
195} // namespace home_io_control
196} // namespace esphome
const std::vector< std::string > * linked_devices(const std::string &remote_id) const
Retrieve the list of device IDs linked to a remote.
bool rollback_optimistic(const std::string &device_id, bool failed_stop=false)
Withdraw every prediction for a device after the command that produced them failed,...
void set_dimmable(const std::string &device_id, bool dimmable)
Set a device's dimmable flag (see IoDevice::dimmable).
bool apply_optimistic_tilt(const std::string &device_id, float tilt_percent)
Set an optimistic slat angle ahead of a confirming poll, and notify.
IoDevice * get_declared(const std::string &device_id)
Retrieve a device the YAML declares, ignoring one a pairing added in this boot.
const std::vector< std::string > * linked_devices_for_class(DeviceType type) const
Retrieve the list of device IDs linked to a device class.
bool remove_registered_by_pairing(const std::string &device_id)
Remove a device a pairing added in this boot.
void add_linked_remote_class(DeviceType type, const std::string &device_id)
Record that a remote's typed-broadcast presses (e.g.
void put(const std::string &device_id, IoDevice device)
Insert or overwrite a device entry without deduplication or hex-validation checks.
bool apply_optimistic_target(const std::string &device_id, float target_io_position)
Set an optimistic target position ahead of a confirming poll/response, and notify.
void set_silent(const std::string &device_id, bool silent)
Select a device's travel profile.
void notify(const std::string &device_id)
Invoke all registered callbacks for device_id.
bool apply_optimistic_stop(const std::string &device_id, bool restorable=false)
Predict that a device has stopped (e.g.
void add_linked_remote(const std::string &remote_id, const std::string &device_id)
Record that a remote node controls a registered device.
void subscribe(DeviceUpdateCallback cb)
Register a callback that fires whenever a device's state changes.
IoDevice * get(const std::string &device_id)
Retrieve a registered device by ID.
void confirm_optimistic_stop(const std::string &device_id)
Mark a device's STOP as delivered: a later rollback must not restore the movement it replaced.
void for_each_linked_remote(const std::function< void(const std::string &, const std::vector< std::string > &)> &fn) const
Invoke fn(remote_id, device_id_list) for every linked-remote entry.
void add(const std::string &device_id)
Register a device by ID with default metadata (UNKNOWN type, subtype 0, not inverted).
Per-hub device table, update-callback fan-out, and linked-remote map.
static constexpr const char * TAG
std::function< void(const std::string &device_id, const IoDevice &device)> DeviceUpdateCallback
Callback type invoked when a device's state changes.
DeviceType
IO-Homecontrol node types.
bool device_supports_tilt(DeviceType type)
Does this device type support tilt (slat angle) control?
bool hex_to_bytes(const std::string &hex, uint8_t *out, uint8_t len)
Convert a hex string (e.g., "123ABC") to a byte array.
IO-Homecontrol 2W frame container: control bytes, IoFrame and (de)serialization.
Fundamental IO-Homecontrol frame and crypto size constants.
YAML-declared device metadata for registration; defaults match an undeclared device.
bool optimistic_state
Whether apply_optimistic_target/apply_optimistic_stop may fire.
bool has_system_key
system_key: declared: the device keeps a key of its own.
bool inverted
Position-inversion flag.
bool low_power
Target is a low-power / duty-cycled receiver: directed frames set CTRL1_LOW_POWER and use the long wa...
uint8_t system_key[AES_KEY_SIZE]
That key; meaningful only when has_system_key. Never logged.
bool battery_entities
battery: true declared: the battery entities exist for this device.
uint8_t subtype
Device subtype byte.
bool silent
Send position moves in "silent operation" (slower) mode.
DeviceType type
Device type from YAML declaration.
Runtime state of a paired IO‑Homecontrol device.
bool inverted
True if open/close positions are swapped (e.g., horizontal awning).
bool silent
True to send position moves with the reference hub's "silent operation" extended block,...
bool low_power
YAML-declared: this target is a low-power / duty-cycled receiver, so directed frames set CTRL1_LOW_PO...
uint8_t subtype
Device subtype (manufacturer‑specific).
bool battery_entities
YAML-declared (battery: true): the battery entities exist, so the status log line names the level eve...
uint8_t node_id[NODE_ID_SIZE]
Device's 3‑byte radio address.
bool dimmable
True for a LIGHT-class device configured dimmable: true in YAML.
uint8_t system_key[AES_KEY_SIZE]
The device's own key; meaningful only when has_system_key.
bool has_system_key
YAML-declared (system_key:): this device keeps a key of its own.
bool optimistic_state
True if target may be set ahead of a confirming poll/response.
DeviceType type
Device type (shutter, awning, etc.).
What the hub predicted ahead of confirmation, kept apart from what the device reported.
@ STOPPED
Predicted to be at rest (a STOP was issued).
@ MOVING
Predicted to be travelling (a position command was issued).
void clear()
Withdraw every prediction.
float stop_replaced_target
The replaced target; meaningful only with the flag.
Motion motion
Predicted movement state.
bool stop_replaced_movement
True while a pending STOP replaced a Motion::MOVING prediction that a failed STOP should bring back (...
void forget_replaced_movement()
Drop the movement a STOP replaced, once nothing can roll the STOP back any more.
float target
Predicted main-position target, or UNKNOWN_POSITION.