-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
42 lines (36 loc) · 1.34 KB
/
timer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
""" module for SlxTimer """
from homeassistant.helpers.event import async_call_later
from homeassistant.core import HomeAssistant
from datetime import timedelta
from collections.abc import Callable
import logging
_LOGGER = logging.getLogger(__name__)
class SlxTimer:
def __init__(
self,
hass: HomeAssistant,
default_time: timedelta,
timer_callback: Callable[[], None],
):
self.hass = hass
self.wait_time: timedelta = default_time
self.timer_callback = timer_callback
self.unsub_callback: Callable[[], None] = None
def schedule_timer(self, new_wait_time: timedelta = None):
if self.unsub_callback is not None:
self.unsub_callback()
self.unsub_callback = None
_LOGGER.debug("Cancel timer %s before scheduling again", __name__)
wait_time_to_use = self.wait_time
if new_wait_time is not None:
wait_time_to_use = new_wait_time
self.unsub_callback = async_call_later(
self.hass, wait_time_to_use, self.timer_callback
)
def cancel_timer(self):
if self.unsub_callback is not None:
self.unsub_callback()
self.unsub_callback = None
_LOGGER.debug("Canceled timer %s", __name__)
else:
_LOGGER.debug("Ignore canceling timer %s", __name__)