craftbeerpi4-pione/cbpi/api/actor.py

101 lines
2.1 KiB
Python
Raw Normal View History

2019-01-05 20:43:48 +01:00
from abc import ABCMeta
2021-01-22 23:25:20 +01:00
import asyncio
2021-02-06 00:41:50 +01:00
from cbpi.api.config import ConfigType
2019-01-05 20:43:48 +01:00
2021-01-23 14:41:26 +01:00
__all__ = ["CBPiActor"]
2019-01-05 20:43:48 +01:00
import logging
logger = logging.getLogger(__file__)
2021-01-23 14:41:26 +01:00
class CBPiActor(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.logger = logging.getLogger(__file__)
self.data_logger = None
self.state = False
self.running = False
2021-11-06 15:15:11 +01:00
self.power = 100
self.timer = 0
2021-01-22 23:25:20 +01:00
def init(self):
pass
def log_data(self, value):
self.cbpi.log.log_data(self.id, value)
def get_state(self):
return dict(state=self.state)
async def start(self):
2021-02-16 20:37:51 +01:00
pass
2021-01-22 23:25:20 +01:00
async def stop(self):
2021-02-16 20:37:51 +01:00
pass
async def on_start(self):
pass
async def on_stop(self):
pass
async def run(self):
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
2021-02-06 00:41:50 +01:00
def get_static_config_value(self,name,default):
return self.cbpi.static_config.get(name, default)
def get_config_value(self,name,default):
return self.cbpi.config.get(name, default=default)
async def set_config_value(self,name,value):
return await self.cbpi.config.set(name,value)
async def add_config_value(self, name, value, type: ConfigType, description, options=None):
await self.cbpi.add(name, value, type, description, options=None)
2021-01-22 23:25:20 +01:00
async def on(self, power):
'''
Code to switch the actor on. Power is provided as integer value
:param power: power value between 0 and 100
:return: None
'''
pass
async def off(self):
'''
Code to switch the actor off
:return: None
'''
pass
async def set_power(self,power):
'''
2021-11-11 11:22:13 +01:00
Code to set power for actor
2021-11-11 11:22:13 +01:00
:return: dict power
'''
return dict(power=self.power)
pass
2021-01-22 23:25:20 +01:00