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