aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-12-05 19:35:41 +0000
committerdiogo464 <[email protected]>2025-12-05 19:35:41 +0000
commitb7ce3e0679817e0fbfec2eface4b21d652621825 (patch)
tree5590aa18d01e489a7873c8e63d94a0d23b622fcb /src
parentdae7894a23badc76ec571786b16a96fe24be46ac (diff)
added publish_on_command option to number
Diffstat (limited to 'src')
-rw-r--r--src/entity_number.rs15
-rw-r--r--src/lib.rs35
2 files changed, 42 insertions, 8 deletions
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 {
70 pub step: Option<f32>, 70 pub step: Option<f32>,
71 pub mode: NumberMode, 71 pub mode: NumberMode,
72 pub class: NumberClass, 72 pub class: NumberClass,
73 pub publish_on_command: bool,
73} 74}
74 75
75impl Default for NumberConfig { 76impl Default for NumberConfig {
@@ -82,6 +83,7 @@ impl Default for NumberConfig {
82 step: None, 83 step: None,
83 mode: NumberMode::Auto, 84 mode: NumberMode::Auto,
84 class: NumberClass::Generic, 85 class: NumberClass::Generic,
86 publish_on_command: true,
85 } 87 }
86 } 88 }
87} 89}
@@ -169,24 +171,31 @@ impl<'a> Number<'a> {
169 Self(entity) 171 Self(entity)
170 } 172 }
171 173
172 pub fn get(&mut self) -> Option<f32> { 174 pub fn state(&mut self) -> Option<f32> {
173 self.0.with_data(|data| { 175 self.0.with_data(|data| {
174 let storage = data.storage.as_number_mut(); 176 let storage = data.storage.as_number_mut();
175 storage.state.as_ref().map(|s| s.value) 177 storage.state.as_ref().map(|s| s.value)
176 }) 178 })
177 } 179 }
178 180
181 pub fn command(&self) -> Option<f32> {
182 self.0.with_data(|data| {
183 let storage = data.storage.as_number_mut();
184 storage.command.as_ref().map(|s| s.value)
185 })
186 }
187
179 pub async fn wait(&mut self) -> f32 { 188 pub async fn wait(&mut self) -> f32 {
180 loop { 189 loop {
181 self.0.wait_command().await; 190 self.0.wait_command().await;
182 match self.get() { 191 match self.command() {
183 Some(value) => return value, 192 Some(value) => return value,
184 None => continue, 193 None => continue,
185 } 194 }
186 } 195 }
187 } 196 }
188 197
189 pub fn set(&mut self, value: f32) { 198 pub fn publish(&mut self, value: f32) {
190 let publish = self.0.with_data(|data| { 199 let publish = self.0.with_data(|data| {
191 let storage = data.storage.as_number_mut(); 200 let storage = data.storage.as_number_mut();
192 let timestamp = embassy_time::Instant::now(); 201 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 {
264pub struct NumberStorage { 264pub struct NumberStorage {
265 pub state: Option<NumberState>, 265 pub state: Option<NumberState>,
266 pub command: Option<NumberCommand>, 266 pub command: Option<NumberCommand>,
267 pub publish_on_command: bool,
267} 268}
268 269
269#[derive(Debug)] 270#[derive(Debug)]
@@ -456,7 +457,13 @@ impl<'a> Device<'a> {
456 entity_config.id = id; 457 entity_config.id = id;
457 config.populate(&mut entity_config); 458 config.populate(&mut entity_config);
458 459
459 let entity = self.create_entity(entity_config, EntityStorage::Number(Default::default())); 460 let entity = self.create_entity(
461 entity_config,
462 EntityStorage::Number(NumberStorage {
463 publish_on_command: config.publish_on_command,
464 ..Default::default()
465 }),
466 );
460 Number::new(entity) 467 Number::new(entity)
461 } 468 }
462 469
@@ -494,7 +501,10 @@ impl<'a> Device<'a> {
494 pub async fn run<T: Transport>(&mut self, transport: &mut T) -> Result<(), Error> { 501 pub async fn run<T: Transport>(&mut self, transport: &mut T) -> Result<(), Error> {
495 let mut client = embedded_mqtt::Client::new(self.mqtt_resources, transport); 502 let mut client = embedded_mqtt::Client::new(self.mqtt_resources, transport);
496 if let Err(err) = client.connect(self.config.device_id).await { 503 if let Err(err) = client.connect(self.config.device_id).await {
497 crate::log::error!("mqtt connect failed with: {:?}", crate::log::Debug2Format(&err)); 504 crate::log::error!(
505 "mqtt connect failed with: {:?}",
506 crate::log::Debug2Format(&err)
507 );
498 return Err(Error::new("mqtt connection failed")); 508 return Err(Error::new("mqtt connection failed"));
499 } 509 }
500 510
@@ -565,7 +575,11 @@ impl<'a> Device<'a> {
565 mode: entity_config.mode, 575 mode: entity_config.mode,
566 device: &device_discovery, 576 device: &device_discovery,
567 }; 577 };
568 crate::log::debug!("discovery for entity '{}': {:?}", entity_config.id, discovery); 578 crate::log::debug!(
579 "discovery for entity '{}': {:?}",
580 entity_config.id,
581 discovery
582 );
569 583
570 self.discovery_buffer 584 self.discovery_buffer
571 .resize(self.discovery_buffer.capacity(), 0) 585 .resize(self.discovery_buffer.capacity(), 0)
@@ -679,7 +693,10 @@ impl<'a> Device<'a> {
679 embassy_futures::select::Either::First(packet) => match packet { 693 embassy_futures::select::Either::First(packet) => match packet {
680 Ok(embedded_mqtt::Packet::Publish(publish)) => publish, 694 Ok(embedded_mqtt::Packet::Publish(publish)) => publish,
681 Err(err) => { 695 Err(err) => {
682 crate::log::error!("mqtt receive failed with: {:?}", crate::log::Debug2Format(&err)); 696 crate::log::error!(
697 "mqtt receive failed with: {:?}",
698 crate::log::Debug2Format(&err)
699 );
683 return Err(Error::new("mqtt receive failed")); 700 return Err(Error::new("mqtt receive failed"));
684 } 701 }
685 _ => continue, 702 _ => continue,
@@ -797,9 +814,17 @@ impl<'a> Device<'a> {
797 continue; 814 continue;
798 } 815 }
799 }; 816 };
817 let timestamp = embassy_time::Instant::now();
818 if number_storage.publish_on_command {
819 data.publish = true;
820 number_storage.state = Some(NumberState {
821 value: command,
822 timestamp,
823 });
824 }
800 number_storage.command = Some(NumberCommand { 825 number_storage.command = Some(NumberCommand {
801 value: command, 826 value: command,
802 timestamp: embassy_time::Instant::now(), 827 timestamp,
803 }); 828 });
804 } 829 }
805 _ => continue 'outer_loop, 830 _ => continue 'outer_loop,