craftbeerpi4-pione/cbpi/api/sensor.py

49 lines
1 KiB
Python
Raw Normal View History

2019-08-16 21:36:55 +02:00
import logging
2021-01-22 23:25:20 +01:00
from abc import abstractmethod, ABCMeta
2019-01-05 20:43:48 +01:00
from cbpi.api.extension import CBPiExtension
2021-01-22 23:25:20 +01:00
2021-01-23 14:41:26 +01:00
class CBPiSensor(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
def init(self):
pass
def log_data(self, value):
self.cbpi.log.log_data(self.id, value)
@abstractmethod
async def run(self):
self.logger.warning("Sensor Init not implemented")
def get_state(self):
pass
def get_value(self):
pass
def get_unit(self):
pass
2021-01-24 22:14:57 +01:00
def push_update(self, value):
try:
self.cbpi.ws.send(dict(topic="sensorstate", id=self.id, value=value))
except:
logging.error("Faild to push sensor update")
2021-01-22 23:25:20 +01:00
async def start(self):
self.running = True
async def stop(self):
self.running = False