Fix tests

This commit is contained in:
Austen Adler 2024-04-13 11:14:47 -04:00
parent e271fb8277
commit ea1b9017f1
4 changed files with 228 additions and 10 deletions

View File

@ -12,7 +12,8 @@ members = [
"webui",
"mqtt",
"common",
"lunatic-webui", "homeassistant-mqtt-discovery",
# "lunatic-webui",
"homeassistant-mqtt-discovery",
]
[dependencies]

View File

@ -1400,6 +1400,9 @@ pub mod light {
#[serde(flatten)]
pub common: Common,
/// The MQTT topic to publish commands to change the switch state.
pub command_topic: String,
/// The MQTT topic to publish commands to change the lights brightness.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub brightness_command_topic: Option<String>,
@ -1444,9 +1447,6 @@ pub mod light {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub color_temp_value_template: Option<Template>,
/// The MQTT topic to publish commands to change the switch state.
pub command_topic: String,
/// The MQTT topic to publish commands to change the light's effect state.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub effect_command_topic: Option<String>,
@ -1623,9 +1623,76 @@ pub mod light {
pub xy_value_template: Option<Template>,
}
impl DefaultDiscovery {
pub fn new(command_topic: impl AsRef<str>) -> Self {
Self {
common: Common::default(),
command_topic: command_topic.as_ref().to_string(),
schema: None,
brightness_command_topic: None,
brightness_command_template: None,
brightness_scale: None,
brightness_state_topic: None,
brightness_value_template: None,
color_mode_state_topic: None,
color_mode_value_template: None,
color_temp_command_template: None,
color_temp_command_topic: None,
color_temp_state_topic: None,
color_temp_value_template: None,
effect_command_topic: None,
effect_command_template: None,
effect_list: None,
effect_state_topic: None,
effect_value_template: None,
hs_command_template: None,
hs_command_topic: None,
hs_state_topic: None,
hs_value_template: None,
icon: None,
json_attributes_template: None,
json_attributes_topic: None,
max_mireds: None,
min_mireds: None,
name: None,
on_command_type: None,
optimistic: None,
payload_available: None,
payload_not_available: None,
payload_off: None,
payload_on: None,
retain: None,
rgb_command_template: None,
rgb_command_topic: None,
rgb_state_topic: None,
rgb_value_template: None,
rgbw_command_template: None,
rgbw_command_topic: None,
rgbw_state_topic: None,
rgbw_value_template: None,
rgbww_command_template: None,
rgbww_command_topic: None,
rgbww_state_topic: None,
rgbww_value_template: None,
state_topic: None,
state_value_template: None,
white_command_topic: None,
white_scale: None,
xy_command_template: None,
xy_command_topic: None,
xy_state_topic: None,
xy_value_template: None,
}
}
}
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct JsonDiscovery {
// Fields common to all discoverable MQTT devices
#[serde(flatten)]
pub common: Common,
/// The MQTT topic to publish commands to change the switch state.
pub command_topic: String,
@ -1724,6 +1791,7 @@ pub mod light {
impl JsonDiscovery {
pub fn new(command_topic: impl AsRef<str>) -> Self {
Self {
common: Common::default(),
command_topic: command_topic.as_ref().to_string(),
schema: JsonSchema::default(),
brightness: None,

View File

@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
// pub mod light;
// TODO: Templates
pub type Template = ();
pub type Template = String;
// TODO: fill with https://developers.home-assistant.io/docs/core/entity/#generic-properties
#[derive(Debug, PartialEq, Eq, Clone, Default)]

View File

@ -1,4 +1,4 @@
use crate::integrations::binary_sensor;
use crate::integrations::{binary_sensor, light};
use super::*;
use serde_json::from_str;
@ -21,6 +21,159 @@ fn device() {
assert_eq!(expected, serde_json::from_str::<Device>(test_data).unwrap(),);
}
#[test]
fn light_default() {
let test_data = r#"{
"name": "Office Light RGB",
"state_topic": "office/rgb1/light/status",
"command_topic": "office/rgb1/light/switch",
"brightness_state_topic": "office/rgb1/brightness/status",
"brightness_command_topic": "office/rgb1/brightness/set",
"rgb_state_topic": "office/rgb1/rgb/status",
"rgb_command_topic": "office/rgb1/rgb/set",
"state_value_template": "{{ value_json.state }}",
"brightness_value_template": "{{ value_json.brightness }}",
"rgb_value_template": "{{ value_json.rgb | join(',') }}",
"qos": 0,
"payload_on": "ON",
"payload_off": "OFF",
"optimistic": false
}"#;
let expected = light::DefaultDiscovery {
common: Common {
unique_id: None,
object_id: None,
entity_category: None,
qos: Some(0),
enabled_by_default: None,
encoding: None,
availability_template: None,
availability_mode: None,
availability: None,
device: None,
},
command_topic: "office/rgb1/light/switch".into(),
brightness_command_topic: Some("office/rgb1/brightness/set".into()),
brightness_command_template: None,
brightness_scale: None,
brightness_state_topic: Some("office/rgb1/brightness/status".into()),
brightness_value_template: Some("{{ value_json.brightness }}".into()),
color_mode_state_topic: None,
color_mode_value_template: None,
color_temp_command_template: None,
color_temp_command_topic: None,
color_temp_state_topic: None,
color_temp_value_template: None,
effect_command_topic: None,
effect_command_template: None,
effect_list: None,
effect_state_topic: None,
effect_value_template: None,
hs_command_template: None,
hs_command_topic: None,
hs_state_topic: None,
hs_value_template: None,
icon: None,
json_attributes_template: None,
json_attributes_topic: None,
max_mireds: None,
min_mireds: None,
name: Some("Office Light RGB".into()),
on_command_type: None,
optimistic: Some(false),
payload_available: None,
payload_not_available: None,
payload_off: Some("OFF".into()),
payload_on: Some("ON".into()),
retain: None,
rgb_command_template: None,
rgb_command_topic: Some("office/rgb1/rgb/set".into()),
rgb_state_topic: Some("office/rgb1/rgb/status".into()),
rgb_value_template: Some("{{ value_json.rgb | join(',') }}".into()),
rgbw_command_template: None,
rgbw_command_topic: None,
rgbw_state_topic: None,
rgbw_value_template: None,
rgbww_command_template: None,
rgbww_command_topic: None,
rgbww_state_topic: None,
rgbww_value_template: None,
schema: None,
state_topic: Some("office/rgb1/light/status".into()),
state_value_template: Some("{{ value_json.state }}".into()),
white_command_topic: None,
white_scale: None,
xy_command_template: None,
xy_command_topic: None,
xy_state_topic: None,
xy_value_template: None,
};
// let expected = light::DefaultDiscovery {
// common: Common::default(),
// command_topic: "office/rgb1/light/switch".into(),
// brightness_command_topic: None,
// brightness_command_template: None,
// brightness_scale: None,
// brightness_state_topic: None,
// brightness_value_template: None,
// color_mode_state_topic: None,
// color_mode_value_template: None,
// color_temp_command_template: None,
// color_temp_command_topic: None,
// color_temp_state_topic: None,
// color_temp_value_template: None,
// effect_command_topic: None,
// effect_command_template: None,
// effect_list: None,
// effect_state_topic: None,
// effect_value_template: None,
// hs_command_template: None,
// hs_command_topic: None,
// hs_state_topic: None,
// hs_value_template: None,
// icon: None,
// json_attributes_template: None,
// json_attributes_topic: None,
// max_mireds: None,
// min_mireds: None,
// name: None,
// on_command_type: None,
// optimistic: None,
// payload_available: None,
// payload_not_available: None,
// payload_off: None,
// payload_on: None,
// retain: None,
// rgb_command_template: None,
// rgb_command_topic: None,
// rgb_state_topic: None,
// rgb_value_template: None,
// rgbw_command_template: None,
// rgbw_command_topic: None,
// rgbw_state_topic: None,
// rgbw_value_template: None,
// rgbww_command_template: None,
// rgbww_command_topic: None,
// rgbww_state_topic: None,
// rgbww_value_template: None,
// schema: None,
// state_topic: None,
// state_value_template: None,
// white_command_topic: None,
// white_scale: None,
// xy_command_template: None,
// xy_command_topic: None,
// xy_state_topic: None,
// xy_value_template: None,
// };
assert_eq!(
expected,
serde_json::from_str::<light::DefaultDiscovery>(test_data).unwrap(),
);
}
#[test]
fn binary_sensor() {
let test_data = r#"{
@ -47,9 +200,6 @@ fn binary_sensor() {
..Common::default()
},
device_class: Some(binary_sensor::DeviceClass::Motion),
enabled_by_default: None,
encoding: None,
entity_category: None,
expire_after: None,
force_update: None,
icon: None,
@ -61,7 +211,6 @@ fn binary_sensor() {
payload_not_available: None,
payload_off: None,
payload_on: None,
qos: None,
state_topic: "homeassistant/binary_sensor/garden/state".to_string(),
value_template: None,
};