craftbeerpi4-pione/cbpi/api/kettle_logic.py

52 lines
1,017 B
Python
Raw Normal View History

2021-02-10 22:11:35 +01:00
from cbpi.api.base import CBPiBase
2019-01-05 20:43:48 +01:00
from cbpi.api.extension import CBPiExtension
2021-01-22 23:25:20 +01:00
from abc import ABCMeta
import logging
import asyncio
2019-01-05 20:43:48 +01:00
2021-02-10 22:11:35 +01:00
class CBPiKettleLogic(CBPiBase, metaclass=ABCMeta):
2021-01-22 23:25:20 +01:00
def __init__(self, cbpi, id, props):
self.cbpi = cbpi
self.id = id
self.props = props
self.state = False
self.running = False
def init(self):
pass
2021-02-16 20:37:51 +01:00
async def on_start(self):
pass
async def on_stop(self):
pass
2021-01-22 23:25:20 +01:00
async def run(self):
2021-02-16 20:37:51 +01:00
pass
async def _run(self):
try:
await self.on_start()
self.cancel_reason = await self.run()
except asyncio.CancelledError as e:
pass
finally:
await self.on_stop()
2021-01-22 23:25:20 +01:00
def get_state(self):
2021-02-16 20:37:51 +01:00
return dict(running=self.state)
2021-01-22 23:25:20 +01:00
async def start(self):
2021-02-16 20:37:51 +01:00
self.state = True
2021-01-22 23:25:20 +01:00
async def stop(self):
2021-02-16 20:37:51 +01:00
2021-02-10 22:11:35 +01:00
self.task.cancel()
await self.task
2021-02-16 20:37:51 +01:00
self.state = False