aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-12-05 20:00:48 +0000
committerdiogo464 <[email protected]>2025-12-05 20:00:48 +0000
commitf64200686f402157e525158f6133763bdfdc2b58 (patch)
tree76678476f8bf6012215b9cfe8df1006d408e6581 /src
parentb7ce3e0679817e0fbfec2eface4b21d652621825 (diff)
added queue_publish logic to switch set method
Updates switch's set method to only queue publish when the state value actually changes, matching the behavior in entity_number. This prevents unnecessary MQTT publishes when setting the same state multiple times. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/entity_switch.rs21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/entity_switch.rs b/src/entity_switch.rs
index 4bf77eb..1cb3647 100644
--- a/src/entity_switch.rs
+++ b/src/entity_switch.rs
@@ -1,4 +1,4 @@
1use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, SwitchState, constants}; 1use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, SwitchCommand, SwitchState, constants};
2 2
3#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] 3#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
4pub enum SwitchClass { 4pub enum SwitchClass {
@@ -65,13 +65,26 @@ impl<'a> Switch<'a> {
65 } 65 }
66 66
67 pub fn set(&mut self, state: BinaryState) { 67 pub fn set(&mut self, state: BinaryState) {
68 self.0.with_data(|data| { 68 let publish = self.0.with_data(|data| {
69 let storage = data.storage.as_switch_mut(); 69 let storage = data.storage.as_switch_mut();
70 let timestamp = embassy_time::Instant::now();
71 let publish = match &storage.command {
72 Some(command) => command.value != state,
73 None => true,
74 };
70 storage.state = Some(SwitchState { 75 storage.state = Some(SwitchState {
71 value: state, 76 value: state,
72 timestamp: embassy_time::Instant::now(), 77 timestamp,
73 }); 78 });
74 }) 79 storage.command = Some(SwitchCommand {
80 value: state,
81 timestamp,
82 });
83 publish
84 });
85 if publish {
86 self.0.queue_publish();
87 }
75 } 88 }
76 89
77 pub async fn wait(&mut self) -> BinaryState { 90 pub async fn wait(&mut self) -> BinaryState {