craftbeerpi4-pione/cbpi/api/sensor.py
avollkopf be1f066782 Minor change sensor update
Change for sensor plugins.

Will update also mqtt per default when push_update is called.

But if False is send for mqtt parameter, only web interface will be updated.

-> Good for sensors that retrieve values only once per minute or so
-> WI can be continuosly updated with current value and no empty value is displayed, but mqtt does not nee to be updated

(example ispindel, scd30 sensor, kettle sensor, Hydrom/Tilt,....)

Plugins will be updated later in main branches
2022-01-14 17:11:16 +01:00

67 lines
1.6 KiB
Python

import asyncio
import logging
from abc import abstractmethod, ABCMeta
from cbpi.api.extension import CBPiExtension
from cbpi.api.base import CBPiBase
class CBPiSensor(CBPiBase, metaclass=ABCMeta):
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)
def get_state(self):
pass
def get_value(self):
pass
def get_unit(self):
pass
def push_update(self, value, mqtt = True):
try:
self.cbpi.ws.send(dict(topic="sensorstate", id=self.id, value=value))
if mqtt:
self.cbpi.push_update("cbpi/sensordata/{}".format(self.id), dict(id=self.id, value=value), retain=True)
# self.cbpi.push_update("cbpi/sensor/{}/udpate".format(self.id), dict(id=self.id, value=value), retain=True)
except:
logging.error("Faild to push sensor update")
async def start(self):
pass
async def stop(self):
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()