diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index fd401f9..1b335eb 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -154,41 +154,6 @@ async fn input_routine( type ModbusMutex = Mutex>; type ModbusOnceLock = OnceLock; -/// Fan state can have a setting while being off although and we emulate that behavior because -/// fan devices actually don't have that behavior -#[derive(PartialEq, Clone)] -struct FanState { - is_on: bool, - setting: SetPoint, -} - -struct FanController { - /// Is on and the setting update independent of each other on homeassistant. - /// Update this state even though it might not yet be set on the fan devices. - /// Use optimistic updates. - /// Senders: - /// - MQTT (server to client) - /// - Button - /// Receivers: - /// - Fan - /// - MQTT (client to server) - fan_states: ( - Watch, - Watch, - ), -} - -impl FanController { - const fn new() -> Self { - Self { - fan_states: (Watch::new(), Watch::new()), - } - } -} - -#[deprecated(note = "Use the new system")] -static FAN_CONTROLLER: FanController = FanController::new(); - /// This routine takes the latest fan state updates and updates all parts of the device that display a state. /// This includes at the time of writing Home Assistant through MQTT and two status LEDs on the device. /// Displays fan status with 2 LEDs: diff --git a/fan-controller/src/task.rs b/fan-controller/src/task.rs index 50f992f..e2d76f8 100644 --- a/fan-controller/src/task.rs +++ b/fan-controller/src/task.rs @@ -9,8 +9,8 @@ use crate::mqtt::packet::subscribe_acknowledgement::SubscribeAcknowledgement; use crate::mqtt::task::send; use crate::mqtt::{self}; use crate::mqtt::{TryDecode, non_zero_u16}; -use crate::{FanState, configuration, fan, gain_control}; use crate::{ModbusOnceLock, modbus}; +use crate::{configuration, fan, gain_control}; use ::mqtt::QualityOfService; use core::future::poll_fn; use core::num::NonZeroU16; @@ -96,80 +96,6 @@ async fn wait_for_acknowledgement( info!("[Subscription] Subscribe acknowledgement received") } -/// A handler that takes MQTT publishes and sets the fan settings accordingly -async fn handle_publish<'f>( - publish: &'f publish::Publish<'f>, - sender: &embassy_sync::watch::Sender<'_, CriticalSectionRawMutex, FanState, 3>, -) { - info!("Handling publish"); - - // This part is not MQTT but application specific - match publish.topic_name { - "fancontroller/speed/percentage" => { - let payload = match core::str::from_utf8(publish.payload) { - Ok(payload) => payload, - Err(error) => { - warn!( - "Expected percentage_command_topic payload (speed percentage) to be a valid UTF-8 string with a number" - ); - return; - } - }; - - // And then to an integer... - let set_point = payload.parse::(); - let set_point = match set_point { - Ok(set_point) => set_point, - Err(error) => { - warn!( - "Expected speed percentage to be a number string. Payload is: {}", - payload - ); - return; - } - }; - - info!("SETTING FAN {}", set_point); - let Ok(setting) = SetPoint::new(set_point) else { - warn!( - "Setting fan speed out of bounds. Not accepting new setting: {}", - set_point - ); - return; - }; - - sender.send(FanState { - setting, - // Set to on when there is a value set - is_on: true, - }); - // Home assistant and fan update will be done by receiver - } - topic::fan_controller::COMMAND => { - info!("Received fan set on command from homeassistant"); - info!( - "Payload: {:?}", - core::str::from_utf8(publish.payload).unwrap() - ); - let is_on = publish.payload == b"ON"; - info!("TURNING FAN {}", if is_on { "ON" } else { "OFF" }); - - sender.send(FanState { - setting: sender - .try_get() - .map(|state| state.setting) - .unwrap_or(SetPoint::ZERO), - is_on, - }); - // Home assistant and fan update will be done by receiver - } - other => warn!( - "Unexpected topic: {} with payload: {}", - other, publish.payload - ), - } -} - /// Callback handler for pings received when listening async fn handle_ping_response( ping_response: PingResponse,