From c693b41a704b3fb091bdfe930084b4cf60dd6e1e Mon Sep 17 00:00:00 2001 From: Xiretza Date: Mon, 22 Jul 2024 12:01:01 +0000 Subject: [PATCH] Don't send opening message if only open_since changed This happens with HomeAssistant reboots. --- src/bot.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 479284f..95a7600 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -58,6 +58,12 @@ pub enum OpenState { Closed, } +impl OpenState { + pub fn is_open(&self) -> bool { + matches!(self, OpenState::Open { .. }) + } +} + #[instrument(skip(api))] async fn get_open_state(api: &ItsApi) -> Result { let state = api @@ -146,13 +152,16 @@ impl Bot { #[instrument(skip(self))] async fn update_open_state(&self) -> Result { let new_state = get_open_state(&self.api).await?; - if self + + let is_open = new_state.is_open(); + let was_open = self .open_state .lock() .unwrap() - .is_some_and(|old_state| old_state != new_state) - { - let message = if let OpenState::Open { .. } = new_state { + .as_ref() + .map(OpenState::is_open); + if was_open.is_some_and(|was_open| was_open != is_open) { + let message = if is_open { "opening IT-Syndikat - Ohai!" } else { "closing IT-Syndikat - nap time!"