Skip to content

Commit 57d5b9f

Browse files
medusalixMossop
authored andcommitted
Add Button component
1 parent b5f0a34 commit 57d5b9f

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/homeassistant/button.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Tools for publishing a [Home Assistant button](https://www.home-assistant.io/integrations/button.mqtt/).
2+
use core::ops::Deref;
3+
4+
use serde::Serialize;
5+
6+
use crate::{homeassistant::Component, Error, Topic};
7+
8+
/// The type of button.
9+
#[derive(Serialize)]
10+
#[serde(rename_all = "snake_case")]
11+
#[allow(missing_docs)]
12+
pub enum ButtonClass {
13+
Identify,
14+
Restart,
15+
Update,
16+
}
17+
18+
/// A button that can be pressed.
19+
#[derive(Serialize)]
20+
pub struct Button {
21+
/// The type of button.
22+
pub device_class: Option<ButtonClass>,
23+
}
24+
25+
impl Component for Button {
26+
type State = ();
27+
28+
fn platform() -> &'static str {
29+
"button"
30+
}
31+
32+
async fn publish_state<T: Deref<Target = str>>(
33+
&self,
34+
_topic: &Topic<T>,
35+
_state: Self::State,
36+
) -> Result<(), Error> {
37+
// Buttons don't have a state
38+
Err(Error::Invalid)
39+
}
40+
}

src/homeassistant/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::{
5050
};
5151

5252
pub mod binary_sensor;
53+
pub mod button;
5354
pub mod light;
5455
pub mod sensor;
5556
mod ser;

0 commit comments

Comments
 (0)