Guest User

inovelli.py

a guest
Jun 6th, 2020
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. # https://community.inovelli.com/t/home-assistant-2nd-gen-switch-rgb-working
  2. # https://nathanfiscus.github.io/inovelli-notification-calc
  3. import appdaemon.plugins.hass.hassapi as hass
  4.  
  5. press_count = {
  6.   7680: 1,
  7.   7860: 2,
  8.   7920: 3,
  9.   7980: 4,
  10.   8040: 5
  11. }
  12.  
  13. hold_data = 7800
  14. release_data = 7740
  15.  
  16. buttons = {
  17.   1: 'down',
  18.   2: 'up',
  19.   3: 'config'
  20. }
  21.  
  22. class InovelliDimmer(hass.Hass):
  23.   def initialize(self):
  24.     self.listen_event(self.handler, 'zwave.scene_activated', node_id = self.args['node_id'])
  25.     self.light = self.args['light']
  26.     self.dimming_period = self.args.get('dimming_period',  1)
  27.     # Scaled to a range of 0-255
  28.     self.dimming_pct = int(self.args.get('dimming_pct', 20)) * 2.55
  29.     self.button_held = {}
  30.     for button in buttons:
  31.       self.button_held[buttons[button]] = False
  32.  
  33.   def handler(self, event, data, kwargs):
  34.     scene_data = data['scene_data']
  35.     button = buttons[data['scene_id']]
  36.     if scene_data == hold_data:
  37.       self.handle_hold(button)
  38.     elif scene_data == release_data:
  39.       self.handle_release(button)
  40.     else:
  41.       self.handle_press(button, press_count[scene_data])
  42.  
  43.   def handle_press(self, button, count=1):
  44.     self.log(f'Button {button} pressed {count} time{"s" if count > 1 else ""}')
  45.  
  46.     if button == 'up' and count == 1:
  47.       self.log(f'Turning on {self.light}')
  48.       self.turn_on(self.light, brightness = 255)
  49.     elif button == 'down' and count == 1:
  50.       self.log(f'Turning off {self.light}')
  51.       self.turn_off(self.light)
  52.  
  53.   def handle_hold(self, button):
  54.     self.log(f'Button {button} held')
  55.     self.button_held[button] = True
  56.     if button == 'up':
  57.       self.loop_brighten(button)
  58.     elif button == 'down':
  59.       self.loop_dim(button)
  60.  
  61.   def handle_release(self, button):
  62.     self.log(f'Button {button} released')
  63.     self.button_held[button] = False
  64.  
  65.   def brighten(self):
  66.     current_brightness = self.get_state(self.light, attribute="brightness") or 0
  67.     self.log(current_brightness)
  68.     self.log(self.dimming_pct)
  69.     new_brightness = min(255, current_brightness + self.dimming_pct)
  70.     self.log(f'Brightening {self.light} from {current_brightness} to {new_brightness}')
  71.     if current_brightness < new_brightness:
  72.       self.turn_on(self.light, brightness = new_brightness)
  73.  
  74.   def dim(self):
  75.     current_brightness = self.get_state(self.light, attribute="brightness") or 0
  76.     new_brightness = max(0, current_brightness - self.dimming_pct)
  77.     self.log(f'Dimming {self.light} from {current_brightness} to {new_brightness}')
  78.     if current_brightness > new_brightness:
  79.       self.turn_on(self.light, brightness = new_brightness)
  80.  
  81.   def loop_times(self, button, fn, ticks_remaining=20):
  82.     if ticks_remaining == 0 or not self.button_held[button]:
  83.       return
  84.     else:
  85.       fn()
  86.       self.run_in(lambda _: self.loop_times(button, fn, ticks_remaining - 1), self.dimming_period)
  87.  
  88.   def loop_brighten(self, button):
  89.     return self.loop_times(button, self.brighten)
  90.  
  91.   def loop_dim(self, button):
  92.     return self.loop_times(button, self.dim)
Add Comment
Please, Sign In to add comment