diff --git a/homeassistant-mqtt-discovery/src/lib.rs b/homeassistant-mqtt-discovery/src/lib.rs index 75f860c..a177630 100644 --- a/homeassistant-mqtt-discovery/src/lib.rs +++ b/homeassistant-mqtt-discovery/src/lib.rs @@ -47,6 +47,7 @@ pub struct Common { pub availability_mode: Option, /// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with `availability_topic`. + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] pub availability: Option, #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] diff --git a/homeassistant-mqtt-discovery/src/tests.rs b/homeassistant-mqtt-discovery/src/tests.rs index c367fb5..fb7dce3 100644 --- a/homeassistant-mqtt-discovery/src/tests.rs +++ b/homeassistant-mqtt-discovery/src/tests.rs @@ -4,7 +4,7 @@ use crate::integrations::{ }; use super::*; -use serde_json::from_str; +use serde_json::{from_str, Value}; #[test] fn device() { @@ -22,6 +22,10 @@ fn device() { }; assert_eq!(expected, serde_json::from_str::(test_data).unwrap(),); + assert_eq!( + serde_json::from_str::(&serde_json::to_string(&expected).unwrap()).unwrap(), + serde_json::from_str::(test_data).unwrap() + ); } #[test] @@ -66,18 +70,15 @@ fn light_default() { expected, serde_json::from_str::(test_data).unwrap(), ); + assert_eq!( + serde_json::from_str::(&serde_json::to_string(&expected).unwrap()).unwrap(), + serde_json::from_str::(test_data).unwrap() + ); } #[test] fn light_json() { - let test_data = r#"{ - "schema": "json", - "name": "mqtt_json_light_1", - "state_topic": "home/rgb1", - "command_topic": "home/rgb1/set", - "brightness": true, - "supported_color_modes": ["rgb"] - }"#; + let test_data = r#"{"schema":"json","name":"mqtt_json_light_1","state_topic":"home/rgb1","command_topic":"home/rgb1/set","brightness":true,"supported_color_modes":["rgb"]}"#; let expected = light::JsonDiscovery { common: Common { ..Common::default() @@ -93,12 +94,16 @@ fn light_json() { expected, serde_json::from_str::(test_data).unwrap(), ); + + assert_eq!( + serde_json::from_str::(&serde_json::to_string(&expected).unwrap()).unwrap(), + serde_json::from_str::(test_data).unwrap() + ); } #[test] fn binary_sensor() { let test_data = r#"{ - "name": null, "device_class": "motion", "state_topic": "homeassistant/binary_sensor/garden/state", "unique_id": "motion01ad", @@ -128,17 +133,20 @@ fn binary_sensor() { expected, serde_json::from_str::(test_data).unwrap(), ); + assert_eq!( + serde_json::from_str::(&serde_json::to_string(&expected).unwrap()).unwrap(), + serde_json::from_str::(test_data).unwrap() + ); } #[test] fn test_availability() { - assert_eq!( - from_str::( - r#"{ + let test_data = r#"{ "availability_topic": "asdf" - }"# - ) - .unwrap(), + }"#; + + assert_eq!( + from_str::(test_data).unwrap(), Availability::AvailabilityTopic(String::from("asdf")) );