Commit 530be454 authored by Peter-Bernd Otte's avatar Peter-Bernd Otte

Update python/relais-control/relais-control.py, python/relais-control/relais.py files

parent 4a04fc18
......@@ -36,6 +36,8 @@ def on_connect(client, userdata, flags, rc):
logger.debug("MQTT: "+x.MQTTname+" switchOffTopics subscribed")
for y in x.inverseSwitchOnTopics: client.subscribe(y)
logger.debug("MQTT: "+x.MQTTname+" inverseSwitchOnTopics subscribed")
for y in x.prolongateStateTopics: client.subscribe(y)
logger.debug("MQTT: "+x.MQTTname+" prolongateStateTopics subscribed")
client.subscribe(x.MQTTname+"/set")
logger.debug("MQTT: Subscribed to all topics")
else:
......@@ -102,12 +104,14 @@ if os.path.exists(args.config_file):
toggleTopics = cfg[section]["toggleTopics"] if "toggleTopics" in cfg[section] else []
switchOffTopics = cfg[section]["switchOffTopics"] if "switchOffTopics" in cfg[section] else []
inverseSwitchOnTopics = cfg[section]["inverseSwitchOnTopics"] if "inverseSwitchOnTopics" in cfg[section] else []
prolongateStateTopics = cfg[section]["prolongateStateTopics"] if "prolongateStateTopics" in cfg[section] else []
# print(type(toggleTopics))
if type(defaultTopics) is not list: defaultTopics = [defaultTopics]
if type(toggleTopics) is not list: toggleTopics = [toggleTopics]
if type(switchOffTopics) is not list: toggleTopics = [switchOffTopics]
if type(inverseSwitchOnTopics) is not list: toggleTopics = [inverseSwitchOnTopics]
if type(prolongateStateTopics) is not list: prolongateStateTopics = [prolongateStateTopics]
defaultSwitchOffTime = cfg[section]["defaultSwitchOffTime"] if "defaultSwitchOffTime" in cfg[section] else None
debugItem = cfg[section]["debug"] if "debug" in cfg[section] else debug
......@@ -115,6 +119,7 @@ if os.path.exists(args.config_file):
relaisList.append(relais(MQTTClient=client, MQTTName=MQTTName,
MQTTRelaisTopic=cfg[section]["MQTTRelaisTopic"], toggleTopics=toggleTopics, switchOffTopics=switchOffTopics,
defaultTopics=defaultTopics, inverseSwitchOnTopics=inverseSwitchOnTopics,
prolongateStateTopics=prolongateStateTopics,
defaultSwitchOffTime=defaultSwitchOffTime, debug=debugItem))
logger.info("Adding successfully.")
......@@ -131,7 +136,6 @@ sleepTime = 1 if args.verbosity>0 else 0.025
while True:
for x in relaisList:
x.check()
# x.relaisMsg(json.loads('{"time": 1564578103.410478, "type": "FSR14", "setv": 1.0, "v": 1.0}'))
time.sleep((1-time.time() % 1) * sleepTime) #every second, even if the processing before took longer
......
......@@ -21,7 +21,7 @@ class rLightDesire(Enum):
class relais:
def __init__(self, MQTTClient, MQTTName, MQTTRelaisTopic, defaultTopics=None, toggleTopics=None, switchOffTopics=None,
inverseSwitchOnTopics=None, defaultSwitchOffTime=None, debug=False):
inverseSwitchOnTopics=None, prolongateStateTopics=None, defaultSwitchOffTime=None, debug=False):
self.MQTTname = MQTTName #string
self.type = "relais"
self.debug = debug
......@@ -38,6 +38,7 @@ class relais:
self.toggleTopics = toggleTopics if toggleTopics is not None else set()
self.switchOffTopics = switchOffTopics if switchOffTopics is not None else set()
self.inverseSwitchOnTopics = inverseSwitchOnTopics if inverseSwitchOnTopics is not None else set()
self.prolongateStateTopics = prolongateStateTopics if prolongateStateTopics is not None else set()
self.v = None # state, the relais should have
self.timeLastChange = None # last change of v
......@@ -86,7 +87,7 @@ class relais:
if msg['v'] > 0:
self.lockingPIRs.add(topic)
self.lightdesire = rLightDesire.lockedDueToPIR
self.setV(1, reason="PIR Motion")
self.setV(1, reason="PIR motion")
else:
self.lockingPIRs.remove(topic)
if len(self.lockingPIRs) == 0:
......@@ -95,16 +96,36 @@ class relais:
self.publishCurrentStatus(reason="last PIR sees no motion")
if self.debug: print(self.MQTTname, "len(self.lockingPIRs)", len(self.lockingPIRs), "self.lockingPIRs",
self.lockingPIRs)
elif msg['type'] in ('FRW', 'FHF', 'FSM60B', 'FTK',
'FTKE'): # smoke detector, window handle, waterleak detector, magnet sensor, handle bar sensor
elif msg['type'] in ('FRW', 'FHF', 'FSM60B', 'FTK', 'FTKE'): # smoke, window handle, waterleak, magnet, handle bar
if msg['v'] > 0:
self.setV(1, reason="defaultTopics "+msg['type'])
else:
self.setV(0)
self.setV(0, reason="defaultTopics "+msg['type'])
elif msg['type'] == "PTM215": # switches
if msg['v'] > 0: self.setV(1, reason="PTM215 pressed down, switch on") # Switch on, if key pressed down
else:
print(self.MQTTname, "Error: Type not supported for normal actions.")
print(self.MQTTname, "Error: Type not supported for defaultTopics.")
for x in self.prolongateStateTopics: # topics will only prolong the relais state
if topic == x:
if self.debug: print(self.MQTTname,"Prolong Topic:", topic, "msg:", msg)
if msg['type'] in ('TFBHSB55', 'FBH63'): # motion sensors
if self.v > 0:
self.timeLastStateConfirm = time.time() # to avoid immediate switch off after last PIR
if msg['v'] > 0:
if self.v > 0: #difference to default behaviour
self.lockingPIRs.add(topic)
self.lightdesire = rLightDesire.lockedDueToPIR
self.publishCurrentStatus(reason="PIR motion prolong")
else:
self.lockingPIRs.remove(topic)
if self.v > 0 and len(self.lockingPIRs) == 0:
self.lightdesire = rLightDesire.stable
self.publishCurrentStatus(reason="last PIR sees no motion")
if self.debug: print(self.MQTTname, "Prolong State Topic:", "len(self.lockingPIRs)", len(self.lockingPIRs),
"self.lockingPIRs", self.lockingPIRs)
else:
print(self.MQTTname, "Error: Type not supported for prolongTopics.")
for x in self.toggleTopics: # topics do a toggle of the relais
if topic == x:
if self.debug: print(self.MQTTname, "Toggle Topic:", topic, "msg:", msg)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment