From 026c22a553a3b508607531186ea5b3a94ab1af7a Mon Sep 17 00:00:00 2001 From: Patrick Kissling Date: Mon, 26 Aug 2024 09:06:34 +0200 Subject: [PATCH] Use `light.turn_on` instead of custom pyscript to adjust lights --- automations/alarm.yaml | 14 ---------- automations/lights.yaml | 4 +-- includes/lovelace.yaml | 2 ++ pyscript/lights.py | 57 ----------------------------------------- sensors/lights.yaml | 3 +++ 5 files changed, 7 insertions(+), 73 deletions(-) delete mode 100755 pyscript/lights.py diff --git a/automations/alarm.yaml b/automations/alarm.yaml index 27cd3cc3..81ed1690 100755 --- a/automations/alarm.yaml +++ b/automations/alarm.yaml @@ -17,20 +17,6 @@ action: - service: input_boolean.turn_off entity_id: input_boolean.night_light_mode - - service: light.turn_on - entity_id: light.bedroom - data: - brightness: 1 - - service: adaptive_lighting.apply - entity_id: switch.adaptive_lighting_bedroom - data: - lights: light.bedroom - adapt_brightness: false - turn_on_lights: true - - service: switch.turn_off - entity_id: switch.adaptive_lighting_bedroom - - delay: - seconds: 5 - service: light.turn_on entity_id: light.bedroom data: diff --git a/automations/lights.yaml b/automations/lights.yaml index 842c40a3..7e2ac9f9 100755 --- a/automations/lights.yaml +++ b/automations/lights.yaml @@ -51,9 +51,9 @@ entity_id: sensor.light_bedroom attribute: brightness action: - service: pyscript.adjust_light + service: light.turn_on data: entity_id: "{{ state_attr(trigger.entity_id, 'target_entity_id') }}" brightness: "{{ state_attr(trigger.entity_id, 'brightness') }}" - color_temp_kelvin: "{{ state_attr(trigger.entity_id, 'color_temp_kelvin') }}" + kelvin: "{{ state_attr(trigger.entity_id, 'color_temp_kelvin') }}" transition: "{{ state_attr(trigger.entity_id, 'transition') }}" diff --git a/includes/lovelace.yaml b/includes/lovelace.yaml index 32d657e0..8d853f34 100755 --- a/includes/lovelace.yaml +++ b/includes/lovelace.yaml @@ -7,6 +7,8 @@ resources: type: module - url: /hacsfiles/bar-card/bar-card.js type: module + - url: /hacsfiles/Bubble-Card/bubble-card.js + type: module - url: /hacsfiles/button-card/button-card.js type: module - url: /hacsfiles/compass-card/compass-card.js diff --git a/pyscript/lights.py b/pyscript/lights.py deleted file mode 100755 index a1412d0b..00000000 --- a/pyscript/lights.py +++ /dev/null @@ -1,57 +0,0 @@ -import math - -@service -def adjust_light(entity_id, brightness, transition, color_temp_kelvin=None): - - log.info(f"START adjust_light: entity_id={entity_id}, brightness={brightness}, color_temp_kelvin={color_temp_kelvin}, transition={transition}") - task.unique(entity_id) - - if brightness < 0 or brightness > 255: - log.error(f"ERROR adjust_light: brightness [{brightness}] must be between 0 and 255") - return - - if state.get(entity_id) == 'off' and brightness == 0: - log.info(f"SKIP adjust_light: Light [{entity_id}] is already off") - return - - if state.get(entity_id) == 'on' and state.get(entity_id).brightness == brightness and color_temp_kelvin is not None and state.get(entity_id).color_temp_kelvin == color_temp_kelvin: - log.info(f"SKIP adjust_light: Light [{entity_id}] is already in target state") - return - - if state.get(entity_id) == 'on' and state.get(entity_id).brightness == brightness and color_temp_kelvin is None: - log.info(f"SKIP adjust_light: Light [{entity_id}] is already in target state") - return - - adapt_light(entity_id, brightness, transition, color_temp_kelvin) - - task.sleep(transition + 1) - - retries = 0 - while retries < 10 and light_has_target_state(entity_id, brightness) is False: - transition = max(1, math.floor(transition / 2)) - log.info(f"RETRY {retries} adjust_light: Light [{entity_id}] was supposed to have brightness [{brightness}] but has [{state.get(entity_id).brightness}]") - adapt_light(entity_id, brightness, transition, color_temp_kelvin) - task.sleep(transition + 1) - retries += 1 - - if retries == 10 and light_has_target_state(entity_id, brightness) is False: - log.error(f"RETRY ERROR adjust_light: After 10 retries light [{entity_id}] still was supposed to have brightness [{brightness}] but has [{state.get(entity_id).brightness}]") - - log.info(f"END adjust_light: entity_id={entity_id}, brightness={brightness}, color_temp_kelvin={color_temp_kelvin}, transition={transition}") - -def adapt_light(entity_id, brightness, transition, color_temp_kelvin=None): - if brightness == 0: - light.turn_off(entity_id=entity_id, transition=transition) - log.info(f"PROGRESS adjust_light: light.turn_off(entity_id={entity_id}, transition={transition})") - elif color_temp_kelvin is not None: - light.turn_on(entity_id=entity_id, brightness=brightness, transition=transition, color_temp_kelvin=color_temp_kelvin) - log.info(f"PROGRESS adjust_light: light.turn_on(entity_id={entity_id}, brightness={brightness}, transition={transition}, color_temp_kelvin={color_temp_kelvin})") - else: - light.turn_on(entity_id=entity_id, brightness=brightness, transition=transition) - log.info(f"PROGRESS adjust_light: light.turn_on(entity_id={entity_id}, brightness={brightness}, transition={transition})") - -def light_has_target_state(entity_id, brightness): - if brightness == 0: - return state.get(entity_id) == 'off' - else: - return state.get(entity_id).brightness == brightness diff --git a/sensors/lights.yaml b/sensors/lights.yaml index 3ed85c12..157467ca 100644 --- a/sensors/lights.yaml +++ b/sensors/lights.yaml @@ -116,6 +116,7 @@ 60 {% endif %} target_entity_id: light.living_room + color_temp_kelvin: "{{ state_attr('switch.adaptive_lighting_living_room', 'color_temp_kelvin') }}" light_corridor: value_template: unknown @@ -139,6 +140,7 @@ 60 {% endif %} target_entity_id: light.corridor + color_temp_kelvin: "{{ state_attr('switch.adaptive_lighting_corridor', 'color_temp_kelvin') }}" light_bedroom: value_template: unknown @@ -153,3 +155,4 @@ {% endif %} transition: 60 target_entity_id: light.bedroom + color_temp_kelvin: "{{ state_attr('switch.adaptive_lighting_bedroom', 'color_temp_kelvin') }}"