-
Notifications
You must be signed in to change notification settings - Fork 1
/
dirigera.rs
148 lines (116 loc) · 4.25 KB
/
dirigera.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![allow(dead_code)]
use rand::Rng;
#[tokio::main]
async fn main() {
let mut hub = dirigera::hub::Hub::default();
trigger_scene(&mut hub).await.unwrap();
}
async fn get_devices(hub: &mut dirigera::hub::Hub) -> anyhow::Result<()> {
for device in hub.devices().await? {
let inner = device.inner();
println!(
"{:<20} {:<40} {:<20} {}",
inner.attributes.custom_name,
inner.id,
inner.device_type,
inner
.room
.as_ref()
.map(|room| room.name.clone())
.unwrap_or("Unknown".to_string()),
);
}
Ok(())
}
async fn get_device(hub: &mut dirigera::hub::Hub) -> anyhow::Result<()> {
println!(
"{:#?}",
hub.device("3b1a04db-9abe-4811-b60a-797970f51e8a_1").await?
);
Ok(())
}
async fn get_scenes(hub: &mut dirigera::hub::Hub) -> anyhow::Result<()> {
for scene in hub.scenes().await? {
println!("{:#?}\n", scene);
}
Ok(())
}
async fn toggle_light(hub: &mut dirigera::hub::Hub) -> anyhow::Result<()> {
let mut light = hub.device("3b1a04db-9abe-4811-b60a-797970f51e8a_1").await?;
loop {
println!(
"Light is on = {}, will toggle",
light.inner().attributes.is_on
);
hub.toggle_on_off(&mut light).await?;
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
}
}
async fn light_level(hub: &mut dirigera::hub::Hub) -> anyhow::Result<()> {
let mut light = hub.device("3b1a04db-9abe-4811-b60a-797970f51e8a_1").await?;
let mut level = 0;
loop {
level = if level <= 90 { level + 10 } else { 0 };
println!(
"Light has level = {}",
light.inner().attributes.light_level.unwrap_or(0),
);
hub.set_light_level(&mut light, level).await?;
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}
}
async fn temperature(hub: &mut dirigera::hub::Hub) -> anyhow::Result<()> {
let mut light = hub.device("86a0fac2-d213-42bb-b1c1-00533ba468cd_1").await?;
let mut temp = 2300;
loop {
temp = if temp <= 3900 { temp + 100 } else { 2300 };
println!(
"Light has temp = {}",
light.inner().attributes.color_temperature.unwrap_or(0),
);
hub.set_temperature(&mut light, temp).await?;
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}
}
async fn randomize_hue(hub: &mut dirigera::hub::Hub) -> anyhow::Result<()> {
let mut light = hub.device("3b1a04db-9abe-4811-b60a-797970f51e8a_1").await?;
if !light.inner().attributes.is_on {
hub.toggle_on_off(&mut light).await?;
}
loop {
println!(
"Light has hue = {}, saturation = {}, will toggle",
light.inner().attributes.color_hue.unwrap_or(0.0),
light.inner().attributes.color_saturation.unwrap_or(0.0),
);
let mut rng = rand::thread_rng();
let hue = rng.gen_range(0.0..360.0);
let saturation = rng.gen_range(0.0..1.0);
hub.set_hue_saturation(&mut light, hue, saturation).await?;
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
}
async fn startup(hub: &mut dirigera::hub::Hub) -> anyhow::Result<()> {
let mut light = hub.device("3b1a04db-9abe-4811-b60a-797970f51e8a_1").await?;
loop {
println!(
"Light startup is = {:?}",
light.inner().attributes.startup_on_off,
);
let new_startup = match light.inner().attributes.startup_on_off {
Some(dirigera::device::Startup::StartPrevious) => dirigera::device::Startup::StartOff,
_ => dirigera::device::Startup::StartPrevious,
};
hub.set_startup_behaviour(&mut light, new_startup).await?;
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
}
}
async fn trigger_scene(hub: &mut dirigera::hub::Hub) -> anyhow::Result<()> {
let scene = hub.scene("744173bf-f7d6-4f27-9dee-d7a2345ffe00").await?;
hub.trigger_scene(&scene).await?;
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
hub.undo_scene(&scene).await?;
let scene = hub.scene("744173bf-f7d6-4f27-9dee-d7a2345ffe00").await?;
println!("{:#?}", scene);
Ok(())
}