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:
Jim Lancaster
2025-01-08 18:22:03 -06:00
parent 6038d0d405
commit e31b2ad4f3
2 changed files with 86 additions and 39 deletions

View File

@@ -2,20 +2,39 @@ hello_world:
module: hello module: hello
class: HelloWorld class: HelloWorld
my_tap_dial: ManCaveLights:
module: tap_dial module: tap_dial
class: TapDial class: TapDial
entity: sensor.tap_dial_action entity: sensor.tapdial01_action
init_brightness: 200
max_brightness: 254
step_size: 25 step_size: 25
button1: light.stick_lamp button1: light.stick_lamp
button2: light.wet_bar button2: light.wet_bar
button3: light.monkey_lamp 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 module: button_switch
class: AqaraSwitch class: AqaraSwitch
button: sensor.button02_action button: sensor.button02_action
switch: switch.keyboards switch: switch.keyboards
# action: double
my_timer:
module: timer
class: TimerClass
on_time: '18:00:00'
light: light.wet_bar

View File

@@ -1,47 +1,75 @@
import re #from appdaemon.adapi import ADAPI
from appdaemon.entity import Entity from appdaemon.entity import Entity
from appdaemon.plugins.hass.hassapi import Hass from appdaemon.plugins.hass.hassapi import Hass
BUTTON_PRESS = re.compile(r'button_(\d)_press_release')
class TapDial(Hass): class TapDial(Hass):
active_entity: Entity active_entity: Entity
def initialize(self): def initialize(self):
self.log(f"Action entity: {self.tap_dial_entity.friendly_name}") self.tap_dial.listen_state(self.handle_state_change)
self.tap_dial_entity.listen_state(self.my_callback) self.log(f'0.0 Tap Dial entity: {self.tap_dial.entity_id}')
@property @property
def tap_dial_entity(self) -> Entity: def tap_dial(self) -> Entity:
return self.get_entity(self.args["entity"]) return self.get_entity(self.args['entity'])
@property @property
def step_size(self) -> int: 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): @property
# self.log(f'{new}') def init_brightness(self) -> int:
if new: return int(self.args.get('init_brightness', 200))
if m := BUTTON_PRESS.match(new):
pressed_button = int(m.group(1))
self.log(f'Pressed button {pressed_button}')
if associated_light := self.args.get(f'button{pressed_button}'): @property
light_entity = self.get_entity(associated_light) def max_brightness(self) -> int:
self.active_entity = light_entity return int(self.args.get('max_brightness', 254))
self.log(f'Setting active entity to {self.active_entity.friendly_name}')
@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.active_entity.toggle()
self.log(f'2.2 Toggle on/off power to {self.active_entity.friendly_name}')
match new: elif switch := self.args.get(f'button{n}'):
case "dial_rotate_right_step": self.active_entity = self.get_entity(switch)
b = self.active_entity.attributes['brightness'] self.log(f'2.2 Set active entity to: {self.active_entity.friendly_name}')
self.active_entity.turn_on(brightness=b+self.step_size) onoff = self.active_entity.get_state()
case "dial_rotate_left_step": self.log(f'2.2 {self.active_entity.friendly_name} is currently {onoff}')
b = self.active_entity.attributes['brightness'] self.active_entity.toggle()
self.active_entity.turn_on(brightness=b-self.step_size) self.log(f'2.2 Toggle on/off power to {self.active_entity.friendly_name}')
# case _:
# self.log(f'Unhandled action: {new}')