diff --git a/Cargo.toml b/Cargo.toml index df173c2..8e5d92d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,9 @@ members = [ "driver/kasa", "emitter-and-signal", "entrypoint", + "home-assistant", "protocol", + "python-utils", ] resolver = "2" @@ -12,7 +14,7 @@ resolver = "2" backoff = "0.4.0" chrono = "0.4.40" chrono-tz = "0.10.1" -deranged = "0.4.1" +deranged = "0.4" derive_more = "2.0.1" snafu = "0.8.5" tokio = "1.32.0" diff --git a/entrypoint/Cargo.toml b/entrypoint/Cargo.toml index feb99cd..ddb17e3 100644 --- a/entrypoint/Cargo.toml +++ b/entrypoint/Cargo.toml @@ -19,32 +19,19 @@ axum = { version = "0.8.1", default-features = false, features = [ chrono = { workspace = true } chrono-tz = { workspace = true } deranged = { workspace = true, features = ["serde"] } -derive_more = { workspace = true, features = [ - "display", - "from", - "from_str", - "into", - "try_from", - "try_into", -] } driver-kasa = { path = "../driver/kasa" } emitter-and-signal = { path = "../emitter-and-signal" } +home-assistant = { path = "../home-assistant" } im = { version = "15.1.0", features = ["rayon"] } -once_cell = "1.21.3" protocol = { path = "../protocol" } pyo3 = { workspace = true, features = [ "auto-initialize", "chrono", "extension-module", ] } -pyo3-async-runtimes = { workspace = true, features = [ - "attributes", - "tokio-runtime", -] } +pyo3-async-runtimes = { workspace = true, features = ["tokio-runtime"] } shadow-rs = { version = "1.0.1", default-features = false } -smol_str = "0.3.2" snafu = { workspace = true } -strum = { version = "0.27.1", features = ["derive"] } tokio = { workspace = true, features = [ "macros", "rt", @@ -55,7 +42,6 @@ tokio = { workspace = true, features = [ tracing = { workspace = true } tracing-appender = "0.2.3" tracing-subscriber = "0.3.17" -ulid = "1.2.0" uom = "0.36.0" [build-dependencies] diff --git a/entrypoint/src/lib.rs b/entrypoint/src/lib.rs index f1fa72b..2926859 100644 --- a/entrypoint/src/lib.rs +++ b/entrypoint/src/lib.rs @@ -18,8 +18,6 @@ use tracing_subscriber::{ }; use tracing_to_home_assistant::TracingToHomeAssistant; -mod home_assistant; -mod python_utils; mod tracing_to_home_assistant; shadow!(build_info); diff --git a/entrypoint/src/tracing_to_home_assistant.rs b/entrypoint/src/tracing_to_home_assistant.rs index f4d161f..dc2c9ac 100644 --- a/entrypoint/src/tracing_to_home_assistant.rs +++ b/entrypoint/src/tracing_to_home_assistant.rs @@ -7,7 +7,7 @@ use tracing::{ }; use tracing_subscriber::{layer::Context, Layer}; -use crate::home_assistant::logger::{HassLogger, LogData}; +use home_assistant::logger::{HassLogger, LogData}; pub struct TracingToHomeAssistant; diff --git a/home-assistant/Cargo.toml b/home-assistant/Cargo.toml new file mode 100644 index 0000000..7ef70fa --- /dev/null +++ b/home-assistant/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "home-assistant" +version = "0.1.0" +edition = "2021" + +[features] +tracing = ["dep:tracing"] + +[dependencies] +arbitrary-value = { path = "../arbitrary-value" } +chrono = { workspace = true } +chrono-tz = { workspace = true } +derive_more = { workspace = true, features = [ + "display", + "from", + "from_str", + "into", + "try_from", + "try_into", +] } +emitter-and-signal = { path = "../emitter-and-signal" } +once_cell = "1.21.3" +protocol = { path = "../protocol" } +pyo3 = { workspace = true } +pyo3-async-runtimes = { workspace = true, features = ["tokio-runtime"] } +python-utils = { path = "../python-utils" } +smol_str = "0.3.2" +snafu = { workspace = true } +strum = { version = "0.27.1", features = ["derive"] } +tokio = { workspace = true } +tracing = { optional = true, workspace = true } +ulid = "1.2.0" diff --git a/entrypoint/src/home_assistant/domain.rs b/home-assistant/src/domain.rs similarity index 100% rename from entrypoint/src/home_assistant/domain.rs rename to home-assistant/src/domain.rs diff --git a/entrypoint/src/home_assistant/entity_id.rs b/home-assistant/src/entity_id.rs similarity index 100% rename from entrypoint/src/home_assistant/entity_id.rs rename to home-assistant/src/entity_id.rs diff --git a/entrypoint/src/home_assistant/event/context/context.rs b/home-assistant/src/event/context/context.rs similarity index 100% rename from entrypoint/src/home_assistant/event/context/context.rs rename to home-assistant/src/event/context/context.rs diff --git a/entrypoint/src/home_assistant/event/context/id.rs b/home-assistant/src/event/context/id.rs similarity index 100% rename from entrypoint/src/home_assistant/event/context/id.rs rename to home-assistant/src/event/context/id.rs diff --git a/entrypoint/src/home_assistant/event/context/mod.rs b/home-assistant/src/event/context/mod.rs similarity index 100% rename from entrypoint/src/home_assistant/event/context/mod.rs rename to home-assistant/src/event/context/mod.rs diff --git a/entrypoint/src/home_assistant/event/event.rs b/home-assistant/src/event/event.rs similarity index 100% rename from entrypoint/src/home_assistant/event/event.rs rename to home-assistant/src/event/event.rs diff --git a/entrypoint/src/home_assistant/event/event_origin.rs b/home-assistant/src/event/event_origin.rs similarity index 100% rename from entrypoint/src/home_assistant/event/event_origin.rs rename to home-assistant/src/event/event_origin.rs diff --git a/entrypoint/src/home_assistant/event/mod.rs b/home-assistant/src/event/mod.rs similarity index 100% rename from entrypoint/src/home_assistant/event/mod.rs rename to home-assistant/src/event/mod.rs diff --git a/entrypoint/src/home_assistant/event/specific/mod.rs b/home-assistant/src/event/specific/mod.rs similarity index 100% rename from entrypoint/src/home_assistant/event/specific/mod.rs rename to home-assistant/src/event/specific/mod.rs diff --git a/entrypoint/src/home_assistant/event/specific/state_changed.rs b/home-assistant/src/event/specific/state_changed.rs similarity index 94% rename from entrypoint/src/home_assistant/event/specific/state_changed.rs rename to home-assistant/src/event/specific/state_changed.rs index 5643688..3978f95 100644 --- a/entrypoint/src/home_assistant/event/specific/state_changed.rs +++ b/home-assistant/src/event/specific/state_changed.rs @@ -1,7 +1,7 @@ use pyo3::exceptions::PyValueError; use pyo3::prelude::*; -use crate::home_assistant::{entity_id::EntityId, state_object::StateObject}; +use crate::{entity_id::EntityId, state_object::StateObject}; #[derive(Debug, Clone)] pub struct Type; diff --git a/entrypoint/src/home_assistant/home_assistant.rs b/home-assistant/src/home_assistant.rs similarity index 96% rename from entrypoint/src/home_assistant/home_assistant.rs rename to home-assistant/src/home_assistant.rs index 3625a6b..a108240 100644 --- a/entrypoint/src/home_assistant/home_assistant.rs +++ b/home-assistant/src/home_assistant.rs @@ -2,7 +2,7 @@ use std::convert::Infallible; use pyo3::prelude::*; -use crate::python_utils::{detach, validate_type_by_name}; +use python_utils::{detach, validate_type_by_name}; use super::{service_registry::ServiceRegistry, state_machine::StateMachine}; diff --git a/entrypoint/src/home_assistant/mod.rs b/home-assistant/src/lib.rs similarity index 100% rename from entrypoint/src/home_assistant/mod.rs rename to home-assistant/src/lib.rs diff --git a/entrypoint/src/home_assistant/light/attributes.rs b/home-assistant/src/light/attributes.rs similarity index 100% rename from entrypoint/src/home_assistant/light/attributes.rs rename to home-assistant/src/light/attributes.rs diff --git a/entrypoint/src/home_assistant/light/mod.rs b/home-assistant/src/light/mod.rs similarity index 95% rename from entrypoint/src/home_assistant/light/mod.rs rename to home-assistant/src/light/mod.rs index 675e4c1..59bb024 100644 --- a/entrypoint/src/home_assistant/light/mod.rs +++ b/home-assistant/src/light/mod.rs @@ -3,7 +3,7 @@ use pyo3::prelude::*; use snafu::{ResultExt, Snafu}; use state::LightState; -use crate::home_assistant::state::HomeAssistantState; +use crate::state::HomeAssistantState; use super::{ domain::Domain, entity_id::EntityId, home_assistant::HomeAssistant, object_id::ObjectId, diff --git a/entrypoint/src/home_assistant/light/protocol.rs b/home-assistant/src/light/protocol.rs similarity index 95% rename from entrypoint/src/home_assistant/light/protocol.rs rename to home-assistant/src/light/protocol.rs index 9673c2c..d11eca2 100644 --- a/entrypoint/src/home_assistant/light/protocol.rs +++ b/home-assistant/src/light/protocol.rs @@ -1,6 +1,6 @@ use super::service::{turn_off::TurnOff, turn_on::TurnOn}; use super::{state::LightState, GetStateObjectError, HomeAssistantLight}; -use crate::home_assistant::{ +use crate::{ event::context::context::Context, state::{ErrorState, HomeAssistantState, UnexpectedState}, }; @@ -67,6 +67,7 @@ impl Light for HomeAssistantLight { .await?; // TODO + #[cfg(feature = "tracing")] tracing::info!(?service_response); Ok(()) @@ -92,6 +93,10 @@ impl Light for HomeAssistantLight { ) .await?; + // TODO + #[cfg(feature = "tracing")] + tracing::info!(?service_response); + Ok(()) } diff --git a/entrypoint/src/home_assistant/light/service/mod.rs b/home-assistant/src/light/service/mod.rs similarity index 100% rename from entrypoint/src/home_assistant/light/service/mod.rs rename to home-assistant/src/light/service/mod.rs diff --git a/entrypoint/src/home_assistant/light/service/turn_off.rs b/home-assistant/src/light/service/turn_off.rs similarity index 97% rename from entrypoint/src/home_assistant/light/service/turn_off.rs rename to home-assistant/src/light/service/turn_off.rs index b6c4fa2..3b623f1 100644 --- a/entrypoint/src/home_assistant/light/service/turn_off.rs +++ b/home-assistant/src/light/service/turn_off.rs @@ -2,7 +2,7 @@ use std::str::FromStr; use pyo3::IntoPyObject; -use crate::home_assistant::{ +use crate::{ entity_id::EntityId, service::{service_domain::ServiceDomain, service_id::ServiceId, IntoServiceCall}, }; diff --git a/entrypoint/src/home_assistant/light/service/turn_on.rs b/home-assistant/src/light/service/turn_on.rs similarity index 92% rename from entrypoint/src/home_assistant/light/service/turn_on.rs rename to home-assistant/src/light/service/turn_on.rs index 9f14810..83d0c6d 100644 --- a/entrypoint/src/home_assistant/light/service/turn_on.rs +++ b/home-assistant/src/light/service/turn_on.rs @@ -1,8 +1,8 @@ -use std::{convert::Infallible, str::FromStr}; +use std::str::FromStr; use pyo3::IntoPyObject; -use crate::home_assistant::{ +use crate::{ entity_id::EntityId, service::{service_domain::ServiceDomain, service_id::ServiceId, IntoServiceCall}, }; diff --git a/entrypoint/src/home_assistant/light/state.rs b/home-assistant/src/light/state.rs similarity index 100% rename from entrypoint/src/home_assistant/light/state.rs rename to home-assistant/src/light/state.rs diff --git a/entrypoint/src/home_assistant/logger.rs b/home-assistant/src/logger.rs similarity index 99% rename from entrypoint/src/home_assistant/logger.rs rename to home-assistant/src/logger.rs index 474d27f..d80d39b 100644 --- a/entrypoint/src/home_assistant/logger.rs +++ b/home-assistant/src/logger.rs @@ -1,7 +1,7 @@ -use crate::python_utils::{detach, validate_type_by_name}; use arbitrary_value::{arbitrary::Arbitrary, map::Map}; use once_cell::sync::OnceCell; use pyo3::{prelude::*, types::PyTuple}; +use python_utils::{detach, validate_type_by_name}; #[derive(Debug)] pub struct HassLogger(Py); diff --git a/entrypoint/src/home_assistant/object_id.rs b/home-assistant/src/object_id.rs similarity index 100% rename from entrypoint/src/home_assistant/object_id.rs rename to home-assistant/src/object_id.rs diff --git a/entrypoint/src/home_assistant/service/mod.rs b/home-assistant/src/service/mod.rs similarity index 100% rename from entrypoint/src/home_assistant/service/mod.rs rename to home-assistant/src/service/mod.rs diff --git a/entrypoint/src/home_assistant/service/service_domain.rs b/home-assistant/src/service/service_domain.rs similarity index 100% rename from entrypoint/src/home_assistant/service/service_domain.rs rename to home-assistant/src/service/service_domain.rs diff --git a/entrypoint/src/home_assistant/service/service_id.rs b/home-assistant/src/service/service_id.rs similarity index 100% rename from entrypoint/src/home_assistant/service/service_id.rs rename to home-assistant/src/service/service_id.rs diff --git a/entrypoint/src/home_assistant/service_registry.rs b/home-assistant/src/service_registry.rs similarity index 96% rename from entrypoint/src/home_assistant/service_registry.rs rename to home-assistant/src/service_registry.rs index f916ba3..eb934a7 100644 --- a/entrypoint/src/home_assistant/service_registry.rs +++ b/home-assistant/src/service_registry.rs @@ -1,8 +1,6 @@ -use pyo3::prelude::*; - -use crate::python_utils::{detach, validate_type_by_name}; - use super::{event::context::context::Context, service::IntoServiceCall}; +use pyo3::prelude::*; +use python_utils::{detach, validate_type_by_name}; #[derive(Debug)] pub struct ServiceRegistry(Py); diff --git a/entrypoint/src/home_assistant/slug.rs b/home-assistant/src/slug.rs similarity index 100% rename from entrypoint/src/home_assistant/slug.rs rename to home-assistant/src/slug.rs diff --git a/entrypoint/src/home_assistant/state.rs b/home-assistant/src/state.rs similarity index 100% rename from entrypoint/src/home_assistant/state.rs rename to home-assistant/src/state.rs diff --git a/entrypoint/src/home_assistant/state_machine.rs b/home-assistant/src/state_machine.rs similarity index 88% rename from entrypoint/src/home_assistant/state_machine.rs rename to home-assistant/src/state_machine.rs index d2f5a18..a88c3d9 100644 --- a/entrypoint/src/home_assistant/state_machine.rs +++ b/home-assistant/src/state_machine.rs @@ -1,11 +1,7 @@ -use pyo3::prelude::*; - -use crate::{ - home_assistant::entity_id::EntityId, - python_utils::{detach, validate_type_by_name}, -}; - +use super::entity_id::EntityId; use super::state_object::StateObject; +use pyo3::prelude::*; +use python_utils::{detach, validate_type_by_name}; #[derive(Debug)] pub struct StateMachine(Py); diff --git a/entrypoint/src/home_assistant/state_object.rs b/home-assistant/src/state_object.rs similarity index 89% rename from entrypoint/src/home_assistant/state_object.rs rename to home-assistant/src/state_object.rs index 764fe20..c50eaf7 100644 --- a/entrypoint/src/home_assistant/state_object.rs +++ b/home-assistant/src/state_object.rs @@ -2,7 +2,7 @@ use super::{ event::{context::context::Context, specific::state_changed}, home_assistant::HomeAssistant, }; -use crate::home_assistant::entity_id::EntityId; +use crate::entity_id::EntityId; use chrono::{DateTime, Utc}; use emitter_and_signal::signal::Signal; use once_cell::sync::OnceCell; @@ -62,6 +62,7 @@ impl< let callback = move |args: &Bound<'_, PyTuple>, _kwargs: Option<&Bound<'_, PyDict>>| { + #[cfg(feature = "tracing")] tracing::debug!("calling the closure"); if let Ok((event,)) = args.extract::<( @@ -77,6 +78,7 @@ impl< )>() { let new_state = event.data.new_state; + #[cfg(feature = "tracing")] tracing::debug!("sending a new state"); // TODO: remove new_state_sender.try_send(new_state).unwrap(); } @@ -89,38 +91,47 @@ impl< ); event_module.call_method1("async_track_state_change_event", args)? }; + #[cfg(feature = "tracing")] tracing::debug!(?untrack, "as any"); let is_callable = untrack.is_callable(); + #[cfg(feature = "tracing")] tracing::debug!(?is_callable); // let untrack = untrack.downcast_into::()?; // tracing::debug!(?untrack, "as downcast"); let untrack = untrack.unbind(); + #[cfg(feature = "tracing")] tracing::debug!(?untrack, "as unbound"); Ok(untrack) }); if let Ok(untrack) = untrack { + #[cfg(feature = "tracing")] tracing::debug!("untrack is ok, going to wait for the next relevant event..."); loop { select! { biased; _ = publisher.all_unsubscribed() => { + #[cfg(feature = "tracing")] tracing::debug!("calling untrack"); let res = Python::with_gil(|py| untrack.call0(py)); + + #[cfg(feature = "tracing")] tracing::debug!(?res); break; } new_state = new_state_receiver.recv() => { match new_state { Some(new_state) => { + #[cfg(feature = "tracing")] tracing::debug!("publishing new state"); publisher.publish(new_state.map(Arc::new)) }, None => { + #[cfg(feature = "tracing")] tracing::debug!("channel dropped"); break }, @@ -129,6 +140,7 @@ impl< } } } else { + #[cfg(feature = "tracing")] tracing::debug!("untrack is err"); } } diff --git a/python-utils/Cargo.toml b/python-utils/Cargo.toml new file mode 100644 index 0000000..0fdf474 --- /dev/null +++ b/python-utils/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "python-utils" +version = "0.1.0" +edition = "2021" + +[dependencies] +pyo3 = { workspace = true } diff --git a/entrypoint/src/python_utils.rs b/python-utils/src/lib.rs similarity index 100% rename from entrypoint/src/python_utils.rs rename to python-utils/src/lib.rs