1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-License-Identifier: MPL-2.0
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! `TasmoR` Lib - A Rust library to control Tasmota devices.
//!
//! This library provides async APIs to interact with Tasmota-powered devices
//! via both HTTP and MQTT protocols.
//!
//! # Supported Features
//!
//! - **Power control**: Turn devices on/off, toggle, blink
//! - **Light control**: Dimmer, color temperature, HSB colors, fade effects
//! - **Status queries**: Device status, network info, firmware version
//! - **Energy monitoring**: Power consumption, voltage, current readings
//!
//! # Supported Modules
//!
//! - Generic (Module 18): Flexible GPIO configuration
//! - Neo Coolcam (Module 49): Smart plugs with energy monitoring
//!
//! # Feature Flags
//!
//! This library supports optional features to reduce compile time and binary size:
//!
//! - `http` - Enables HTTP protocol support (enabled by default)
//! - `mqtt` - Enables MQTT protocol support (enabled by default)
//!
//! Both features are enabled by default. To use only one protocol:
//!
//! ```toml
//! # HTTP only
//! tasmor_lib = { version = "0.5", default-features = false, features = ["http"] }
//!
//! # MQTT only
//! tasmor_lib = { version = "0.5", default-features = false, features = ["mqtt"] }
//! ```
//!
//! # Quick Start
//!
//! ## HTTP Device with Auto-Detection
//!
//! ```no_run
//! use tasmor_lib::Device;
//!
//! #[tokio::main]
//! async fn main() -> tasmor_lib::Result<()> {
//! // Create device with automatic capability detection
//! // Returns (device, initial_state) tuple
//! let (device, _initial_state) = Device::http("192.168.1.100")
//! .build()
//! .await?;
//!
//! // Basic power control
//! device.power_on().await?;
//!
//! // Check capabilities before using features
//! if device.capabilities().supports_dimmer_control() {
//! device.set_dimmer(tasmor_lib::Dimmer::new(75)?).await?;
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## HTTP Device with Manual Capabilities
//!
//! ```no_run
//! use tasmor_lib::{Device, Capabilities};
//!
//! #[tokio::main]
//! async fn main() -> tasmor_lib::Result<()> {
//! // Create device without probing (faster startup)
//! // Returns (device, initial_state) tuple
//! let (device, _initial_state) = Device::http("192.168.1.100")
//! .with_capabilities(Capabilities::rgbcct_light())
//! .build_without_probe()
//! .await?;
//!
//! device.power_on().await?;
//! Ok(())
//! }
//! ```
//!
//! ## MQTT Device
//!
//! ```no_run
//! use tasmor_lib::MqttBroker;
//!
//! #[tokio::main]
//! async fn main() -> tasmor_lib::Result<()> {
//! // Connect to MQTT broker
//! let broker = MqttBroker::builder()
//! .host("192.168.1.50")
//! .build()
//! .await?;
//!
//! // Create device - returns (device, initial_state) tuple
//! let (device, _initial_state) = broker.device("tasmota_switch")
//! .build()
//! .await?;
//!
//! device.power_toggle().await?;
//!
//! // Clean disconnect when done
//! device.disconnect().await;
//! broker.disconnect().await?;
//! Ok(())
//! }
//! ```
//!
//! ## MQTT Device with Callbacks (Event Subscriptions)
//!
//! MQTT devices support real-time event subscriptions via callbacks:
//!
//! ```no_run
//! use tasmor_lib::{MqttBroker, subscription::Subscribable};
//!
//! #[tokio::main]
//! async fn main() -> tasmor_lib::Result<()> {
//! let broker = MqttBroker::builder()
//! .host("192.168.1.50")
//! .build()
//! .await?;
//!
//! let (device, _initial_state) = broker.device("tasmota_switch")
//! .build()
//! .await?;
//!
//! // Subscribe to power state changes
//! device.on_power_changed(|relay_idx, state| {
//! println!("Relay {} is now {:?}", relay_idx, state);
//! });
//!
//! // Subscribe to dimmer changes
//! device.on_dimmer_changed(|value| {
//! println!("Dimmer set to {:?}", value);
//! });
//!
//! device.power_toggle().await?;
//!
//! // Clean disconnect when done
//! device.disconnect().await;
//! broker.disconnect().await?;
//! Ok(())
//! }
//! ```
//!
//! # HTTP vs MQTT: Choosing a Protocol
//!
//! This library supports two protocols for communicating with Tasmota devices.
//! Each has distinct characteristics suited to different use cases.
//!
//! ## Feature Comparison
//!
//! | Feature | HTTP | MQTT |
//! |---------|------|------|
//! | Connection type | Stateless (request/response) | Persistent (pub/sub) |
//! | Real-time events | ❌ Not supported | ✅ Full support |
//! | Event subscriptions | ❌ Compile-time error | ✅ [`Subscribable`] trait |
//! | Connection overhead | New connection per command | Single persistent connection |
//! | Network requirements | Direct device access | MQTT broker required |
//! | Firewall friendly | ✅ Standard HTTP/HTTPS | May require port forwarding |
//! | Multi-device efficiency | One connection per device | Shared broker connection |
//! | Device topic | N/A | ✅ `device.topic()` |
//! | Explicit disconnect | N/A | ✅ `device.disconnect()` |
//!
//! ## When to Use HTTP
//!
//! - **Simple scripts**: One-off commands or automation scripts
//! - **Direct device access**: No MQTT broker available
//! - **Firewall constraints**: Only HTTP ports are open
//! - **Low-frequency control**: Occasional commands without state tracking
//!
//! ```no_run
//! use tasmor_lib::Device;
//!
//! # async fn example() -> tasmor_lib::Result<()> {
//! // HTTP: Simple, direct control
//! let (device, _) = Device::http("192.168.1.100").build().await?;
//! device.power_on().await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## When to Use MQTT
//!
//! - **Real-time monitoring**: React to device state changes instantly
//! - **Home automation**: Integration with existing MQTT infrastructure
//! - **Multi-device setups**: Efficiently manage many devices via one broker
//! - **State synchronization**: Keep local state in sync with device state
//!
//! ```no_run
//! use tasmor_lib::{MqttBroker, subscription::Subscribable};
//!
//! # async fn example() -> tasmor_lib::Result<()> {
//! // MQTT: Real-time events and state tracking
//! let broker = MqttBroker::builder()
//! .host("192.168.1.50")
//! .build()
//! .await?;
//!
//! let (device, _initial_state) = broker.device("tasmota_plug")
//! .build()
//! .await?;
//!
//! // React to external changes (physical button, other apps, etc.)
//! device.on_power_changed(|idx, state| {
//! println!("Relay {idx} changed to {state:?}");
//! });
//!
//! // MQTT-specific methods
//! println!("Device topic: {}", device.topic());
//!
//! // Clean disconnect when done
//! device.disconnect().await;
//! assert!(device.is_disconnected());
//! # Ok(())
//! # }
//! ```
//!
//! ## Type Safety
//!
//! The protocol choice is encoded in the type system. Attempting to use
//! subscription methods on an HTTP device results in a **compile-time error**:
//!
//! ```compile_fail
//! use tasmor_lib::{Device, subscription::Subscribable};
//!
//! # async fn example() -> tasmor_lib::Result<()> {
//! let (device, _) = Device::http("192.168.1.100").build().await?;
//!
//! // This will NOT compile - HTTP devices don't implement Subscribable
//! device.on_power_changed(|idx, state| {
//! println!("Power changed");
//! });
//! # Ok(())
//! # }
//! ```
//!
//! [`Subscribable`]: subscription::Subscribable
// Core types
pub use ;
pub use Device;
pub use ;
pub use ;
// Protocol configuration
pub use HttpConfig;
pub use ;
// Command building (Routine only - other commands via Device methods)
pub use ;
// Response types (returned by Device methods)
pub use ;
// Subscriptions (MQTT only)
pub use ;
// Value types (parameters for commands and state)
pub use ;