From 0c86da392af50c7588b087c3f72602e8368af65e Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 5 Dec 2025 12:17:01 +0000 Subject: reworked entity creation --- src/entity_binary_sensor.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/entity_binary_sensor.rs (limited to 'src/entity_binary_sensor.rs') 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 @@ +use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, constants}; + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub enum BinarySensorClass { + #[default] + Generic, + Battery, + BatteryCharging, + Connectivity, + Door, + GarageDoor, + Motion, + Occupancy, + Opening, + Plug, + Power, + Presence, + Problem, + Smoke, + Window, +} + +#[derive(Debug, Default)] +pub struct BinarySensorConfig { + pub common: EntityCommonConfig, + pub class: BinarySensorClass, +} + +impl BinarySensorConfig { + pub(crate) fn populate(&self, config: &mut EntityConfig) { + self.common.populate(config); + config.domain = constants::HA_DOMAIN_BINARY_SENSOR; + config.device_class = match self.class { + BinarySensorClass::Generic => None, + BinarySensorClass::Battery => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_BATTERY), + BinarySensorClass::BatteryCharging => { + Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_BATTERY_CHARGING) + } + BinarySensorClass::Connectivity => { + Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_CONNECTIVITY) + } + BinarySensorClass::Door => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_DOOR), + BinarySensorClass::GarageDoor => { + Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_GARAGE_DOOR) + } + BinarySensorClass::Motion => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_MOTION), + BinarySensorClass::Occupancy => { + Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_OCCUPANCY) + } + BinarySensorClass::Opening => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_OPENING), + BinarySensorClass::Plug => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_PLUG), + BinarySensorClass::Power => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_POWER), + BinarySensorClass::Presence => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_PRESENCE), + BinarySensorClass::Problem => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_PROBLEM), + BinarySensorClass::Smoke => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_SMOKE), + BinarySensorClass::Window => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_WINDOW), + }; + } +} + +pub struct BinarySensor<'a>(Entity<'a>); + +impl<'a> BinarySensor<'a> { + pub(crate) fn new(entity: Entity<'a>) -> Self { + Self(entity) + } + + pub fn set(&mut self, state: BinaryState) { + self.0.publish(state.as_str().as_bytes()); + } + + pub fn value(&self) -> Option { + self.0 + .with_data(|data| BinaryState::try_from(data.publish_value.as_slice())) + .ok() + } + + pub fn toggle(&mut self) -> BinaryState { + let new_state = self.value().unwrap_or(BinaryState::Off).flip(); + self.set(new_state); + new_state + } +} -- cgit