chore+feat(home-assistant): update to pyo3 0.27 and update extraction errors, switch out SmolStr for Arc<str>, tighten up light service calls and implement some for notify, start implementing units of measurement like for power
This commit is contained in:
@@ -1,34 +1,58 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::entity_id::EntityId;
|
||||
use super::state_object::StateObject;
|
||||
use pyo3::prelude::*;
|
||||
use python_utils::{detach, validate_type_by_name};
|
||||
use python_utils::{detach, validate_type_by_name, TypeByNameValidationError};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct StateMachine(Py<PyAny>);
|
||||
|
||||
impl<'py> FromPyObject<'py> for StateMachine {
|
||||
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
|
||||
impl<'a, 'py> FromPyObject<'a, 'py> for StateMachine {
|
||||
type Error = TypeByNameValidationError;
|
||||
|
||||
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
|
||||
// region: Validation
|
||||
validate_type_by_name(ob, "StateMachine")?;
|
||||
validate_type_by_name(&ob, "StateMachine")?;
|
||||
// endregion: Validation
|
||||
|
||||
Ok(Self(detach(ob)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Snafu)]
|
||||
pub enum GetStateError<ExtractStateObjectError: 'static + snafu::Error> {
|
||||
/// couldn't get this state object from the state machine
|
||||
GetStateObjectError { source: Arc<PyErr> },
|
||||
|
||||
/// couldn't extract the state as a [`StateObject`]
|
||||
ExtractStateObjectError { source: ExtractStateObjectError },
|
||||
}
|
||||
|
||||
impl StateMachine {
|
||||
pub fn get<
|
||||
'a,
|
||||
'py,
|
||||
State: FromPyObject<'py>,
|
||||
Attributes: FromPyObject<'py>,
|
||||
ContextEvent: FromPyObject<'py>,
|
||||
State: FromPyObjectOwned<'py>,
|
||||
Attributes: FromPyObjectOwned<'py>,
|
||||
ContextEvent: FromPyObjectOwned<'py>,
|
||||
>(
|
||||
&self,
|
||||
&'a self,
|
||||
py: Python<'py>,
|
||||
entity_id: EntityId,
|
||||
) -> PyResult<Option<StateObject<State, Attributes, ContextEvent>>> {
|
||||
) -> Result<
|
||||
Option<StateObject<State, Attributes, ContextEvent>>,
|
||||
GetStateError<
|
||||
<Option<StateObject<State, Attributes, ContextEvent>> as FromPyObject<'a, 'py>>::Error,
|
||||
>,
|
||||
> {
|
||||
let args = (entity_id.to_string(),);
|
||||
let state = self.0.call_method1(py, "get", args)?;
|
||||
state.extract(py)
|
||||
let state = self
|
||||
.0
|
||||
.call_method1(py, "get", args)
|
||||
.map_err(Arc::new)
|
||||
.context(GetStateObjectSnafu)?;
|
||||
Ok(state.extract(py).context(ExtractStateObjectSnafu)?)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user