2021-02-10 22:11:35 +01:00
|
|
|
import asyncio
|
|
|
|
from asyncio import tasks
|
|
|
|
import logging
|
|
|
|
from cbpi.api import *
|
|
|
|
|
|
|
|
@parameters([Property.Number(label="OffsetOn", configurable=True, description="Offset below target temp when heater should switched on"),
|
2021-03-30 07:46:41 +02:00
|
|
|
Property.Number(label="OffsetOff", configurable=True, description="Offset below target temp when heater should switched off")])
|
2021-02-10 22:11:35 +01:00
|
|
|
class Hysteresis(CBPiKettleLogic):
|
|
|
|
|
|
|
|
async def run(self):
|
|
|
|
try:
|
|
|
|
self.offset_on = float(self.props.get("OffsetOn", 0))
|
|
|
|
self.offset_off = float(self.props.get("OffsetOff", 0))
|
|
|
|
self.kettle = self.get_kettle(self.id)
|
2021-02-16 20:37:51 +01:00
|
|
|
self.heater = self.kettle.heater
|
2023-03-02 06:51:57 +01:00
|
|
|
heater = self.cbpi.actor.find_by_id(self.heater)
|
2021-02-17 23:52:54 +01:00
|
|
|
logging.info("Hysteresis {} {} {} {}".format(self.offset_on, self.offset_off, self.id, self.heater))
|
|
|
|
|
2021-03-07 02:05:19 +01:00
|
|
|
# self.get_actor_state()
|
|
|
|
|
2021-02-17 23:52:54 +01:00
|
|
|
|
2021-03-08 01:34:13 +01:00
|
|
|
while self.running == True:
|
2021-02-17 23:52:54 +01:00
|
|
|
|
2021-02-16 20:37:51 +01:00
|
|
|
sensor_value = self.get_sensor_value(self.kettle.sensor).get("value")
|
|
|
|
target_temp = self.get_kettle_target_temp(self.id)
|
2023-03-02 06:51:57 +01:00
|
|
|
try:
|
|
|
|
heater_state=heater.instance.state
|
|
|
|
except:
|
|
|
|
heater_state = False
|
2021-02-10 22:11:35 +01:00
|
|
|
if sensor_value < target_temp - self.offset_on:
|
2023-03-02 06:51:57 +01:00
|
|
|
if self.heater and (heater_state == False):
|
|
|
|
await self.actor_on(self.heater)
|
2021-03-30 07:46:41 +02:00
|
|
|
elif sensor_value >= target_temp - self.offset_off:
|
2023-03-02 06:51:57 +01:00
|
|
|
if self.heater and (heater_state == True):
|
|
|
|
await self.actor_off(self.heater)
|
2021-02-10 22:11:35 +01:00
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
except asyncio.CancelledError as e:
|
|
|
|
pass
|
|
|
|
except Exception as e:
|
|
|
|
logging.error("CustomLogic Error {}".format(e))
|
|
|
|
finally:
|
|
|
|
self.running = False
|
|
|
|
await self.actor_off(self.heater)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup(cbpi):
|
|
|
|
|
|
|
|
'''
|
|
|
|
This method is called by the server during startup
|
|
|
|
Here you need to register your plugins at the server
|
|
|
|
|
|
|
|
:param cbpi: the cbpi core
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
|
|
|
|
cbpi.plugin.register("Hysteresis", Hysteresis)
|