Don't send opening message if only open_since changed

This happens with HomeAssistant reboots.
This commit is contained in:
Xiretza 2024-07-22 12:01:01 +00:00
parent a339e71e6d
commit c693b41a70

View file

@ -58,6 +58,12 @@ pub enum OpenState {
Closed, Closed,
} }
impl OpenState {
pub fn is_open(&self) -> bool {
matches!(self, OpenState::Open { .. })
}
}
#[instrument(skip(api))] #[instrument(skip(api))]
async fn get_open_state(api: &ItsApi) -> Result<OpenState> { async fn get_open_state(api: &ItsApi) -> Result<OpenState> {
let state = api let state = api
@ -146,13 +152,16 @@ impl Bot {
#[instrument(skip(self))] #[instrument(skip(self))]
async fn update_open_state(&self) -> Result<OpenState> { async fn update_open_state(&self) -> Result<OpenState> {
let new_state = get_open_state(&self.api).await?; let new_state = get_open_state(&self.api).await?;
if self
let is_open = new_state.is_open();
let was_open = self
.open_state .open_state
.lock() .lock()
.unwrap() .unwrap()
.is_some_and(|old_state| old_state != new_state) .as_ref()
{ .map(OpenState::is_open);
let message = if let OpenState::Open { .. } = new_state { if was_open.is_some_and(|was_open| was_open != is_open) {
let message = if is_open {
"opening IT-Syndikat - Ohai!" "opening IT-Syndikat - Ohai!"
} else { } else {
"closing IT-Syndikat - nap time!" "closing IT-Syndikat - nap time!"