diff options
Diffstat (limited to 'src/entity_button.rs')
| -rw-r--r-- | src/entity_button.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/entity_button.rs b/src/entity_button.rs new file mode 100644 index 0000000..baa89a4 --- /dev/null +++ b/src/entity_button.rs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | use crate::{Entity, EntityCommonConfig, EntityConfig, constants}; | ||
| 2 | |||
| 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | ||
| 4 | pub enum ButtonClass { | ||
| 5 | #[default] | ||
| 6 | Generic, | ||
| 7 | Identify, | ||
| 8 | Restart, | ||
| 9 | Update, | ||
| 10 | } | ||
| 11 | |||
| 12 | #[derive(Debug, Default)] | ||
| 13 | pub struct ButtonConfig { | ||
| 14 | pub common: EntityCommonConfig, | ||
| 15 | pub class: ButtonClass, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl ButtonConfig { | ||
| 19 | pub(crate) fn populate(&self, config: &mut EntityConfig) { | ||
| 20 | self.common.populate(config); | ||
| 21 | config.domain = constants::HA_DOMAIN_BUTTON; | ||
| 22 | config.device_class = match self.class { | ||
| 23 | ButtonClass::Generic => None, | ||
| 24 | ButtonClass::Identify => Some(constants::HA_DEVICE_CLASS_BUTTON_IDENTIFY), | ||
| 25 | ButtonClass::Restart => Some(constants::HA_DEVICE_CLASS_BUTTON_RESTART), | ||
| 26 | ButtonClass::Update => Some(constants::HA_DEVICE_CLASS_BUTTON_UPDATE), | ||
| 27 | }; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | pub struct Button<'a>(Entity<'a>); | ||
| 32 | |||
| 33 | impl<'a> Button<'a> { | ||
| 34 | pub(crate) fn new(entity: Entity<'a>) -> Self { | ||
| 35 | Self(entity) | ||
| 36 | } | ||
| 37 | |||
| 38 | pub async fn pressed(&mut self) { | ||
| 39 | self.0.wait_command().await; | ||
| 40 | } | ||
| 41 | } | ||
