10#include "esphome/core/log.h"
18static constexpr const char *
TAG =
"home_io_control";
23 if (devices_.count(device_id) != 0)
27 ESP_LOGW(
TAG,
"Ignoring invalid device ID %s", device_id.c_str());
44 devices_[device_id] = dev;
48 const auto existing = devices_.find(device_id);
49 if (existing != devices_.end()) {
52 device.
dimmable = existing->second.dimmable;
53 device.
model = existing->second.model;
56 devices_[device_id] = device;
60 auto it = devices_.find(device_id);
61 return (it != devices_.end()) ? &it->second :
nullptr;
65 auto it = devices_.find(device_id);
66 return (it != devices_.end() && !it->second.registered_by_pairing) ? &it->second :
nullptr;
70 auto it = devices_.find(device_id);
71 if (it == devices_.end() || !it->second.registered_by_pairing)
85 auto it = devices_.find(device_id);
86 if (it == devices_.end())
88 for (
auto &cb : callbacks_)
89 cb(device_id, it->second);
93 linked_remotes_[remote_id].push_back(device_id);
97 auto it = linked_remotes_.find(remote_id);
98 return (it != linked_remotes_.end()) ? &it->second :
nullptr;
102 linked_remote_classes_[type].push_back(device_id);
106 auto it = linked_remote_classes_.find(type);
107 return (it != linked_remote_classes_.end()) ? &it->second :
nullptr;
111 auto it = devices_.find(device_id);
112 if (it == devices_.end() || !it->second.optimistic_state)
117 if (it->second.position == UNKNOWN_POSITION) {
118 ESP_LOGD(
TAG,
"Device %s: optimistic target=%.0f (inverted=%s, last reported position=unknown)", device_id.c_str(),
119 target_io_position, YESNO(it->second.inverted));
121 ESP_LOGD(
TAG,
"Device %s: optimistic target=%.0f (inverted=%s, last reported position=%.0f)", device_id.c_str(),
122 target_io_position, YESNO(it->second.inverted), it->second.position);
124 it->second.optimistic.target = target_io_position;
126 it->second.optimistic.forget_replaced_movement();
132 auto it = devices_.find(device_id);
133 if (it == devices_.end() || !it->second.optimistic_state)
136 ESP_LOGD(
TAG,
"Device %s: optimistic stop", device_id.c_str());
144 optimistic.
target = UNKNOWN_POSITION;
151 auto it = devices_.find(device_id);
152 if (it == devices_.end() || !it->second.optimistic_state)
158 if (!
has_feature(it->second.features, device_feature::TILT))
160 if (it->second.tilt == UNKNOWN_POSITION) {
161 ESP_LOGD(
TAG,
"Device %s: optimistic tilt=%.0f (last reported tilt=unknown)", device_id.c_str(), tilt_percent);
163 ESP_LOGD(
TAG,
"Device %s: optimistic tilt=%.0f (last reported tilt=%.0f)", device_id.c_str(), tilt_percent,
166 it->second.optimistic.tilt = tilt_percent;
172 auto it = devices_.find(device_id);
173 if (it == devices_.end() || it->second.optimistic.empty())
177 ESP_LOGD(
TAG,
"Device %s: optimistic rollback (stop failed, movement toward %.0f resumes)", device_id.c_str(),
181 optimistic.
target = resumed_target;
184 ESP_LOGD(
TAG,
"Device %s: optimistic rollback (command failed)", device_id.c_str());
192 auto it = devices_.find(device_id);
193 if (it != devices_.end())
194 it->second.optimistic.forget_replaced_movement();
198 std::set<std::string> ids;
199 for (
const auto &pair : linked_remotes_)
200 ids.insert(pair.second.begin(), pair.second.end());
201 for (
const auto &pair : linked_remote_classes_)
202 ids.insert(pair.second.begin(), pair.second.end());
203 return {ids.begin(), ids.end()};
207 const std::function<
void(
const std::string &,
const std::vector<std::string> &)> &fn)
const {
208 for (
const auto &pair : linked_remotes_)
209 fn(pair.first, pair.second);
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,...
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.
std::vector< std::string > all_linked_devices() const
Every device with a linked_remotes entry, by remote id or by class:, once each, sorted.
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, no overrides).
void set_travel_speed(const std::string &device_id, TravelSpeed speed)
Select a device's travel speed.
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.
void resolve_device_features(IoDevice &dev)
Resolve a device's capability bits and inversion from its kind and its YAML overrides.
DeviceType
IO-Homecontrol node types.
constexpr bool has_feature(DeviceFeatures set, DeviceFeatures bit)
True when every bit of bit is set in set.
TravelSpeed
The speed a cover's position moves ask the motor for (ADR 0065).
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.
DeviceModel model
YAML io_model:; NONE when not declared.
DeviceCapabilityClass entity_class
Class of the declaring entity; decides the features of a kind the type table does not know.
bool low_power
Target is a low-power / duty-cycled receiver: directed frames set CTRL1_LOW_POWER and use the long wa...
bool dimmable
YAML dimmable: true of a light.
uint8_t system_key[AES_KEY_SIZE]
That key; meaningful only when has_system_key. Never logged.
std::optional< bool > invert_override
YAML invert_position: when declared; the kind's default otherwise.
bool battery_entities
battery: true declared: the battery entities exist for this device.
uint8_t subtype
Device subtype byte.
TravelSpeed travel_speed
Speed profile of position moves.
DeviceType type
Device type from YAML declaration.
Runtime state of a paired IO‑Homecontrol device.
DeviceCapabilityClass entity_class
Class of the entity that declared the device; UNKNOWN for a device no entity declared (the type's cla...
std::optional< bool > invert_override
YAML invert_position: when declared; wins over the kind's default.
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...
DeviceModel model
YAML-declared product model below the subtype (io_model:); never on the wire.
uint8_t node_id[NODE_ID_SIZE]
Device's 3‑byte radio address.
bool dimmable
YAML-declared dimmable: true of a LIGHT entity.
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.).
TravelSpeed travel_speed
Speed profile of position moves.
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.