115 lines
4.7 KiB
Python
115 lines
4.7 KiB
Python
from appdaemon.entity import Entity
|
|
from appdaemon.plugins.hass.hassapi import Hass
|
|
|
|
class TapDial(Hass):
|
|
active_entity: Entity
|
|
|
|
def initialize(self):
|
|
self.tap_dial.listen_state(self.handle_state_change)
|
|
self.log(f'0.0 Tap Dial entity: {self.tap_dial.entity_id}')
|
|
|
|
@property
|
|
def tap_dial(self) -> Entity:
|
|
return self.get_entity(self.args['entity'])
|
|
|
|
@property
|
|
def step_size(self) -> int:
|
|
return int(self.args.get('step_size', 25))
|
|
|
|
@property
|
|
def init_brightness(self) -> int:
|
|
return int(self.args.get('init_brightness', 200))
|
|
|
|
@property
|
|
def max_brightness(self) -> int:
|
|
return int(self.args.get('max_brightness', 254))
|
|
|
|
@property
|
|
def active_entity_is_on(self) -> bool:
|
|
return self.active_entity.get_state() == 'on'
|
|
|
|
@property
|
|
def current_brightness(self) -> int:
|
|
return self.active_entity.attributes['brightness']
|
|
|
|
def handle_state_change(self, entity: str, attribute: str, old: str, new: str, **kwargs):
|
|
self.log(f'0.1 {new}')
|
|
|
|
# Dial actions
|
|
if new.startswith('dial_rotate') and self.active_entity_is_on:
|
|
_, _, dir, speed = new.split('_', 4)
|
|
self.log(f'1.1 Direction {dir} speed {speed}')
|
|
match speed:
|
|
case 'step':
|
|
rate = self.step_size
|
|
case 'slow':
|
|
rate = 2 * self.step_size
|
|
case 'fast':
|
|
rate = 3 * self.step_size
|
|
val = self.active_entity.attributes['brightness']
|
|
self.log(f'1.2 Brightness value = {val} Change rate = {rate}')
|
|
if dir == 'right':
|
|
val += rate
|
|
self.active_entity.turn_on(brightness=val)
|
|
self.log(f'1.2.1 Brightness value = {val}')
|
|
else:
|
|
val -= rate
|
|
self.active_entity.turn_on(brightness=val)
|
|
self.log(f'1.2.2 Brightness value = {val}')
|
|
|
|
# Button actions
|
|
elif new.endswith('release'):
|
|
_, n, typ, _ = new.split('_', 4)
|
|
# type will be either press or hold
|
|
self.log(f'2.1 Button {n} {typ}')
|
|
|
|
if eid := self.args.get(f'button{n}'):
|
|
self.active_entity = self.get_entity(eid)
|
|
self.log(f'2.1.1 Set active entity to: {self.active_entity.name}')
|
|
|
|
domain, entity = eid.split('.')
|
|
match domain:
|
|
case 'light':
|
|
# Set the light to maximum brightness if the button is held.
|
|
if typ == 'hold':
|
|
self.active_entity.turn_on(brightness=self.max_brightness)
|
|
self.log(f'2.1.1 Set {self.active_entity.friendly_name} to maximum brightness {self.max_brightness}')
|
|
else:
|
|
if self.active_entity_is_on:
|
|
self.active_entity.turn_off()
|
|
self.log(f'2.1.2 Turn {self.active_entity.friendly_name} off')
|
|
else:
|
|
self.active_entity.turn_on(brightness=self.init_brightness)
|
|
self.log(f'2.1.3 Turn {self.active_entity.friendly_name} on with brightness {self.init_brightness}')
|
|
case 'switch':
|
|
self.active_entity.toggle()
|
|
self.log(f'2.1.4 Toggle on/off power to {self.active_entity.friendly_name}')
|
|
|
|
elif switch := self.args.get(f'button{n}'):
|
|
self.active_entity = self.get_entity(switch)
|
|
self.log(f'2.2 Set active entity to: {self.active_entity.friendly_name}')
|
|
onoff = self.active_entity.get_state()
|
|
self.log(f'2.2 {self.active_entity.friendly_name} is currently {onoff}')
|
|
self.active_entity.toggle()
|
|
self.log(f'2.2 Toggle on/off power to {self.active_entity.friendly_name}')
|
|
|
|
# This function was written by chatgpt!
|
|
def hex_to_rgb(self, hex_color: str) -> list:
|
|
|
|
# Remove the "#" if it's included
|
|
hex_color = hex_color.lstrip('#')
|
|
|
|
# Check if the string has a valid length
|
|
if len(hex_color) != 6:
|
|
raise ValueError(f"Invalid hex color: {hex_color}. Must be 6 characters long.")
|
|
|
|
try:
|
|
# Split the hex color into its RGB components and convert to integers
|
|
r = int(hex_color[0:2], 16)
|
|
g = int(hex_color[2:4], 16)
|
|
b = int(hex_color[4:6], 16)
|
|
return [r, g, b]
|
|
except ValueError:
|
|
raise ValueError(f"Invalid hex color: {hex_color}. Must contain only valid hex digits.")
|
|
|