From b7ce3e0679817e0fbfec2eface4b21d652621825 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 5 Dec 2025 19:35:41 +0000 Subject: added publish_on_command option to number --- examples/number.rs | 3 ++- src/entity_number.rs | 15 ++++++++++++--- src/lib.rs | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/examples/number.rs b/examples/number.rs index 19f11fa..2231be1 100644 --- a/examples/number.rs +++ b/examples/number.rs @@ -34,6 +34,7 @@ async fn main_task(spawner: Spawner) { step: Some(0.5), mode: embassy_ha::NumberMode::Slider, class: embassy_ha::NumberClass::Distance, + ..Default::default() }, ); @@ -46,7 +47,7 @@ async fn main_task(spawner: Spawner) { async fn number_task(mut number: embassy_ha::Number<'static>) { loop { let value = number.wait().await; - number.set(value); + tracing::info!("value = {}", value); Timer::after_secs(1).await; } } diff --git a/src/entity_number.rs b/src/entity_number.rs index f96c3c7..e2a89c1 100644 --- a/src/entity_number.rs +++ b/src/entity_number.rs @@ -70,6 +70,7 @@ pub struct NumberConfig { pub step: Option, pub mode: NumberMode, pub class: NumberClass, + pub publish_on_command: bool, } impl Default for NumberConfig { @@ -82,6 +83,7 @@ impl Default for NumberConfig { step: None, mode: NumberMode::Auto, class: NumberClass::Generic, + publish_on_command: true, } } } @@ -169,24 +171,31 @@ impl<'a> Number<'a> { Self(entity) } - pub fn get(&mut self) -> Option { + pub fn state(&mut self) -> Option { self.0.with_data(|data| { let storage = data.storage.as_number_mut(); storage.state.as_ref().map(|s| s.value) }) } + pub fn command(&self) -> Option { + self.0.with_data(|data| { + let storage = data.storage.as_number_mut(); + storage.command.as_ref().map(|s| s.value) + }) + } + pub async fn wait(&mut self) -> f32 { loop { self.0.wait_command().await; - match self.get() { + match self.command() { Some(value) => return value, None => continue, } } } - pub fn set(&mut self, value: f32) { + pub fn publish(&mut self, value: f32) { let publish = self.0.with_data(|data| { let storage = data.storage.as_number_mut(); let timestamp = embassy_time::Instant::now(); diff --git a/src/lib.rs b/src/lib.rs index 3ebb190..1571255 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -264,6 +264,7 @@ pub struct NumberCommand { pub struct NumberStorage { pub state: Option, pub command: Option, + pub publish_on_command: bool, } #[derive(Debug)] @@ -456,7 +457,13 @@ impl<'a> Device<'a> { entity_config.id = id; config.populate(&mut entity_config); - let entity = self.create_entity(entity_config, EntityStorage::Number(Default::default())); + let entity = self.create_entity( + entity_config, + EntityStorage::Number(NumberStorage { + publish_on_command: config.publish_on_command, + ..Default::default() + }), + ); Number::new(entity) } @@ -494,7 +501,10 @@ impl<'a> Device<'a> { pub async fn run(&mut self, transport: &mut T) -> Result<(), Error> { let mut client = embedded_mqtt::Client::new(self.mqtt_resources, transport); if let Err(err) = client.connect(self.config.device_id).await { - crate::log::error!("mqtt connect failed with: {:?}", crate::log::Debug2Format(&err)); + crate::log::error!( + "mqtt connect failed with: {:?}", + crate::log::Debug2Format(&err) + ); return Err(Error::new("mqtt connection failed")); } @@ -565,7 +575,11 @@ impl<'a> Device<'a> { mode: entity_config.mode, device: &device_discovery, }; - crate::log::debug!("discovery for entity '{}': {:?}", entity_config.id, discovery); + crate::log::debug!( + "discovery for entity '{}': {:?}", + entity_config.id, + discovery + ); self.discovery_buffer .resize(self.discovery_buffer.capacity(), 0) @@ -679,7 +693,10 @@ impl<'a> Device<'a> { embassy_futures::select::Either::First(packet) => match packet { Ok(embedded_mqtt::Packet::Publish(publish)) => publish, Err(err) => { - crate::log::error!("mqtt receive failed with: {:?}", crate::log::Debug2Format(&err)); + crate::log::error!( + "mqtt receive failed with: {:?}", + crate::log::Debug2Format(&err) + ); return Err(Error::new("mqtt receive failed")); } _ => continue, @@ -797,9 +814,17 @@ impl<'a> Device<'a> { continue; } }; + let timestamp = embassy_time::Instant::now(); + if number_storage.publish_on_command { + data.publish = true; + number_storage.state = Some(NumberState { + value: command, + timestamp, + }); + } number_storage.command = Some(NumberCommand { value: command, - timestamp: embassy_time::Instant::now(), + timestamp, }); } _ => continue 'outer_loop, -- cgit