Compare commits

...

2 Commits

3 changed files with 150 additions and 34 deletions

View File

@@ -101,41 +101,98 @@ async fn real_main(
let built_at = build_info::BUILD_TIME;
tracing::info!(built_at);
let mut bathroom_mirror_lights = [
LB130USHandle::new(
([10, 0, 3, 80], 9999).into(),
Duration::from_secs(10),
(64).try_into().unwrap(),
),
LB130USHandle::new(
([10, 0, 3, 82], 9999).into(),
Duration::from_secs(10),
(64).try_into().unwrap(),
),
];
tokio::join!(
async {
let mut bathroom_mirror_lights = [
LB130USHandle::new(
([10, 0, 3, 80], 9999).into(),
Duration::from_secs(10),
(64).try_into().unwrap(),
),
LB130USHandle::new(
([10, 0, 3, 82], 9999).into(),
Duration::from_secs(10),
(64).try_into().unwrap(),
),
];
let mut interval = interval(Duration::from_secs(15));
interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
let mut interval = interval(Duration::from_secs(15));
interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
let mut temperature = Kelvin::MIN;
let mut temperature = Kelvin::MIN;
loop {
let instant = interval.tick().await;
// temperature = temperature.wrapping_add(5173);
tracing::info!(?temperature);
loop {
let instant = interval.tick().await;
// temperature = temperature.wrapping_add(5173);
tracing::info!(?temperature);
let tasks = bathroom_mirror_lights
.iter_mut()
.map(|bathroom_mirror_light| bathroom_mirror_light.turn_to_temperature(temperature));
let mut tasks = FuturesUnordered::from_iter(tasks);
let tasks = bathroom_mirror_lights
.iter_mut()
.map(|bathroom_mirror_light| {
bathroom_mirror_light.turn_to_temperature(temperature)
});
let mut tasks = FuturesUnordered::from_iter(tasks);
while let Some(result) = tasks.next().await {
match result {
Ok(()) => {}
Err(error_turning_to_temperature) => tracing::error!(?error_turning_to_temperature),
while let Some(result) = tasks.next().await {
match result {
Ok(()) => {}
Err(error_turning_to_temperature) => {
tracing::error!(?error_turning_to_temperature)
}
}
}
}
},
async {
let jacob_phone_id = "galaxy_s21_ultra_1";
let jacob_phone_object_id = ObjectId::from_str(jacob_phone_id).unwrap();
let services = Python::attach(|py| home_assistant.services(py)).unwrap();
let mut interval = interval(Duration::from_secs(15));
interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
use home_assistant::notify::service::mobile_app::command;
// loop {
// let instant = interval.tick().await;
// let context: Option<Context<()>> = None;
// let target: Option<()> = None;
// let turn_off_result: Result<Py<PyAny>, _> = services.call_service(command::DoNotDisturb {
// object_id: jacob_phone_object_id.clone(),
// filter: command::DoNotDisturbFilter::Off,
// }, context, target, false).await;
// dbg!(turn_off_result);
// let instant = interval.tick().await;
// let context: Option<Context<()>> = None;
// let target: Option<()> = None;
// let alarms_only_result: Result<Py<PyAny>, _> = services.call_service(command::DoNotDisturb {
// object_id: jacob_phone_object_id.clone(),
// filter: command::DoNotDisturbFilter::AlarmsOnly,
// }, context, target, false).await;
// dbg!(alarms_only_result);
// let instant = interval.tick().await;
// let context: Option<Context<()>> = None;
// let target: Option<()> = None;
// let priority_only_result: Result<Py<PyAny>, _> = services.call_service(command::DoNotDisturb {
// object_id: jacob_phone_object_id.clone(),
// filter: command::DoNotDisturbFilter::PriorityOnly,
// }, context, target, false).await;
// dbg!(priority_only_result);
// let instant = interval.tick().await;
// let context: Option<Context<()>> = None;
// let target: Option<()> = None;
// let total_silence_result: Result<Py<PyAny>, _> = services.call_service(command::DoNotDisturb {
// object_id: jacob_phone_object_id.clone(),
// filter: command::DoNotDisturbFilter::TotalSilence,
// }, context, target, false).await;
// dbg!(total_silence_result);
// }
}
}
).0
}
#[pyfunction]

View File

@@ -1,5 +1,5 @@
mod do_not_disturb;
mod request_location_update;
pub mod do_not_disturb;
pub mod request_location_update;
pub use do_not_disturb::DoNotDisturb;
pub use do_not_disturb::{DoNotDisturb, Filter as DoNotDisturbFilter};
pub use request_location_update::RequestLocationUpdate;

View File

@@ -1,6 +1,6 @@
use std::{convert::Infallible, str::FromStr};
use pyo3::{types::PyString, Bound, IntoPyObject, Python};
use pyo3::{types::{PyDict, PyString, PyDictMethods}, Bound, IntoPyObject, Python, PyErr};
use python_utils::IntoPyObjectViaDisplay;
use snafu::Snafu;
use strum::EnumString;
@@ -215,7 +215,7 @@ impl<'py> IntoPyObject<'py> for MediaStream {
}
}
#[derive(Debug, Default, Clone, IntoPyObject, typed_builder::TypedBuilder)]
#[derive(Debug, Default, Clone, typed_builder::TypedBuilder)]
#[builder(field_defaults(default, setter(strip_option(fallback_suffix = "_option"))))]
pub struct NotifyMobileAppServiceDataData {
actions: Option<Vec<Action>>,
@@ -225,13 +225,72 @@ pub struct NotifyMobileAppServiceDataData {
visibility: Option<Visibility>,
}
#[derive(Debug, Clone, IntoPyObject, typed_builder::TypedBuilder)]
// TODO: derive macro that does this for me
impl<'py> IntoPyObject<'py> for NotifyMobileAppServiceDataData {
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let NotifyMobileAppServiceDataData { actions, command, media_stream, tts_text, visibility } = self;
let dict = PyDict::new(py);
if let Some(actions) = actions {
dict.set_item("actions", actions)?;
}
if let Some(command) = command {
dict.set_item("command", command)?;
}
if let Some(media_stream) = media_stream {
dict.set_item("media_stream", media_stream)?;
}
if let Some(tts_text) = tts_text {
dict.set_item("tts_text", tts_text)?;
}
if let Some(visibility) = visibility {
dict.set_item("visibility", visibility)?;
}
Ok(dict)
}
}
#[derive(Debug, Clone, typed_builder::TypedBuilder)]
#[builder(field_defaults(default, setter(strip_option(fallback_suffix = "_option"))))]
pub struct NotifyMobileAppServiceData {
#[builder(!default, setter(!strip_option))]
message: String,
title: Option<String>,
#[builder(setter(!strip_option))]
data: NotifyMobileAppServiceDataData,
}
// TODO: derive macro that does this for me
impl<'py> IntoPyObject<'py> for NotifyMobileAppServiceData {
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let NotifyMobileAppServiceData { message, title, data } = self;
let dict = PyDict::new(py);
dict.set_item("message", message)?;
if let Some(title) = title {
dict.set_item("title", title);
}
dict.set_item("data", data);
Ok(dict)
}
}