diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 615854a..a75a231 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.1.6.b1" +__version__ = "4.1.6.b2" __codename__ = "Groundhog Day" diff --git a/cbpi/extension/timer/__init__.py b/cbpi/extension/timer/__init__.py new file mode 100644 index 0000000..bd621e8 --- /dev/null +++ b/cbpi/extension/timer/__init__.py @@ -0,0 +1,105 @@ +# -*- coding: utf-8 -*- +import os +from aiohttp import web +import logging +import asyncio +from cbpi.api import * +from cbpi.api import base +from time import strftime, gmtime +import datetime +from cbpi.api.timer import Timer +from cbpi.api.dataclasses import DataType +from cbpi.api.dataclasses import NotificationAction, NotificationType + +logger = logging.getLogger(__name__) + + +@parameters([]) +class AlarmTimer(CBPiSensor): + + def __init__(self, cbpi, id, props): + super(AlarmTimer, self).__init__(cbpi, id, props) + self.value = "00:00:00" + self.datatype=DataType.STRING + self.timer = None + self.time=0 + self.stopped=False + self.sensor=self.get_sensor(self.id) + + @action(key="Set Timer", parameters=[Property.Number(label="time", description="Time in Minutes", configurable=True)]) + async def set(self, time = 0,**kwargs): + self.stopped=False + self.time = float(time) + self.value=self.calculate_time(self.time) + await self.timer.stop() + self.timer = Timer(int(self.time * 60), on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer.start() + await self.timer.stop() + self.timer.is_running = False + logging.info("Set Timer") + + @action(key="Start Timer", parameters=[]) + async def start(self , **kwargs): + if self.timer is None: + self.timer = Timer(int(self.time * 60), on_update=self.on_timer_update, on_done=self.on_timer_done) + + if self.timer.is_running is not True: + self.cbpi.notify(self.sensor.name,'Timer started', NotificationType.INFO) + self.timer.start() + self.stopped=False + self.timer.is_running = True + else: + self.cbpi.notify(self.sensor.name,'Timer is already running', NotificationType.WARNING) + + @action(key="Stop Timer", parameters=[]) + async def stop(self , **kwargs): + self.stopped=False + await self.timer.stop() + self.timer.is_running = False + self.cbpi.notify(self.sensor.name,'Timer stopped', NotificationType.INFO) + logging.info("Stop Timer") + + @action(key="Reset Timer", parameters=[]) + async def Reset(self , **kwargs): + self.stopped=False + await self.timer.stop() + self.value=self.calculate_time(self.time) + self.timer = Timer(int(self.time * 60), on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer.start() + await self.timer.stop() + self.timer.is_running = False + logging.info("Reset Timer") + + async def on_timer_done(self, timer): + #self.value = "Stopped" + if self.stopped is True: + self.cbpi.notify(self.sensor.name,'Timer done', NotificationType.INFO) + + self.timer.is_running = False + pass + + async def on_timer_update(self, timer, seconds): + self.stopped=True + self.value = Timer.format_time(seconds) + #await self.push_update() + + async def NextStep(self): + self.next = True + pass + + async def run(self): + while self.running is True: + self.push_update(self.value) + await asyncio.sleep(1) + pass + + def get_state(self): + return dict(value=self.value) + + def calculate_time(self, time): + return strftime("%H:%M:%S", gmtime(time*60)) + + +def setup(cbpi): + cbpi.plugin.register("AlarmTimer", AlarmTimer) + pass \ No newline at end of file diff --git a/cbpi/extension/timer/config.yaml b/cbpi/extension/timer/config.yaml new file mode 100644 index 0000000..a777645 --- /dev/null +++ b/cbpi/extension/timer/config.yaml @@ -0,0 +1,3 @@ +name: timer +version: 4 +active: true \ No newline at end of file