Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import HABApp
- import logging
- from HABApp.mqtt.items import MqttItem
- from HABApp.openhab.items import SwitchItem, NumberItem, DimmerItem, ColorItem
- class Apply(object):
- def __init__(self, rule, **params):
- self.delayTime = 0.5
- self.rule = rule
- if "delayTime" in params:
- self.delayTime=params["delayTime"]
- if "actions" in params:
- self.actions=params["actions"]
- if "log" in params:
- self.log=params["log"]
- self.needs_delay=0
- if not hasattr(self, "actions"):
- return
- self.action_idx=0
- self.part_idx=0
- self.rule.run.soon(self.run)
- # self.string_to_dict("breadboard7leds_Led1_Switch=on")
- #self.string_to_dict("nodelay*dimmer*breadboard7leds_Led1_Switch=1.0")
- #mydict=self.string_to_dict("retain*homie/breadboard-7leds/led1/switch=false")
- #self.log.info(mydict)
- def isfloat(self,num):
- try:
- float(num)
- return True
- except ValueError:
- return False
- def string_to_dict(self,str):
- equ = str.rfind('=')
- assignment = str[equ+1:]
- item = str[:equ]
- commands = list()
- itemType = ""
- while item.find('*')>=0:
- star=star = item.find('*')
- cmd=item[:star]
- commands.append(cmd)
- item=item[star+1:]
- for cmd in commands:
- if cmd == "dimmer":
- itemType = "dimmer"
- elif cmd== "number":
- itemType = "number"
- elif cmd== "mqtt":
- itemType = "mqtt"
- if not itemType:
- if item.find('/')>=0:
- itemType = "mqtt"
- if not itemType:
- if self.isfloat(assignment):
- itemType = "number"
- if not itemType:
- if assignment=="on" or assignment=="off":
- itemType = "switch"
- if itemType == "mqtt":
- delay = "false" if "nodelay" in commands else "true"
- retain = "true" if "retain" in commands else "false"
- return { "item": item, "state": assignment, "type": itemType, "delay": delay, "retain": retain }
- #self.log.info(f"itemtype: '{itemtype}' item: '{item}' assignment: '{assignment}'")
- return { "item": item, "state": assignment, "type": itemType }
- def run(self):
- if hasattr(self, "log"):
- self.log.info(f'---------- action {self.action_idx} part {self.part_idx} ----------')
- cur = self.actions[self.action_idx][self.part_idx]
- if str(type(cur)) == "<class 'str'>":
- cur = self.string_to_dict(cur)
- if hasattr(self, "log"):
- self.log.info(f' cur: {cur}')
- if cur["type"]=="switch":
- item=SwitchItem.get_item(cur["item"])
- desired = cur["state"]=="on"
- if item.is_on() != desired:
- self.needs_delay = 1
- self.rule.run.soon(item.oh_send_command,'ON' if desired else 'OFF')
- elif cur["type"]=="dimmer":
- item=DimmerItem.get_item(cur["item"])
- item.oh_send_command(cur["state"])
- elif cur["type"]=="number":
- item=NumberItem.get_item(cur["item"])
- item.oh_send_command(cur["state"])
- elif cur["type"]=="mqtt":
- if "delay" in cur:
- if cur["delay"]=="true":
- self.needs_delay = 1
- retain = False
- if "retain" in cur:
- if cur["retain"]=="true":
- retain = True
- self.rule.mqtt.publish(cur["item"], cur["state"], 1, retain)
- #log.info(f' cur: {cur}')
- #log.info(f' cur: {cur["item"]}')
- if self.part_idx < len(self.actions[self.action_idx])-1:
- self.part_idx += 1
- self.rule.run.soon(self.run)
- return
- else:
- self.part_idx = 0
- self.action_idx += 1
- use_delay = self.delayTime if self.needs_delay else 0
- self.needs_delay = 0
- if self.action_idx < len(self.actions):
- self.rule.run.at(use_delay,self.run)
- else:
- return
Advertisement
Add Comment
Please, Sign In to add comment