Skip to main content

homeassistant_agent/model/
id.rs

1use crate::model::Component;
2use std::borrow::Cow;
3
4#[derive(Clone, Debug)]
5pub struct DeviceId {
6    pub id: Cow<'static, str>,
7    pub component: Component,
8    pub node_id: Option<Cow<'static, str>>,
9}
10
11impl DeviceId {
12    pub fn new(id: impl Into<Cow<'static, str>>, component: Component) -> Self {
13        Self {
14            id: id.into(),
15            component,
16            node_id: None,
17        }
18    }
19    pub fn with_node_id<I, C>(
20        id: impl Into<Cow<'static, str>>,
21        component: Component,
22        node_id: impl Into<Cow<'static, str>>,
23    ) -> Self {
24        Self {
25            id: id.into(),
26            component,
27            node_id: Some(node_id.into()),
28        }
29    }
30
31    /// render the config topic
32    pub fn config_topic(&self) -> String {
33        format!(
34            "{component}/{node_id}{node_id_slash}{object_id}/config",
35            component = self.component,
36            object_id = self.id,
37            node_id_slash = if self.node_id.is_some() { "/" } else { "" },
38            node_id = self.node_id.as_deref().unwrap_or(""),
39        )
40    }
41}