aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/entity_sensor.rs7
-rw-r--r--src/lib.rs38
-rw-r--r--src/log.rs11
-rw-r--r--src/mqtt/mod.rs50
-rw-r--r--src/mqtt/qos.rs3
-rw-r--r--src/mqtt/varint.rs4
6 files changed, 60 insertions, 53 deletions
diff --git a/src/entity_sensor.rs b/src/entity_sensor.rs
index 1168c37..e221141 100644
--- a/src/entity_sensor.rs
+++ b/src/entity_sensor.rs
@@ -19,7 +19,9 @@ impl StateClass {
19} 19}
20 20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)] 21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22#[derive(Default)]
22pub enum SensorClass { 23pub enum SensorClass {
24 #[default]
23 Generic, 25 Generic,
24 ApparentPower, 26 ApparentPower,
25 Aqi, 27 Aqi,
@@ -75,11 +77,6 @@ pub enum SensorClass {
75 Other(&'static str), 77 Other(&'static str),
76} 78}
77 79
78impl Default for SensorClass {
79 fn default() -> Self {
80 SensorClass::Generic
81 }
82}
83 80
84impl SensorClass { 81impl SensorClass {
85 pub fn as_str(&self) -> Option<&'static str> { 82 pub fn as_str(&self) -> Option<&'static str> {
diff --git a/src/lib.rs b/src/lib.rs
index 714e186..50c6308 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -485,8 +485,10 @@ pub fn create_sensor<'a>(
485 id: &'static str, 485 id: &'static str,
486 config: SensorConfig, 486 config: SensorConfig,
487) -> Sensor<'a> { 487) -> Sensor<'a> {
488 let mut entity_config = EntityConfig::default(); 488 let mut entity_config = EntityConfig {
489 entity_config.id = id; 489 id,
490 ..Default::default()
491 };
490 config.populate(&mut entity_config); 492 config.populate(&mut entity_config);
491 493
492 let entity = create_entity( 494 let entity = create_entity(
@@ -502,8 +504,10 @@ pub fn create_button<'a>(
502 id: &'static str, 504 id: &'static str,
503 config: ButtonConfig, 505 config: ButtonConfig,
504) -> Button<'a> { 506) -> Button<'a> {
505 let mut entity_config = EntityConfig::default(); 507 let mut entity_config = EntityConfig {
506 entity_config.id = id; 508 id,
509 ..Default::default()
510 };
507 config.populate(&mut entity_config); 511 config.populate(&mut entity_config);
508 512
509 let entity = create_entity( 513 let entity = create_entity(
@@ -519,8 +523,10 @@ pub fn create_number<'a>(
519 id: &'static str, 523 id: &'static str,
520 config: NumberConfig, 524 config: NumberConfig,
521) -> Number<'a> { 525) -> Number<'a> {
522 let mut entity_config = EntityConfig::default(); 526 let mut entity_config = EntityConfig {
523 entity_config.id = id; 527 id,
528 ..Default::default()
529 };
524 config.populate(&mut entity_config); 530 config.populate(&mut entity_config);
525 531
526 let entity = create_entity( 532 let entity = create_entity(
@@ -539,8 +545,10 @@ pub fn create_switch<'a>(
539 id: &'static str, 545 id: &'static str,
540 config: SwitchConfig, 546 config: SwitchConfig,
541) -> Switch<'a> { 547) -> Switch<'a> {
542 let mut entity_config = EntityConfig::default(); 548 let mut entity_config = EntityConfig {
543 entity_config.id = id; 549 id,
550 ..Default::default()
551 };
544 config.populate(&mut entity_config); 552 config.populate(&mut entity_config);
545 553
546 let entity = create_entity( 554 let entity = create_entity(
@@ -559,8 +567,10 @@ pub fn create_binary_sensor<'a>(
559 id: &'static str, 567 id: &'static str,
560 config: BinarySensorConfig, 568 config: BinarySensorConfig,
561) -> BinarySensor<'a> { 569) -> BinarySensor<'a> {
562 let mut entity_config = EntityConfig::default(); 570 let mut entity_config = EntityConfig {
563 entity_config.id = id; 571 id,
572 ..Default::default()
573 };
564 config.populate(&mut entity_config); 574 config.populate(&mut entity_config);
565 575
566 let entity = create_entity( 576 let entity = create_entity(
@@ -693,7 +703,7 @@ pub async fn run<T: Transport>(device: &mut Device<'_>, transport: &mut T) -> Re
693 .discovery_buffer 703 .discovery_buffer
694 .resize(device.discovery_buffer.capacity(), 0) 704 .resize(device.discovery_buffer.capacity(), 0)
695 .unwrap(); 705 .unwrap();
696 let n = serde_json_core::to_slice(&discovery, &mut device.discovery_buffer) 706 let n = serde_json_core::to_slice(&discovery, device.discovery_buffer)
697 .expect("discovery buffer too small"); 707 .expect("discovery buffer too small");
698 device.discovery_buffer.truncate(n); 708 device.discovery_buffer.truncate(n);
699 } 709 }
@@ -702,7 +712,7 @@ pub async fn run<T: Transport>(device: &mut Device<'_>, transport: &mut T) -> Re
702 crate::log::debug!("sending discovery to topic '{}'", discovery_topic); 712 crate::log::debug!("sending discovery to topic '{}'", discovery_topic);
703 match embassy_time::with_timeout( 713 match embassy_time::with_timeout(
704 MQTT_TIMEOUT, 714 MQTT_TIMEOUT,
705 client.publish(discovery_topic, &device.discovery_buffer), 715 client.publish(discovery_topic, device.discovery_buffer),
706 ) 716 )
707 .await 717 .await
708 { 718 {
@@ -1077,10 +1087,12 @@ pub async fn connect_and_run(
1077 } 1087 }
1078 }; 1088 };
1079 1089
1090 #[allow(unreachable_patterns)]
1080 let ipv4_addr = match addrs 1091 let ipv4_addr = match addrs
1081 .iter() 1092 .iter()
1082 .filter_map(|addr| match addr { 1093 .filter_map(|addr| match addr {
1083 embassy_net::IpAddress::Ipv4(ipv4) => Some((*ipv4).into()), 1094 embassy_net::IpAddress::Ipv4(ipv4) => Some(*ipv4),
1095 _ => None,
1084 }) 1096 })
1085 .next() 1097 .next()
1086 { 1098 {
diff --git a/src/log.rs b/src/log.rs
index d25d210..5b67001 100644
--- a/src/log.rs
+++ b/src/log.rs
@@ -32,6 +32,7 @@ pub trait Format {}
32pub use defmt::Debug2Format; 32pub use defmt::Debug2Format;
33 33
34// For tracing or no logging, Debug2Format is a passthrough 34// For tracing or no logging, Debug2Format is a passthrough
35#[allow(non_snake_case)]
35#[cfg(not(feature = "defmt"))] 36#[cfg(not(feature = "defmt"))]
36#[inline] 37#[inline]
37pub fn Debug2Format<T>(value: &T) -> &T { 38pub fn Debug2Format<T>(value: &T) -> &T {
@@ -51,7 +52,7 @@ macro_rules! trace {
51 tracing::trace!($($arg)*); 52 tracing::trace!($($arg)*);
52 53
53 #[cfg(not(any(feature = "defmt", feature = "tracing")))] 54 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
54 { let _ = (); } // no-op 55 { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving
55 }; 56 };
56} 57}
57 58
@@ -65,7 +66,7 @@ macro_rules! debug {
65 tracing::debug!($($arg)*); 66 tracing::debug!($($arg)*);
66 67
67 #[cfg(not(any(feature = "defmt", feature = "tracing")))] 68 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
68 { let _ = (); } // no-op 69 { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving
69 }; 70 };
70} 71}
71 72
@@ -79,7 +80,7 @@ macro_rules! info {
79 tracing::info!($($arg)*); 80 tracing::info!($($arg)*);
80 81
81 #[cfg(not(any(feature = "defmt", feature = "tracing")))] 82 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
82 { let _ = (); } // no-op 83 { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving
83 }; 84 };
84} 85}
85 86
@@ -93,7 +94,7 @@ macro_rules! warn {
93 tracing::warn!($($arg)*); 94 tracing::warn!($($arg)*);
94 95
95 #[cfg(not(any(feature = "defmt", feature = "tracing")))] 96 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
96 { let _ = (); } // no-op 97 { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving
97 }; 98 };
98} 99}
99 100
@@ -107,7 +108,7 @@ macro_rules! error {
107 tracing::error!($($arg)*); 108 tracing::error!($($arg)*);
108 109
109 #[cfg(not(any(feature = "defmt", feature = "tracing")))] 110 #[cfg(not(any(feature = "defmt", feature = "tracing")))]
110 { let _ = (); } // no-op 111 { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving
111 }; 112 };
112} 113}
113 114
diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs
index 30e3a33..04e63b6 100644
--- a/src/mqtt/mod.rs
+++ b/src/mqtt/mod.rs
@@ -1,3 +1,5 @@
1#![allow(unused)]
2
1mod connect_code; 3mod connect_code;
2mod field; 4mod field;
3mod packet_id; 5mod packet_id;
@@ -23,7 +25,7 @@ pub enum Error<T: Transport> {
23 Transport(T::Error), 25 Transport(T::Error),
24 TransportEOF, 26 TransportEOF,
25 InsufficientBufferSpace, 27 InsufficientBufferSpace,
26 ProtocolError(&'static str), 28 Protocol(&'static str),
27 ConnectFailed(ConnectCode), 29 ConnectFailed(ConnectCode),
28} 30}
29 31
@@ -33,7 +35,7 @@ impl<T: Transport> core::fmt::Debug for Error<T> {
33 Error::Transport(err) => f.debug_tuple("Transport").field(err).finish(), 35 Error::Transport(err) => f.debug_tuple("Transport").field(err).finish(),
34 Error::TransportEOF => f.write_str("TransportEOF"), 36 Error::TransportEOF => f.write_str("TransportEOF"),
35 Error::InsufficientBufferSpace => f.write_str("InsufficientBufferSpace"), 37 Error::InsufficientBufferSpace => f.write_str("InsufficientBufferSpace"),
36 Error::ProtocolError(msg) => f.debug_tuple("ProtocolError").field(msg).finish(), 38 Error::Protocol(msg) => f.debug_tuple("ProtocolError").field(msg).finish(),
37 Error::ConnectFailed(code) => f.debug_tuple("ConnectFailed").field(code).finish(), 39 Error::ConnectFailed(code) => f.debug_tuple("ConnectFailed").field(code).finish(),
38 } 40 }
39 } 41 }
@@ -47,7 +49,7 @@ impl<T: Transport> core::fmt::Display for Error<T> {
47 Error::InsufficientBufferSpace => { 49 Error::InsufficientBufferSpace => {
48 write!(f, "insufficient buffer space to receive packet") 50 write!(f, "insufficient buffer space to receive packet")
49 } 51 }
50 Error::ProtocolError(msg) => write!(f, "MQTT protocol error: {}", msg), 52 Error::Protocol(msg) => write!(f, "MQTT protocol error: {}", msg),
51 Error::ConnectFailed(code) => write!(f, "connection failed: {}", code), 53 Error::ConnectFailed(code) => write!(f, "connection failed: {}", code),
52 } 54 }
53 } 55 }
@@ -209,7 +211,7 @@ where
209 // Wait for CONNACK response 211 // Wait for CONNACK response
210 match self.receive_inner().await? { 212 match self.receive_inner().await? {
211 rx::Packet::ConnAck { 213 rx::Packet::ConnAck {
212 session_present, 214 session_present: _,
213 code, 215 code,
214 } => { 216 } => {
215 if code == ConnectCode::ConnectionAccepted { 217 if code == ConnectCode::ConnectionAccepted {
@@ -218,7 +220,7 @@ where
218 Err(Error::ConnectFailed(code)) 220 Err(Error::ConnectFailed(code))
219 } 221 }
220 } 222 }
221 _ => Err(Error::ProtocolError( 223 _ => Err(Error::Protocol(
222 "expected CONNACK packet after CONNECT", 224 "expected CONNACK packet after CONNECT",
223 )), 225 )),
224 } 226 }
@@ -351,12 +353,12 @@ where
351 return Err(Error::InsufficientBufferSpace); 353 return Err(Error::InsufficientBufferSpace);
352 } 354 }
353 } 355 }
354 rx::Error::InvalidPacket(msg) => return Err(Error::ProtocolError(msg)), 356 rx::Error::InvalidPacket(msg) => return Err(Error::Protocol(msg)),
355 rx::Error::UnsupportedPacket { packet_type, .. } => { 357 rx::Error::UnsupportedPacket { packet_type: _, .. } => {
356 return Err(Error::ProtocolError("unsupported packet type")); 358 return Err(Error::Protocol("unsupported packet type"));
357 } 359 }
358 rx::Error::UnknownPacket { packet_type, .. } => { 360 rx::Error::UnknownPacket { packet_type: _, .. } => {
359 return Err(Error::ProtocolError("unknown packet type")); 361 return Err(Error::Protocol("unknown packet type"));
360 } 362 }
361 }, 363 },
362 } 364 }
@@ -367,9 +369,7 @@ where
367 369
368 pub async fn receive<'s>(&'s mut self) -> Result<Packet<'s>, Error<T>> { 370 pub async fn receive<'s>(&'s mut self) -> Result<Packet<'s>, Error<T>> {
369 match self.receive_inner().await? { 371 match self.receive_inner().await? {
370 rx::Packet::ConnAck { .. } => { 372 rx::Packet::ConnAck { .. } => Err(Error::Protocol("unexpected CONNACK packet")),
371 return Err(Error::ProtocolError("unexpected CONNACK packet"));
372 }
373 rx::Packet::Publish { 373 rx::Packet::Publish {
374 topic, 374 topic,
375 packet_id, 375 packet_id,
@@ -377,23 +377,19 @@ where
377 retain, 377 retain,
378 dup: _dup, 378 dup: _dup,
379 data_len, 379 data_len,
380 } => { 380 } => Ok(Packet::Publish(Publish {
381 return Ok(Packet::Publish(Publish { 381 topic,
382 topic, 382 packet_id,
383 packet_id, 383 qos,
384 qos, 384 retain,
385 retain, 385 data_len,
386 data_len, 386 })),
387 })); 387 rx::Packet::PubAck { packet_id } => Ok(Packet::PublishAck(PublishAck { packet_id })),
388 }
389 rx::Packet::PubAck { packet_id } => {
390 return Ok(Packet::PublishAck(PublishAck { packet_id }));
391 }
392 rx::Packet::SubscribeAck { packet_id, success } => { 388 rx::Packet::SubscribeAck { packet_id, success } => {
393 return Ok(Packet::SubscribeAck(SubscribeAck { packet_id, success })); 389 Ok(Packet::SubscribeAck(SubscribeAck { packet_id, success }))
394 } 390 }
395 rx::Packet::UnsubscribeAck { packet_id } => { 391 rx::Packet::UnsubscribeAck { packet_id } => {
396 return Ok(Packet::UnsubscribeAck(UnsubscribeAck { packet_id })); 392 Ok(Packet::UnsubscribeAck(UnsubscribeAck { packet_id }))
397 } 393 }
398 } 394 }
399 } 395 }
diff --git a/src/mqtt/qos.rs b/src/mqtt/qos.rs
index 0d464b4..34fa7c1 100644
--- a/src/mqtt/qos.rs
+++ b/src/mqtt/qos.rs
@@ -9,6 +9,7 @@ impl core::fmt::Display for InvalidQos {
9 9
10impl core::error::Error for InvalidQos {} 10impl core::error::Error for InvalidQos {}
11 11
12#[allow(clippy::enum_variant_names)]
12#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] 13#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
13pub enum Qos { 14pub enum Qos {
14 #[default] 15 #[default]
@@ -28,7 +29,7 @@ impl core::fmt::Display for Qos {
28} 29}
29 30
30impl Qos { 31impl Qos {
31 pub fn to_u8(&self) -> u8 { 32 pub fn to_u8(self) -> u8 {
32 match self { 33 match self {
33 Qos::AtMostOnce => 0, 34 Qos::AtMostOnce => 0,
34 Qos::AtLeastOnce => 1, 35 Qos::AtLeastOnce => 1,
diff --git a/src/mqtt/varint.rs b/src/mqtt/varint.rs
index 63bdd06..d5416ca 100644
--- a/src/mqtt/varint.rs
+++ b/src/mqtt/varint.rs
@@ -41,8 +41,8 @@ pub fn encode(mut v: u32) -> ([u8; 4], usize) {
41pub fn decode(buf: &[u8]) -> Result<(u32, usize), Error> { 41pub fn decode(buf: &[u8]) -> Result<(u32, usize), Error> {
42 let mut value = 0u32; 42 let mut value = 0u32;
43 43
44 let v = buf.get(0).ok_or(Error::NeedMoreData)?; 44 let v = buf.first().ok_or(Error::NeedMoreData)?;
45 value |= ((v & 0x7F) as u32) << 0; 45 value |= (v & 0x7F) as u32;
46 if v & 0x80 == 0 { 46 if v & 0x80 == 0 {
47 return Ok((value, 1)); 47 return Ok((value, 1));
48 } 48 }