SHARE
TWEET

Untitled

a guest Dec 18th, 2018 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import appdaemon.plugins.hass.hassapi as hass
  2. import math
  3.  
  4. #
  5. # Maps HUE lights to the KNX bus
  6. #
  7. # Args:
  8. #
  9.  
  10. class Hue2Knx(hass.Hass):
  11.  
  12.     DIM_STEP_SIZE = 25
  13.     DIM_DELAY = 0.2
  14.     BRIGHTNESS_MAX = 255
  15.     BRIGHTNESS_MIN = 0
  16.     KNX_DIMM_UP = 9
  17.     KNX_DIMM_DOWN = 1
  18.     KNX_DIMM_END = 0
  19.  
  20.     def initialize(self):
  21.         self._brightness = 0
  22.         self._state = ""
  23.         self._color = 0
  24.         self._isDimming = 0
  25.  
  26.         self._stateListener = self.listen_state(self.light_changed, entity = self.args["entity"], attribue = "all")
  27.         self._eventListener = self.listen_event(self.event_triggered, "knx_event")
  28.  
  29.     def terminate(self):
  30.         self.cancel_listen_state(self._stateListener)
  31.         self.cancel_listen_event(self._eventListener)
  32.        
  33.     def light_changed(self, entity, attribute, old, new, kwargs):
  34.         states = self.get_state(self.args["entity"], attribute="all")["attributes"]
  35.  
  36.         if (new != self._state):
  37.             self._state = new
  38.             state = 1 if new == "on" else 0
  39.             self.call_service("knx/send", address=self.args["stateAddress"], payload=state)
  40.             # KNX switches that can dimm need a brightness of 0 in the "off" state to correctly show the lights state
  41.             if (state == 0):
  42.                 states["brightness"] = self.BRIGHTNESS_MIN
  43.  
  44.         if ("brightnessAddress" in self.args and "brightness" in states):
  45.             brightness = int(states["brightness"])
  46.             if (self._brightness != brightness):
  47.                 self._brightness = brightness
  48.                 self.call_service("knx/send", address=self.args["brightnessAddress"], payload=[brightness])
  49.  
  50.         if ("colorAddress" in self.args and "color_temp" in states):
  51.             color = int(states["color_temp"])
  52.             if (self._color != color):
  53.                 self._color = color
  54.                 self.call_service("knx/send", address=self.args["colorAddress"], payload=[color])
  55.    
  56.     def event_triggered(self, event, payload, kwargs):
  57.         value = payload["data"]
  58.         address = payload["address"]
  59.         # handle button presses and toogle the light
  60.         if ("toggleAddress" in self.args and address == self.args["toggleAddress"]):
  61.             mode = "turn_on" if int(value) == 1 else "turn_off"
  62.             self.call_service("light/" + mode, entity_id=self.args["entity"])
  63.         # handle dimming of the lights
  64.         elif ("dimmAddress" in self.args and address == self.args["dimmAddress"]):
  65.             value = int(value)
  66.             if value:
  67.                 if (self._brightness == self.BRIGHTNESS_MAX and value == self.KNX_DIMM_UP):
  68.                     return
  69.                 elif (self._brightness == self.BRIGHTNESS_MIN and value == self.KNX_DIMM_DOWN):
  70.                     return
  71.  
  72.                 self._isDimming = 1
  73.  
  74.                 dimDirection = 1 if value == self.KNX_DIMM_UP else -1
  75.                 step = self.DIM_STEP_SIZE * dimDirection
  76.                 self.dim_light({"step": step})
  77.                
  78.             else:
  79.                 self._isDimming = 0
  80.  
  81.     def dim_light(self, kwargs):
  82.         if (self._isDimming):
  83.             brightness = min( max(self.BRIGHTNESS_MIN, self._brightness + kwargs["step"]), self.BRIGHTNESS_MAX)
  84.             self.set_light_properties({"brightness":brightness})
  85.             self.run_in(self.dim_light, self.DIM_DELAY, **kwargs)
  86.  
  87.     def set_light_properties(self, kwargs):
  88.         kwargs["entity_id"] = self.args["entity"]
  89.         self.call_service("light/turn_on", **kwargs)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top