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:
35
home-assistant/src/state/unexpected_state.rs
Normal file
35
home-assistant/src/state/unexpected_state.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use pyo3::{exceptions::PyException, prelude::*};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
#[derive(Debug, Clone, derive_more::Display)]
|
||||
pub struct UnexpectedState(pub Arc<str>);
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum ExtractUnexpectedStateError {
|
||||
/// couldn't extract the object as a string
|
||||
ExtractStringError { source: PyErr },
|
||||
}
|
||||
|
||||
impl From<ExtractUnexpectedStateError> for PyErr {
|
||||
fn from(error: ExtractUnexpectedStateError) -> Self {
|
||||
match &error {
|
||||
ExtractUnexpectedStateError::ExtractStringError { .. } => {
|
||||
PyException::new_err(error.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: replace with a derive(PyFromStr) (analogous to serde_with::DeserializeFromStr) once I make one
|
||||
impl<'a, 'py> FromPyObject<'a, 'py> for UnexpectedState {
|
||||
type Error = ExtractUnexpectedStateError;
|
||||
|
||||
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
|
||||
let s = ob.extract::<String>().context(ExtractStringSnafu)?;
|
||||
let s = s.into();
|
||||
|
||||
Ok(UnexpectedState(s))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user