feat: static Event and Context types

This commit is contained in:
2025-03-13 16:01:52 -04:00
parent 96495b2a85
commit 95b7f38fc1
5 changed files with 21 additions and 29 deletions

View File

@@ -1,24 +1,14 @@
use pyo3::prelude::*; use pyo3::prelude::*;
use crate::home_assistant::event::event::Event;
use super::id::Id; use super::id::Id;
/// The context that triggered something. /// The context that triggered something.
#[derive(Debug, FromPyObject)] #[derive(Debug, FromPyObject)]
pub struct Context { pub struct Context<Event> {
pub id: Id, pub id: Id,
pub user_id: Option<String>, pub user_id: Option<String>,
pub parent_id: Option<String>, pub parent_id: Option<String>,
/// In order to prevent cycles, the user must extract this to an [`Event<Arbitrary>`](super::event::Event) themself (or even specify a specific type parameter!) /// In order to prevent cycles, the user must decide to pass [`Py<PyAny>`] for the `Event` type here
origin_event: Py<PyAny>, /// or for the `Context` type in [`Event`]
} pub origin_event: Event,
impl Context {
pub fn origin_event<'py, Type: FromPyObject<'py>, Data: FromPyObject<'py>>(
&self,
py: Python<'py>,
) -> PyResult<Event<Type, Data>> {
self.origin_event.extract(py)
}
} }

View File

@@ -5,20 +5,17 @@ use super::{context::context::Context, event_origin::EventOrigin};
/// Representation of an event within the bus. /// Representation of an event within the bus.
#[derive(Debug, FromPyObject)] #[derive(Debug, FromPyObject)]
pub struct Event<Type, Data> { pub struct Event<Type, Data, Context> {
pub event_type: Type, pub event_type: Type,
pub data: Data, pub data: Data,
pub origin: EventOrigin, pub origin: EventOrigin,
/// In order to prevent cycles, the user must extract this to a [`Context`](super::context::Context) themself, using the [`context`](Self::context) method /// In order to prevent cycles, the user must decide to pass [`Py<PyAny>`] for the `Context` type here
context: Py<PyAny>, /// or for the `Event` type in [`Context`]
pub context: Context,
time_fired_timestamp: f64, time_fired_timestamp: f64,
} }
impl<Type, Data> Event<Type, Data> { impl<Type, Data, Context> Event<Type, Data, Context> {
pub fn context<'py>(&self, py: Python<'py>) -> PyResult<Context> {
self.context.extract(py)
}
pub fn time_fired(&self) -> Option<DateTime<Utc>> { pub fn time_fired(&self) -> Option<DateTime<Utc>> {
const NANOS_PER_SEC: i32 = 1_000_000_000; const NANOS_PER_SEC: i32 = 1_000_000_000;

View File

@@ -24,11 +24,12 @@ impl<'py> FromPyObject<'py> for Type {
#[derive(Debug, FromPyObject)] #[derive(Debug, FromPyObject)]
#[pyo3(from_item_all)] #[pyo3(from_item_all)]
pub struct Data { pub struct Data<OldStateContextEvent, NewStateContextEvent> {
pub entity_id: EntityId, pub entity_id: EntityId,
pub old_state: Option<State>, pub old_state: Option<State<OldStateContextEvent>>,
pub new_state: Option<State>, pub new_state: Option<State<NewStateContextEvent>>,
} }
/// A state changed event is fired when on state write the state is changed. /// A state changed event is fired when on state write the state is changed.
pub type Event = super::super::event::Event<Type, Data>; pub type Event<OldStateContextEvent, NewStateContextEvent, Context> =
super::super::event::Event<Type, Data<OldStateContextEvent, NewStateContextEvent>, Context>;

View File

@@ -6,12 +6,12 @@ use crate::{arbitrary::map::Map, home_assistant::entity_id::EntityId};
use super::event::context::context::Context; use super::event::context::context::Context;
#[derive(Debug, FromPyObject)] #[derive(Debug, FromPyObject)]
pub struct State { pub struct State<ContextEvent> {
pub entity_id: EntityId, pub entity_id: EntityId,
pub state: String, pub state: String,
pub attributes: Map, pub attributes: Map,
pub last_changed: Option<DateTime<Utc>>, pub last_changed: Option<DateTime<Utc>>,
pub last_reported: Option<DateTime<Utc>>, pub last_reported: Option<DateTime<Utc>>,
pub last_updated: Option<DateTime<Utc>>, pub last_updated: Option<DateTime<Utc>>,
pub context: Context, pub context: Context<ContextEvent>,
} }

View File

@@ -21,7 +21,11 @@ impl<'py> FromPyObject<'py> for StateMachine {
} }
impl StateMachine { impl StateMachine {
pub fn get(&self, py: &Python, entity_id: EntityId) -> Result<Option<State>, PyErr> { pub fn get<ContextEvent: for<'py> FromPyObject<'py>>(
&self,
py: &Python,
entity_id: EntityId,
) -> Result<Option<State<ContextEvent>>, PyErr> {
let args = (entity_id.to_string(),); let args = (entity_id.to_string(),);
let state = self.0.call_method1(*py, "get", args)?; let state = self.0.call_method1(*py, "get", args)?;
state.extract(*py) state.extract(*py)