Advertisement
proditaki

ComfoFan plugin domoticz

Jan 23rd, 2021 (edited)
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.28 KB | None | 0 0
  1. #Comfofan Controller
  2. #
  3. """
  4. <plugin key="ComfoFanControl" name="Zehnder Comfofan Controller" version="0.0.1">
  5.    <params>
  6.        <param field="Address" label="Server address" width="300px" required="true" default="192.168.2.2"/>
  7.        <param field="Username" label="Username" default="admin" width="300px"/>
  8.        <param field="Password" label="Password" default="nrf905" width="300px"/>
  9.    </params>
  10. </plugin>
  11. """
  12.  
  13. import Domoticz
  14. #import json
  15. from requests import get
  16. from requests import post
  17.  
  18.  
  19. class BasePlugin:
  20. #heartbeat of more thsn 30 secs gives error in domoticz log
  21. #countbeat is the multiplier of 30 seconds so in this case 900 second between each update of the FAN data
  22.     countBeats=30
  23.  
  24.     def __init__(self):
  25.         return
  26.  
  27.     def onStart(self):
  28.         # Enable heartbeat
  29.  
  30.         Domoticz.Log(Parameters["Address"])
  31.         if len(Devices) < 3:
  32.             Domoticz.Log("Missing devices:(")
  33.  
  34.             self.createDevices()
  35.         Domoticz.Heartbeat(30)
  36.  
  37.     def createDevices(self):
  38.         if not (1 in Devices):
  39.             Domoticz.Device(Name="FanSpeed", Unit=1, Type=243, Subtype=19, Image=7, Used=1).Create()
  40.  
  41.         if not (2 in Devices):
  42.             SelectorNames="|Low|Medium|High|Max"
  43.             SelectorOptions="||||"
  44.             Options={"LevelNames":SelectorNames,
  45.                      "LevelActions":SelectorOptions,
  46.                      "LevelOffHidden": "true",
  47.                      "SelectorStyle": "0"}
  48.             Domoticz.Device(Name="Afzuiging Snelheid", Used=1, Unit=2, TypeName="Selector Switch", Switchtype=18, Image=7, Options=Options).Create()
  49.             Domoticz.Log("Devices created.")
  50.  
  51.         if not (3 in Devices):
  52.             SelectorNames="|10min|30min|60min"
  53.             SelectorOptions="|||"
  54.             Options={"LevelNames":SelectorNames,
  55.                      "LevelActions":SelectorOptions,
  56.                      "LevelOffHidden": "true",
  57.                      "SelectorStyle": "0"}
  58.             Domoticz.Device(Name="Afzuiging Timer", Used=1, Unit=3, TypeName="Selector Switch", Switchtype=18, Image=13, Options=Options).Create()
  59.             Domoticz.Log("Devices created.")
  60.  
  61.  
  62.     def onStop(self):
  63.         Domoticz.Log("Stopping")
  64.  
  65.     def onHeartbeat(self):
  66.         self.countBeats -= 1
  67.         if self.countBeats > 0:
  68.             return
  69.         else:
  70.             self.countBeats = 30
  71.         Domoticz.Log("Heartbeating...")
  72.         url = 'http://'+Parameters['Address']+'/api/test/fan/querydevice.json'
  73.         x =  get(url, auth=(Parameters['Username'], Parameters['Password']))
  74. #        Parameters["Username"]
  75. #        data = json.loads(x.text)
  76.         data = x.json()
  77.         devices = data['devices']
  78.         fanID = list(devices.keys())[0] # get the first fanID, i only have one :)
  79.         fanValues = devices[fanID]
  80.  
  81.         Domoticz.Log("ID: {0}".format(fanValues['id']))
  82.         Domoticz.Log("Speed: {0}".format(fanValues['speed']))
  83.         Domoticz.Log("Timer: {0}".format(fanValues['timer']))
  84.         Domoticz.Log("Percentage: {0}".format(fanValues['percentage']))
  85.         Domoticz.Log("Voltage: {0}".format(fanValues['voltage']))
  86.         if (1 in Devices):
  87.             Devices[1].Update(nValue = 0,sValue=fanValues['speed'])
  88. #            Devices[2].Update(nValue = 10)
  89.         if (2 in Devices):
  90.            if fanValues['speed'] == 'low':
  91.                curSpeed = '10'
  92.            elif fanValues['speed'] == 'medium':
  93.                curSpeed = '20'
  94.            elif fanValues['speed'] == 'high':
  95.                curSpeed = '30'
  96.            else:
  97.                curSpeed = '40'
  98.            Devices[2].Update(0, curSpeed)
  99.  
  100.     def onCommand(self, Unit, Command, Level, Hue):
  101. #        Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
  102.         if Unit == 2:
  103.             if  Level == 10:
  104. #                Domoticz.Log("Fan speed set to Low")
  105.                 setSpeed = 'low'
  106.             elif Level == 20:
  107.                 setSpeed = 'medium'
  108.             elif Level == 30:
  109.                 setSpeed = 'high'
  110.             else:
  111.                 setSpeed = "max"
  112.             url = 'http://'+Parameters['Address']+'/api/v2/fan/setspeed.json?speed={0}'.format(setSpeed)
  113.             x =  get(url, auth=(Parameters['Username'], Parameters['Password']))
  114.             Devices[2].Update(0, str(Level))
  115.         elif Unit == 3:
  116.             if Level == 10:
  117.                 setTime = 10
  118.                 Domoticz.Log("10 minuten")
  119.             elif Level == 20:
  120.                 Domoticz.Log("30 minuten")
  121.                 setTime = 30
  122.             else:
  123.                 setTime = 60
  124.                 Domoticz.Log("60 minuten")
  125.             setSpeed = "max"
  126.             url = 'http://'+Parameters['Address']+'/api/v2/fan/setspeed.json?speed={0}&timer={1}'.format(setSpeed, setTime)
  127.             x =  get(url, auth=(Parameters['Username'], Parameters['Password']))
  128.  
  129. #            Domoticz.Log('{0}'.format(Devices[2].sValue))
  130. global _plugin
  131. _plugin = BasePlugin()
  132.  
  133. def onStart():
  134.     global _plugin
  135.     _plugin.onStart()
  136.  
  137. def onStop():
  138.     global _plugin
  139.     _plugin.onStop()
  140.  
  141. def onHeartbeat():
  142.     global _plugin
  143.     _plugin.onHeartbeat()
  144.  
  145. def onCommand(Unit, Command, Level, Hue):
  146.     global _plugin
  147.     _plugin.onCommand(Unit, Command, Level, Hue)
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement