Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import appdaemon.plugins.hass.hassapi as hass
- import math
- #
- # Maps HUE lights to the KNX bus
- #
- # Args:
- #
- class Hue2Knx(hass.Hass):
- DIM_STEP_SIZE = 25
- DIM_DELAY = 0.2
- BRIGHTNESS_MAX = 255
- BRIGHTNESS_MIN = 0
- KNX_DIMM_UP = 9
- KNX_DIMM_DOWN = 1
- KNX_DIMM_END = 0
- def initialize(self):
- self._brightness = 0
- self._state = ""
- self._color = 0
- self._isDimming = 0
- self._stateListener = self.listen_state(self.light_changed, entity = self.args["entity"], attribue = "all")
- self._eventListener = self.listen_event(self.event_triggered, "knx_event")
- def terminate(self):
- self.cancel_listen_state(self._stateListener)
- self.cancel_listen_event(self._eventListener)
- def light_changed(self, entity, attribute, old, new, kwargs):
- states = self.get_state(self.args["entity"], attribute="all")["attributes"]
- if (new != self._state):
- self._state = new
- state = 1 if new == "on" else 0
- self.call_service("knx/send", address=self.args["stateAddress"], payload=state)
- # KNX switches that can dimm need a brightness of 0 in the "off" state to correctly show the lights state
- if (state == 0):
- states["brightness"] = self.BRIGHTNESS_MIN
- if ("brightnessAddress" in self.args and "brightness" in states):
- brightness = int(states["brightness"])
- if (self._brightness != brightness):
- self._brightness = brightness
- self.call_service("knx/send", address=self.args["brightnessAddress"], payload=[brightness])
- if ("colorAddress" in self.args and "color_temp" in states):
- color = int(states["color_temp"])
- if (self._color != color):
- self._color = color
- self.call_service("knx/send", address=self.args["colorAddress"], payload=[color])
- def event_triggered(self, event, payload, kwargs):
- value = payload["data"]
- address = payload["address"]
- # handle button presses and toogle the light
- if ("toggleAddress" in self.args and address == self.args["toggleAddress"]):
- mode = "turn_on" if int(value) == 1 else "turn_off"
- self.call_service("light/" + mode, entity_id=self.args["entity"])
- # handle dimming of the lights
- elif ("dimmAddress" in self.args and address == self.args["dimmAddress"]):
- value = int(value)
- if value:
- if (self._brightness == self.BRIGHTNESS_MAX and value == self.KNX_DIMM_UP):
- return
- elif (self._brightness == self.BRIGHTNESS_MIN and value == self.KNX_DIMM_DOWN):
- return
- self._isDimming = 1
- dimDirection = 1 if value == self.KNX_DIMM_UP else -1
- step = self.DIM_STEP_SIZE * dimDirection
- self.dim_light({"step": step})
- else:
- self._isDimming = 0
- def dim_light(self, kwargs):
- if (self._isDimming):
- brightness = min( max(self.BRIGHTNESS_MIN, self._brightness + kwargs["step"]), self.BRIGHTNESS_MAX)
- self.set_light_properties({"brightness":brightness})
- self.run_in(self.dim_light, self.DIM_DELAY, **kwargs)
- def set_light_properties(self, kwargs):
- kwargs["entity_id"] = self.args["entity"]
- self.call_service("light/turn_on", **kwargs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement