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,
}
impl OpenState {
pub fn is_open(&self) -> bool {
matches!(self, OpenState::Open { .. })
}
}
#[instrument(skip(api))]
async fn get_open_state(api: &ItsApi) -> Result<OpenState> {
let state = api
@ -146,13 +152,16 @@ impl Bot {
#[instrument(skip(self))]
async fn update_open_state(&self) -> Result<OpenState> {
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!"