aboutsummaryrefslogtreecommitdiff
path: root/src/entity_binary_sensor.rs
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-12-05 12:17:01 +0000
committerdiogo464 <[email protected]>2025-12-05 12:17:01 +0000
commit0c86da392af50c7588b087c3f72602e8368af65e (patch)
tree894cd2f353298b83a56cde06eafd7b1e366aa6b3 /src/entity_binary_sensor.rs
parent1d2ee64d0ec917a2c2b66f8d58e1f37dd174a89d (diff)
reworked entity creation
Diffstat (limited to 'src/entity_binary_sensor.rs')
-rw-r--r--src/entity_binary_sensor.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/entity_binary_sensor.rs b/src/entity_binary_sensor.rs
new file mode 100644
index 0000000..b80f718
--- /dev/null
+++ b/src/entity_binary_sensor.rs
@@ -0,0 +1,83 @@
1use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, constants};
2
3#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
4pub enum BinarySensorClass {
5 #[default]
6 Generic,
7 Battery,
8 BatteryCharging,
9 Connectivity,
10 Door,
11 GarageDoor,
12 Motion,
13 Occupancy,
14 Opening,
15 Plug,
16 Power,
17 Presence,
18 Problem,
19 Smoke,
20 Window,
21}
22
23#[derive(Debug, Default)]
24pub struct BinarySensorConfig {
25 pub common: EntityCommonConfig,
26 pub class: BinarySensorClass,
27}
28
29impl BinarySensorConfig {
30 pub(crate) fn populate(&self, config: &mut EntityConfig) {
31 self.common.populate(config);
32 config.domain = constants::HA_DOMAIN_BINARY_SENSOR;
33 config.device_class = match self.class {
34 BinarySensorClass::Generic => None,
35 BinarySensorClass::Battery => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_BATTERY),
36 BinarySensorClass::BatteryCharging => {
37 Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_BATTERY_CHARGING)
38 }
39 BinarySensorClass::Connectivity => {
40 Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_CONNECTIVITY)
41 }
42 BinarySensorClass::Door => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_DOOR),
43 BinarySensorClass::GarageDoor => {
44 Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_GARAGE_DOOR)
45 }
46 BinarySensorClass::Motion => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_MOTION),
47 BinarySensorClass::Occupancy => {
48 Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_OCCUPANCY)
49 }
50 BinarySensorClass::Opening => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_OPENING),
51 BinarySensorClass::Plug => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_PLUG),
52 BinarySensorClass::Power => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_POWER),
53 BinarySensorClass::Presence => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_PRESENCE),
54 BinarySensorClass::Problem => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_PROBLEM),
55 BinarySensorClass::Smoke => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_SMOKE),
56 BinarySensorClass::Window => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_WINDOW),
57 };
58 }
59}
60
61pub struct BinarySensor<'a>(Entity<'a>);
62
63impl<'a> BinarySensor<'a> {
64 pub(crate) fn new(entity: Entity<'a>) -> Self {
65 Self(entity)
66 }
67
68 pub fn set(&mut self, state: BinaryState) {
69 self.0.publish(state.as_str().as_bytes());
70 }
71
72 pub fn value(&self) -> Option<BinaryState> {
73 self.0
74 .with_data(|data| BinaryState::try_from(data.publish_value.as_slice()))
75 .ok()
76 }
77
78 pub fn toggle(&mut self) -> BinaryState {
79 let new_state = self.value().unwrap_or(BinaryState::Off).flip();
80 self.set(new_state);
81 new_state
82 }
83}