From f64200686f402157e525158f6133763bdfdc2b58 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 5 Dec 2025 20:00:48 +0000 Subject: added queue_publish logic to switch set method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/entity_switch.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src') 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 @@ -use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, SwitchState, constants}; +use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, SwitchCommand, SwitchState, constants}; #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub enum SwitchClass { @@ -65,13 +65,26 @@ impl<'a> Switch<'a> { } pub fn set(&mut self, state: BinaryState) { - self.0.with_data(|data| { + let publish = self.0.with_data(|data| { let storage = data.storage.as_switch_mut(); + let timestamp = embassy_time::Instant::now(); + let publish = match &storage.command { + Some(command) => command.value != state, + None => true, + }; storage.state = Some(SwitchState { value: state, - timestamp: embassy_time::Instant::now(), + timestamp, }); - }) + storage.command = Some(SwitchCommand { + value: state, + timestamp, + }); + publish + }); + if publish { + self.0.queue_publish(); + } } pub async fn wait(&mut self) -> BinaryState { -- cgit