Save unsaved work

This commit is contained in:
Jim Lancaster
2025-10-23 07:05:05 -05:00
parent be632795a9
commit 9c620b7585
5 changed files with 195 additions and 38 deletions

View File

@@ -9,6 +9,7 @@ ManCaveLights:
init_brightness: 200
max_brightness: 254
step_size: 25
color: antique_white
button1: light.stick_lamp
button2: light.wet_bar
button3: light.monkey_lamp

View File

@@ -24,6 +24,15 @@ class TapDial(Hass):
def max_brightness(self) -> int:
return int(self.args.get('max_brightness', 254))
# The active_entity_is_on property fails if no button has been pushed and the dial is rotated with the error:
# File "/conf/apps/tap_dial.py", line 59, in handle_state_change
# if new.startswith('dial_rotate') and self.active_entity_is_on:
# ^^^^^^^^^^^^^^^^^^^^^^^^
# File "/conf/apps/tap_dial.py", line 29, in active_entity_is_on
# return self.active_entity.get_state() == 'on'
# ^^^^^^^^^^^^^^^^^^
# AttributeError: 'TapDial' object has no attribute 'active_entity'. Did you mean: 'remove_entity'?
@property
def active_entity_is_on(self) -> bool:
return self.active_entity.get_state() == 'on'
@@ -67,61 +76,64 @@ class TapDial(Hass):
case 'fast':
rate = 3 * self.step_size
val = self.active_entity.attributes['brightness']
self.log(f'070 Brightness value = {val} Change rate = {rate}')
if dir == 'right':
if val < self.max_brightness:
if (val + rate) > self.max_brightness:
val = self.max_brightness
else:
val += rate
self.active_entity.turn_on(brightness=val)
self.log(f'078 Brightness value = {val}')
if val != 'null':
self.log(f'071 Brightness value = {val} Change rate = {rate}')
if dir == 'right':
if val < self.max_brightness:
if (val + rate) > self.max_brightness:
val = self.max_brightness
else:
val += rate
self.active_entity.turn_on(brightness=val)
self.log(f'079 Brightness value = {val}')
else:
if val > 0:
if (val - rate) < 0:
val = 0
else:
val -= rate
self.active_entity.turn_on(brightness=val)
self.log(f'087 Brightness value = {val}')
else:
if val > 0:
if (val - rate) < 0:
val = 0
else:
val -= rate
self.active_entity.turn_on(brightness=val)
self.log(f'086 Brightness value = {val}')
self.log(f'089 WARNING: Brightness value = {val}')
# Button actions
elif new.endswith('release'):
_, n, typ, _ = new.split('_', 4)
# type will be either press or hold
self.log(f'092 Button {n} {typ}')
self.log(f'104 Button {n} {typ}')
if eid := self.args.get(f'button{n}'):
self.active_entity = self.get_entity(eid)
self.log(f'096 Set active entity to: {self.active_entity.name}')
self.log(json.dumps(self.active_entity.get_state('all'), indent=4))
self.log(f'108 Set active entity to: {self.active_entity.name}')
# self.log(json.dumps(self.active_entity.get_state('all'), indent=4))
domain, entity = eid.split('.')
self.log(f'099 Curent domain: {domain}, entity: {entity}')
self.log(f'112 Curent domain: {domain}, entity: {entity}')
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'105 Set {self.active_entity.friendly_name} to maximum brightness {self.max_brightness}')
self.log(f'118 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'109 Turn off {self.active_entity.friendly_name}')
self.log(f'122 Turn off {self.active_entity.friendly_name}')
else:
self.active_entity.turn_on(brightness=self.init_brightness)
self.log(f'112 Turn on {self.active_entity.friendly_name} with brightness {self.init_brightness}')
self.log(f'125 Turn on {self.active_entity.friendly_name} with brightness {self.init_brightness}')
case 'switch':
self.active_entity.toggle()
self.log(f'115 Toggle on/off power to {self.active_entity.friendly_name}')
self.log(f'128 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'119 Set active entity to: {self.active_entity.friendly_name}')
self.log(f'132 Set active entity to: {self.active_entity.friendly_name}')
onoff = self.active_entity.get_state()
self.log(f'114 {self.active_entity.friendly_name} is currently {onoff}')
self.log(f'134 {self.active_entity.friendly_name} is currently {onoff}')
self.active_entity.toggle()
self.log(f'116 Toggle on/off power to {self.active_entity.friendly_name}')
self.log(f'136 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:
@@ -131,7 +143,7 @@ class TapDial(Hass):
# Check if the string has a valid length
if len(hex_color) != 6:
raise ValueError(f"126 Invalid hex color: {hex_color}. Must be 6 characters long.")
raise ValueError(f"146 Invalid hex color: {hex_color}. Must be 6 characters long.")
try:
# Split the hex color into its RGB components and convert to integers
@@ -140,5 +152,5 @@ class TapDial(Hass):
b = int(hex_color[4:6], 16)
return [r, g, b]
except ValueError:
raise ValueError(f"135 Invalid hex color: {hex_color}. Must contain only valid hex digits.")
raise ValueError(f"155 Invalid hex color: {hex_color}. Must contain only valid hex digits.")