home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.67k stars 30.8k forks source link

Suggestion: Timed `device_sun_light_trigger` #1555

Closed llauren closed 8 years ago

llauren commented 8 years ago

Home Assistant release (hass --version):

0.15.0

Python release (python3 --version):

Python 3.4.3

Component/platform:

components/device_sun_light_trigger.py

Description of problem:

The presence based lights automated by device_sun_light_trigger should not turn on after it's sleepy time.

Expected:

I suggest the attribute before: 'HH:mm' to be added to the device_sun_light_trigger configuration and the following bits added to the device_sun_light_trigger.py code. Alas, i am no developer, so i don't know how to actually contribute to the project correctly, nor if this code in fact is kosher.

diff --git a/homeassistant/components/device_sun_light_trigger.py b/homeassistant/components/device_sun_light_trigger.py
index a954ae8..10b5ccb 100644
--- a/homeassistant/components/device_sun_light_trigger.py
+++ b/homeassistant/components/device_sun_light_trigger.py
@@ -24,6 +24,7 @@ LIGHT_PROFILE = 'relax'
 CONF_LIGHT_PROFILE = 'light_profile'
 CONF_LIGHT_GROUP = 'light_group'
 CONF_DEVICE_GROUP = 'device_group'
+CONF_BEFORE = 'before'

 # pylint: disable=too-many-locals
@@ -107,13 +108,20 @@ def setup(hass, config):
     if sun.is_on(hass):
         schedule_lights_at_sun_set(hass, None, None, None)

+    def it_is_late():
+        now = dt_util.now()
+        automation_ends = config[DOMAIN].get(CONF_BEFORE)
+        if automation_ends is None:
+            return False
+        return now < now.replace(hour=automation_ends.hour, minute=automation_ends.minute)
+
     @track_state_change(device_entity_ids, STATE_NOT_HOME, STATE_HOME)
     def check_light_on_dev_state_change(hass, entity, old_state, new_state):
         """Handle tracked device state changes."""
         # pylint: disable=unused-variable
         lights_are_on = group.is_on(hass, light_group)

-        light_needed = not (lights_are_on or sun.is_on(hass))
+        light_needed = not (lights_are_on or sun.is_on(hass) or it_is_late())

         # These variables are needed for the elif check
         now = dt_util.now()

Additional info:

Landrash commented 8 years ago

Wouldn't it be better to implement this with a rule in your automation?

#Copied from example code on home-assistant.io with addition of time condition
automation:
  trigger:
    platform: sun
    event: sunset
    offset: "-00:45:00"
  condition:
    - platform: state
      entity_id: group.all_devices
      state: home
    - platform: time
      before: "20:00:00"
  action:
    service: homeassistant.turn_on
    entity_id: group.living_room_lights
llauren commented 8 years ago

I don't think so. The rule you propose won't catch the situation where i come home after sunset (but before before). For that, i'll need to write the rule to trigger on the homecoming of group.all_devices on the condition that time is between sunset and before. Furthermore, i'll need to write rules to check whether group.all_devices leave home during that same interval. Four rules in all.

Landrash commented 8 years ago

@llauren Ahh, misinterpreted what you where trying to accomplish. If you want to be more specifik I could happily try and write those rules out for you. Not much of a coder but that I can at least do.

llauren commented 8 years ago

My automation currently has those four rules and they're just not very pretty to watch, nor are they very maintainable. Abstracting away all the mess behind one automation rule would be my way to go, assuming my diff above actually does that.

I could copy-paste my automation rules here for making my rule-bugs shallower, but that doesn't make those configs any prettier...

Spartan-II-117 commented 8 years ago

This seems like it would be a perfect fit for the new "apps" functionality

llauren commented 8 years ago

Indeed! I took a look @acockburn 's AppDaemon (i suppose that's the apps you suggested) architecture, and while i've yet to write my first one, i was rather impressed!

Spartan-II-117 commented 8 years ago

Can this issue be closed?