diff options
Diffstat (limited to 'src/command_policy.rs')
| -rw-r--r-- | src/command_policy.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/command_policy.rs b/src/command_policy.rs new file mode 100644 index 0000000..049d56a --- /dev/null +++ b/src/command_policy.rs | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | /// Determines how an entity handles commands received from Home Assistant. | ||
| 2 | /// | ||
| 3 | /// This policy controls whether an entity automatically publishes its state when it receives | ||
| 4 | /// a command from Home Assistant, or if the application should handle state updates manually. | ||
| 5 | /// | ||
| 6 | /// # Variants | ||
| 7 | /// | ||
| 8 | /// ## `PublishState` (Default) | ||
| 9 | /// | ||
| 10 | /// When a command is received from Home Assistant, the entity automatically: | ||
| 11 | /// 1. Updates its internal state to match the command value | ||
| 12 | /// 2. Publishes the new state back to Home Assistant | ||
| 13 | /// | ||
| 14 | /// This is useful for simple entities where the command should immediately be reflected as the | ||
| 15 | /// current state, such as: | ||
| 16 | /// - A switch that turns on/off immediately when commanded | ||
| 17 | /// - A number input that updates its value when changed in the UI | ||
| 18 | /// | ||
| 19 | /// ## `Manual` | ||
| 20 | /// | ||
| 21 | /// When a command is received from Home Assistant, the entity: | ||
| 22 | /// 1. Stores the command for the application to read via `wait()` or `command()` | ||
| 23 | /// 2. Does NOT automatically update or publish the state | ||
| 24 | /// | ||
| 25 | /// The application must manually update the entity's state after processing the command. | ||
| 26 | /// This is useful when: | ||
| 27 | /// - The command triggers an action that may fail (e.g., turning on a motor) | ||
| 28 | /// - The actual state may differ from the commanded state | ||
| 29 | /// - You need to validate or transform the command before applying it | ||
| 30 | /// | ||
| 31 | /// # Examples | ||
| 32 | /// | ||
| 33 | /// ## Auto-publish (default) | ||
| 34 | /// | ||
| 35 | /// ```no_run | ||
| 36 | /// # use embassy_ha::{CommandPolicy, SwitchConfig}; | ||
| 37 | /// let config = SwitchConfig { | ||
| 38 | /// command_policy: CommandPolicy::PublishState, // or just use default | ||
| 39 | /// ..Default::default() | ||
| 40 | /// }; | ||
| 41 | /// // When Home Assistant sends "ON", the switch state automatically becomes "ON" | ||
| 42 | /// ``` | ||
| 43 | /// | ||
| 44 | /// ## Manual control | ||
| 45 | /// | ||
| 46 | /// ```no_run | ||
| 47 | /// # use embassy_ha::{CommandPolicy, SwitchConfig, BinaryState, Switch}; | ||
| 48 | /// # async fn example(mut switch: Switch<'_>) { | ||
| 49 | /// let config = SwitchConfig { | ||
| 50 | /// command_policy: CommandPolicy::Manual, | ||
| 51 | /// ..Default::default() | ||
| 52 | /// }; | ||
| 53 | /// | ||
| 54 | /// loop { | ||
| 55 | /// let command = switch.wait().await; | ||
| 56 | /// | ||
| 57 | /// // Try to perform the action | ||
| 58 | /// if turn_on_motor().await.is_ok() { | ||
| 59 | /// // Only update state if the action succeeded | ||
| 60 | /// switch.set(command); | ||
| 61 | /// } | ||
| 62 | /// } | ||
| 63 | /// # } | ||
| 64 | /// # async fn turn_on_motor() -> Result<(), ()> { Ok(()) } | ||
| 65 | /// ``` | ||
| 66 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 67 | pub enum CommandPolicy { | ||
| 68 | /// Automatically publish the entity's state when a command is received. | ||
| 69 | PublishState, | ||
| 70 | |||
| 71 | /// Do not automatically publish state. The application must manually update the state. | ||
| 72 | Manual, | ||
| 73 | } | ||
| 74 | |||
| 75 | impl Default for CommandPolicy { | ||
| 76 | fn default() -> Self { | ||
| 77 | Self::PublishState | ||
| 78 | } | ||
| 79 | } | ||
