Refine tap_dial to include code for lights and switches. Add additional options to apps.yaml
Co-authored-by: John Lancaster <jsl12@users.noreply.github.com>
This commit is contained in:
@@ -2,20 +2,39 @@ hello_world:
|
||||
module: hello
|
||||
class: HelloWorld
|
||||
|
||||
my_tap_dial:
|
||||
ManCaveLights:
|
||||
module: tap_dial
|
||||
class: TapDial
|
||||
entity: sensor.tap_dial_action
|
||||
entity: sensor.tapdial01_action
|
||||
init_brightness: 200
|
||||
max_brightness: 254
|
||||
step_size: 25
|
||||
button1: light.stick_lamp
|
||||
button2: light.wet_bar
|
||||
button3: light.monkey_lamp
|
||||
button4: light.kitchen_candlelabra
|
||||
# button4: light.kitchen_candlelabra
|
||||
# device: tapdial01
|
||||
|
||||
LivingRoomLights:
|
||||
module: tap_dial
|
||||
class: TapDial
|
||||
entity: sensor.tapdial02_action
|
||||
init_brightness: 200
|
||||
max_brightness: 254
|
||||
step_size: 25
|
||||
button1: light.living_room_lamps
|
||||
button2: light.living_room_floodlights
|
||||
button3: light.living_room_spot
|
||||
button4: switch.christmas_tree
|
||||
|
||||
piano_button:
|
||||
button02:
|
||||
module: button_switch
|
||||
class: AqaraSwitch
|
||||
button: sensor.button02_action
|
||||
switch: switch.keyboards
|
||||
# action: double
|
||||
|
||||
my_timer:
|
||||
module: timer
|
||||
class: TimerClass
|
||||
on_time: '18:00:00'
|
||||
light: light.wet_bar
|
||||
|
||||
@@ -1,47 +1,75 @@
|
||||
import re
|
||||
|
||||
#from appdaemon.adapi import ADAPI
|
||||
from appdaemon.entity import Entity
|
||||
from appdaemon.plugins.hass.hassapi import Hass
|
||||
|
||||
|
||||
BUTTON_PRESS = re.compile(r'button_(\d)_press_release')
|
||||
|
||||
|
||||
|
||||
class TapDial(Hass):
|
||||
active_entity: Entity
|
||||
|
||||
def initialize(self):
|
||||
self.log(f"Action entity: {self.tap_dial_entity.friendly_name}")
|
||||
self.tap_dial_entity.listen_state(self.my_callback)
|
||||
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_entity(self) -> Entity:
|
||||
return self.get_entity(self.args["entity"])
|
||||
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))
|
||||
return int(self.args.get('step_size', 25))
|
||||
|
||||
def my_callback(self, entity: str, attribute: str, old: str, new: str, **kwargs):
|
||||
# self.log(f'{new}')
|
||||
if new:
|
||||
if m := BUTTON_PRESS.match(new):
|
||||
pressed_button = int(m.group(1))
|
||||
self.log(f'Pressed button {pressed_button}')
|
||||
@property
|
||||
def init_brightness(self) -> int:
|
||||
return int(self.args.get('init_brightness', 200))
|
||||
|
||||
if associated_light := self.args.get(f'button{pressed_button}'):
|
||||
light_entity = self.get_entity(associated_light)
|
||||
self.active_entity = light_entity
|
||||
self.log(f'Setting active entity to {self.active_entity.friendly_name}')
|
||||
@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'
|
||||
|
||||
def handle_state_change(self, entity: str, attribute: str, old: str, new: str, **kwargs):
|
||||
self.log(f'0.0 {new}')
|
||||
|
||||
# Dial actions
|
||||
if new.startswith('dial_rotate'):
|
||||
dir = 'left' if new.find('left') != -1 else 'right'
|
||||
self.log(f'1.1 Dial {dir}')
|
||||
# More code goes here....
|
||||
|
||||
# Button actions
|
||||
elif new.endswith('release'):
|
||||
_, n, typ, _ = new.split('_', 4)
|
||||
# type will be either press or hold
|
||||
self.log(f'1.2 Button {n} {typ}')
|
||||
|
||||
if eid := self.args.get(f'button{n}'):
|
||||
self.active_entity = self.get_entity(eid)
|
||||
self.log(f'2.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'3.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'4.1 Turn {self.active_entity.friendly_name} off')
|
||||
else:
|
||||
self.active_entity.turn_on(brightness=self.init_brightness)
|
||||
self.log(f'4.2 Turn {self.active_entity.friendly_name} on with brightness {self.init_brightness}')
|
||||
case 'switch':
|
||||
self.active_entity.toggle()
|
||||
self.log(f'2.2 Toggle on/off power to {self.active_entity.friendly_name}')
|
||||
|
||||
match new:
|
||||
case "dial_rotate_right_step":
|
||||
b = self.active_entity.attributes['brightness']
|
||||
self.active_entity.turn_on(brightness=b+self.step_size)
|
||||
case "dial_rotate_left_step":
|
||||
b = self.active_entity.attributes['brightness']
|
||||
self.active_entity.turn_on(brightness=b-self.step_size)
|
||||
# case _:
|
||||
# self.log(f'Unhandled action: {new}')
|
||||
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}')
|
||||
|
||||
Reference in New Issue
Block a user