Finished basic google home implementation with some slight refactors along the way

This commit is contained in:
2022-12-16 06:54:31 +01:00
parent 995ff08784
commit e88e2fe48b
23 changed files with 551 additions and 208 deletions

View File

@@ -2,7 +2,8 @@ use std::{time::Duration, rc::Rc, cell::RefCell, process::exit};
use dotenv::dotenv;
use automation::{devices::{Devices, IkeaOutlet, AsStateOnOff}, zigbee::Zigbee, mqtt::Notifier};
use automation::{devices::{Devices, IkeaOutlet, TestOutlet}, zigbee::Zigbee, mqtt::Notifier};
use google_home::GoogleHome;
use rumqttc::{MqttOptions, Transport, Client};
fn get_required_env(name: &str) -> String {
@@ -30,13 +31,60 @@ fn main() {
let devices = Rc::new(RefCell::new(Devices::new()));
// Create a new device and add it to the holder
devices.borrow_mut().add_device(IkeaOutlet::new(Zigbee::new("kitchen/kettle", "zigbee2mqtt/kitchen/kettle"), client.clone()));
devices.borrow_mut().add_device(IkeaOutlet::new("Kettle".into(), Zigbee::new("kitchen/kettle", "zigbee2mqtt/kitchen/kettle"), client.clone()));
devices.borrow_mut().add_device(TestOutlet::new());
{
for (_, d) in devices.borrow_mut().as_on_offs().iter_mut() {
d.set_on(true).unwrap();
}
}
let gc = GoogleHome::new("Dreaded_X");
let json = r#"{
"requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
"inputs": [
{
"intent": "action.devices.EXECUTE",
"payload": {
"commands": [
{
"devices": [
{
"id": "kitchen/kettle"
},
{
"id": "test_device"
}
],
"execution": [
{
"command": "action.devices.commands.OnOff",
"params": {
"on": false
}
}
]
}
]
}
}
]
}"#;
let request = serde_json::from_str(json).unwrap();
{
let mut binding = devices.borrow_mut();
let mut ghd = binding.as_fullfillments();
let response = gc.handle_request(request, &mut ghd).unwrap();
println!("{response:?}");
}
let mut notifier = Notifier::new();
// Update the state of the kettle
AsStateOnOff::cast_mut(devices.borrow_mut().get_device("kitchen/kettle").unwrap()).unwrap().set_state(false);
notifier.add_listener(Rc::downgrade(&devices));
notifier.start(connection);