-
-
Save tal/b36f963c2b04bae52a1c1a4889592eee to your computer and use it in GitHub Desktop.
pyscript for fading luton lights in hassocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@service | |
def fade_light(entity_id=None, ending_brightness=None, minutes=3, notify_after=None): | |
light_state = state.get(entity_id) | |
if state.getattr(entity_id) is None: | |
log.error(f"No entity found for {entity_id}") | |
return | |
transition_secs = minutes * 60 | |
def get_brightness(): | |
nonlocal entity_id | |
attrs = state.getattr(entity_id) | |
return attrs.get("brightness", 0) | |
if ending_brightness is None: | |
log.error(f"No ending_brightness found for {ending_brightness}") | |
return | |
task.unique(f"fade_light|{entity_id}") | |
starting_brightness = get_brightness() | |
light_diff = ending_brightness - starting_brightness | |
if light_diff == 0: | |
log.info(f"light is faded to same brightness it started with") | |
return | |
target_tick_count = light_diff / 2 | |
delay_per_tick = max((abs(transition_secs / target_tick_count), 1)) | |
tick_count = transition_secs / delay_per_tick | |
change_per_tick = light_diff / tick_count | |
ending_brightness = ending_brightness | |
def hasnt_met_target(): | |
current = get_brightness() | |
should = False | |
if light_diff > 0: # it's going up | |
should = current < ending_brightness | |
else: # it's going down | |
should = current > ending_brightness | |
log.debug(f"current={current} ending_brightness={ending_brightness} light_diff={light_diff} should={should}") | |
return should | |
log.debug(f"running: entity_id={entity_id} light_diff={light_diff} ending_brightness={ending_brightness} delay_per_tick={delay_per_tick}") | |
expected_brightness = starting_brightness | |
while hasnt_met_target(): | |
current_brightness = get_brightness() | |
log.debug(f"current_brightness={current_brightness} expected_brightness={expected_brightness}") | |
if abs(current_brightness - expected_brightness) > 4: | |
log.debug(f"Expected brightness didn't match current brightness ex:{expected_brightness} cu:{current_brightness}") | |
break | |
new_brightness = expected_brightness + change_per_tick | |
log.debug(f"new_brightness={new_brightness} change_per_tick={change_per_tick}") | |
if new_brightness > 0: | |
brightness_to_set = round(new_brightness) | |
light.turn_on(entity_id=entity_id, brightness=brightness_to_set) | |
else: | |
light.turn_off(entity_id=entity_id) | |
expected_brightness = new_brightness | |
task.sleep(delay_per_tick) | |
if notify_after is not None: | |
brightness = get_brightness() | |
pct = int(brightness*100/255) | |
attrs2 = state.getattr(entity_id) | |
name = attrs2.get('friendly_name', entity_id) | |
message = f"{pct}% {name} Fade Complete" | |
service.call("notify", f"mobile_app_{notify_after}", message=message) |
This is a great script that helped me a lot !
Is there a way of changing it so the transition steps is always 1 or the minimal possible so becomes smother the fade?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Trying to get this working in Hassio, saw that you posted it in an HA forum.
Not sure if you have any tips on how to get it working?