Start work on subscriptions

This commit is contained in:
Austen Adler 2024-04-13 10:43:33 -04:00
parent 9032aba60a
commit b49326f5dc

View File

@ -10,6 +10,8 @@ use std::{
sync::{mpsc::Sender, Mutex},
};
const HOMEASSISTANT_TOPIC: &str = "homeassistant/status";
pub fn start(strip_tx: Sender<strip::Message>, config: MqttConfig) -> ProgramResult<()> {
info!("Starting mqtt with config {config:?}");
@ -31,10 +33,21 @@ fn x(config: &MqttConfig) {
}
info!("Starting mqtt client");
let (mut client, mut connection) = Client::new(mqttoptions, 10);
let (client, mut connection) = Client::new(mqttoptions, 10);
info!("Subscribing to homeassistant");
info!(
"discovery_message: {:#?}",
gen_discovery_message(&config, "my power")
);
// client.publish(, QoS::AtLeastOnce, false, payload)
client
.subscribe("homeassistant/status", QoS::AtMostOnce)
.subscribe(HOMEASSISTANT_TOPIC, QoS::AtMostOnce)
.unwrap();
client
.subscribe(format!("{}/+/{}/+/set"), QoS::AtMostOnce)
.unwrap();
// thread::spawn(move || {
// for i in 0..10 {
@ -53,8 +66,17 @@ fn x(config: &MqttConfig) {
}
use homeassistant_mqtt_discovery::{integrations::device_trigger, Common, Device};
fn gen_discovery_message(config: MqttConfig, button_id: &str) -> device_trigger::Discovery {
device_trigger::Discovery {
fn gen_discovery_message(
config: &MqttConfig,
button_id: &str,
) -> (String, device_trigger::Discovery) {
// "<discovery_prefix>/device_automation/[<node_id>/]<object_id>/config",
// homeassistant/device_automation/0x90fd9ffffedf1266/action_arrow_left_click/config
let topic_name = format!(
"{}/device_automation/{}/{}/config",
config.mqtt_discovery_prefix, config.mqtt_id, button_id
);
let discovery = device_trigger::Discovery {
common: Common {
device: Some(Device {
identifiers: Some(vec![format!("aw_lights_{}", config.mqtt_id)]),
@ -62,19 +84,15 @@ fn gen_discovery_message(config: MqttConfig, button_id: &str) -> device_trigger:
}),
..Common::default()
},
// "<discovery_prefix>/device_automation/[<node_id>/]<object_id>/config",
// homeassistant/device_automation/0x90fd9ffffedf1266/action_arrow_left_click/config
topic: format!(
"{}/device_automation/{}/{}/config",
config.mqtt_discovery_prefix, config.mqtt_id, button_id
),
r#type: "".to_string(),
subtype: "".to_string(),
topic: format!("aw_lights/{}/action", config.mqtt_id),
r#type: "action".to_string(),
subtype: format!("{button_id}_click"),
automation_type: "trigger".to_string(),
payload: None,
qos: None,
value_template: None,
}
};
(topic_name, discovery)
}
// unique_id: bedroom_switch