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:
2026-01-07 02:10:03 -05:00
parent 97aef026b2
commit fa36b39e81
35 changed files with 1255 additions and 259 deletions

View File

@@ -2,17 +2,20 @@ use std::convert::Infallible;
use pyo3::prelude::*;
use python_utils::{detach, validate_type_by_name};
use python_utils::{detach, validate_type_by_name, TypeByNameValidationError};
use snafu::{ResultExt, Snafu};
use super::{service_registry::ServiceRegistry, state_machine::StateMachine};
#[derive(Debug)]
pub struct HomeAssistant(Py<PyAny>);
impl<'source> FromPyObject<'source> for HomeAssistant {
fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult<Self> {
impl<'a, 'py> FromPyObject<'a, 'py> for HomeAssistant {
type Error = TypeByNameValidationError;
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
// region: Validation
validate_type_by_name(ob, "HomeAssistant")?;
validate_type_by_name(&ob, "HomeAssistant")?;
// endregion: Validation
Ok(Self(detach(ob)))
@@ -29,6 +32,24 @@ impl<'py> IntoPyObject<'py> for &HomeAssistant {
}
}
#[derive(Debug, Snafu)]
pub enum GetStatesError {
/// couldn't get the `states` attribute on the Home Assistant object
GetStatesAttributeError { source: PyErr },
/// couldn't extract the `states` as a [`StateMachine`]
ExtractStateMachineError { source: TypeByNameValidationError },
}
#[derive(Debug, Snafu)]
pub enum GetServicesError {
/// couldn't get the `services` attribute on the Home Assistant object
GetServicesAttributeError { source: PyErr },
/// couldn't extract the `states` as a [`ServiceRegistry`]
ExtractServiceRegistryError { source: TypeByNameValidationError },
}
impl HomeAssistant {
/// Return the representation
pub fn repr(&self, py: Python<'_>) -> Result<String, PyErr> {
@@ -48,13 +69,19 @@ impl HomeAssistant {
is_stopping.extract(py)
}
pub fn states(&self, py: Python<'_>) -> Result<StateMachine, PyErr> {
let states = self.0.getattr(py, "states")?;
states.extract(py)
pub fn states(&self, py: Python<'_>) -> Result<StateMachine, GetStatesError> {
let states = self
.0
.getattr(py, "states")
.context(GetStatesAttributeSnafu)?;
states.extract(py).context(ExtractStateMachineSnafu)
}
pub fn services(&self, py: Python<'_>) -> Result<ServiceRegistry, PyErr> {
let services = self.0.getattr(py, "services")?;
services.extract(py)
pub fn services(&self, py: Python<'_>) -> Result<ServiceRegistry, GetServicesError> {
let services = self
.0
.getattr(py, "services")
.context(GetServicesAttributeSnafu)?;
services.extract(py).context(ExtractServiceRegistrySnafu)
}
}