Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://community.inovelli.com/t/home-assistant-2nd-gen-switch-rgb-working
- # https://nathanfiscus.github.io/inovelli-notification-calc
- import appdaemon.plugins.hass.hassapi as hass
- press_count = {
- 7680: 1,
- 7860: 2,
- 7920: 3,
- 7980: 4,
- 8040: 5
- }
- hold_data = 7800
- release_data = 7740
- buttons = {
- 1: 'down',
- 2: 'up',
- 3: 'config'
- }
- class InovelliDimmer(hass.Hass):
- def initialize(self):
- self.listen_event(self.handler, 'zwave.scene_activated', node_id = self.args['node_id'])
- self.light = self.args['light']
- self.dimming_period = self.args.get('dimming_period', 1)
- # Scaled to a range of 0-255
- self.dimming_pct = int(self.args.get('dimming_pct', 20)) * 2.55
- self.button_held = {}
- for button in buttons:
- self.button_held[buttons[button]] = False
- def handler(self, event, data, kwargs):
- scene_data = data['scene_data']
- button = buttons[data['scene_id']]
- if scene_data == hold_data:
- self.handle_hold(button)
- elif scene_data == release_data:
- self.handle_release(button)
- else:
- self.handle_press(button, press_count[scene_data])
- def handle_press(self, button, count=1):
- self.log(f'Button {button} pressed {count} time{"s" if count > 1 else ""}')
- if button == 'up' and count == 1:
- self.log(f'Turning on {self.light}')
- self.turn_on(self.light, brightness = 255)
- elif button == 'down' and count == 1:
- self.log(f'Turning off {self.light}')
- self.turn_off(self.light)
- def handle_hold(self, button):
- self.log(f'Button {button} held')
- self.button_held[button] = True
- if button == 'up':
- self.loop_brighten(button)
- elif button == 'down':
- self.loop_dim(button)
- def handle_release(self, button):
- self.log(f'Button {button} released')
- self.button_held[button] = False
- def brighten(self):
- current_brightness = self.get_state(self.light, attribute="brightness") or 0
- self.log(current_brightness)
- self.log(self.dimming_pct)
- new_brightness = min(255, current_brightness + self.dimming_pct)
- self.log(f'Brightening {self.light} from {current_brightness} to {new_brightness}')
- if current_brightness < new_brightness:
- self.turn_on(self.light, brightness = new_brightness)
- def dim(self):
- current_brightness = self.get_state(self.light, attribute="brightness") or 0
- new_brightness = max(0, current_brightness - self.dimming_pct)
- self.log(f'Dimming {self.light} from {current_brightness} to {new_brightness}')
- if current_brightness > new_brightness:
- self.turn_on(self.light, brightness = new_brightness)
- def loop_times(self, button, fn, ticks_remaining=20):
- if ticks_remaining == 0 or not self.button_held[button]:
- return
- else:
- fn()
- self.run_in(lambda _: self.loop_times(button, fn, ticks_remaining - 1), self.dimming_period)
- def loop_brighten(self, button):
- return self.loop_times(button, self.brighten)
- def loop_dim(self, button):
- return self.loop_times(button, self.dim)
Add Comment
Please, Sign In to add comment