feat: early interface with Python

This commit is contained in:
2025-03-13 15:38:51 -04:00
parent afe5eae96b
commit 96495b2a85
22 changed files with 623 additions and 6 deletions

View File

@@ -0,0 +1,34 @@
use std::str::FromStr;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use crate::home_assistant::{entity_id::EntityId, state::State};
#[derive(Debug, Clone)]
pub struct Type;
impl<'py> FromPyObject<'py> for Type {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let s = ob.extract::<&str>()?;
if s == "state_changed" {
Ok(Type)
} else {
Err(PyValueError::new_err(format!(
"expected a string of value 'state_changed', but got {s}"
)))
}
}
}
#[derive(Debug, FromPyObject)]
#[pyo3(from_item_all)]
pub struct Data {
pub entity_id: EntityId,
pub old_state: Option<State>,
pub new_state: Option<State>,
}
/// A state changed event is fired when on state write the state is changed.
pub type Event = super::super::event::Event<Type, Data>;