2020//! unique_id: Some("motion"),
2121//! name: "Motion",
2222//! availability: AvailabilityTopics::All([DEVICE_AVAILABILITY_TOPIC]),
23- //! state_topic: MOTION_STATE_TOPIC,
23+ //! state_topic: Some(MOTION_STATE_TOPIC),
24+ //! command_topic: None,
2425//! component: BinarySensor {
2526//! device_class: Some(BinarySensorClass::Motion),
2627//! },
@@ -206,7 +207,9 @@ pub struct Entity<'a, const A: usize, C: Component> {
206207 /// determine this entity's availability.
207208 pub availability : AvailabilityTopics < ' a , A > ,
208209 /// The state topic that this entity's state is published to.
209- pub state_topic : Topic < & ' a str > ,
210+ pub state_topic : Option < Topic < & ' a str > > ,
211+ /// The command topic that this entity receives commands from.
212+ pub command_topic : Option < Topic < & ' a str > > ,
210213 /// The specific entity.
211214 pub component : C ,
212215}
@@ -233,8 +236,16 @@ impl<const A: usize, C: Component> Entity<'_, A, C> {
233236 }
234237
235238 /// Publishes this entity's state to the broker.
239+ ///
240+ /// # Errors
241+ ///
242+ /// - [`Error::Invalid`] if the entity doesn't have a state topic.
236243 pub async fn publish_state ( & self , state : C :: State ) -> Result < ( ) , Error > {
237- self . component . publish_state ( & self . state_topic , state) . await
244+ if let Some ( topic) = self . state_topic {
245+ self . component . publish_state ( & topic, state) . await
246+ } else {
247+ Err ( Error :: Invalid )
248+ }
238249 }
239250}
240251
0 commit comments