From 04f9aa24cfa38e9a010efcf3b98b5849fe5f71f6 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 22 Mar 2025 16:53:26 -0400 Subject: [PATCH] feat: implement `IntoPyObject` for `EntityId` --- src/home_assistant/entity_id.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/home_assistant/entity_id.rs b/src/home_assistant/entity_id.rs index 0a7bb81..ef3356a 100644 --- a/src/home_assistant/entity_id.rs +++ b/src/home_assistant/entity_id.rs @@ -1,6 +1,6 @@ -use std::{fmt::Display, str::FromStr}; +use std::{convert::Infallible, fmt::Display, str::FromStr}; -use pyo3::{exceptions::PyValueError, prelude::*}; +use pyo3::{exceptions::PyValueError, prelude::*, types::PyString}; use snafu::{ResultExt, Snafu}; use super::{ @@ -58,3 +58,14 @@ impl<'py> FromPyObject<'py> for EntityId { Ok(entity_id) } } + +impl<'py> IntoPyObject<'py> for &EntityId { + type Target = PyString; + type Output = Bound<'py, Self::Target>; + type Error = Infallible; + + fn into_pyobject(self, py: Python<'py>) -> Result { + let s = self.to_string(); + s.into_pyobject(py) + } +}