From 9e7e5cdcc8c80520e86b05bbd66cdc3535b84dd7 Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 8 Jul 2026 16:49:21 -0400 Subject: [PATCH] fix: make some types of mobile app notifications work --- .../notify/service/mobile_app/command/mod.rs | 6 +- .../src/notify/service/mobile_app/mod.rs | 65 ++++++++++++++++++- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/home-assistant/src/notify/service/mobile_app/command/mod.rs b/home-assistant/src/notify/service/mobile_app/command/mod.rs index fdde121..8cfe7cc 100644 --- a/home-assistant/src/notify/service/mobile_app/command/mod.rs +++ b/home-assistant/src/notify/service/mobile_app/command/mod.rs @@ -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; diff --git a/home-assistant/src/notify/service/mobile_app/mod.rs b/home-assistant/src/notify/service/mobile_app/mod.rs index 8d9fe36..236f10f 100644 --- a/home-assistant/src/notify/service/mobile_app/mod.rs +++ b/home-assistant/src/notify/service/mobile_app/mod.rs @@ -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>, @@ -225,13 +225,72 @@ pub struct NotifyMobileAppServiceDataData { visibility: Option, } -#[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 { + 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, + #[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 { + 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) + } +} \ No newline at end of file