Exemple #1
0
    def _turn_to_next_day(self):
        """Turn to to the next day."""
        if is_sun_event(self._after):
            self._time_after = get_astral_event_next(
                self.opp, self._after, self._time_after - self._after_offset)
            self._time_after += self._after_offset
        else:
            # Offset is already there
            self._time_after += timedelta(days=1)

        if is_sun_event(self._before):
            self._time_before = get_astral_event_next(
                self.opp, self._before,
                self._time_before - self._before_offset)
            self._time_before += self._before_offset
        else:
            # Offset is already there
            self._time_before += timedelta(days=1)
Exemple #2
0
    def _listen_next_sun_event(self) -> None:
        """Set up the sun event listener."""
        assert self._unsub_sun is None

        self._unsub_sun = async_track_point_in_utc_time(
            self.opp,
            self._handle_sun_event,
            get_astral_event_next(self.opp, self.event, offset=self.offset),
        )
Exemple #3
0
    def _calculate_boudary_time(self):
        """Calculate internal absolute time boundaries."""
        nowutc = dt_util.utcnow()
        # If after value is a sun event instead of absolute time
        if is_sun_event(self._after):
            # Calculate the today's event utc time or
            # if not available take next
            after_event_date = get_astral_event_date(
                self.opp, self._after, nowutc) or get_astral_event_next(
                    self.opp, self._after, nowutc)
        else:
            # Convert local time provided to UTC today
            # datetime.combine(date, time, tzinfo) is not supported
            # in python 3.5. The self._after is provided
            # with opp configured TZ not system wide
            after_event_date = self._naive_time_to_utc_datetime(self._after)

        self._time_after = after_event_date

        # If before value is a sun event instead of absolute time
        if is_sun_event(self._before):
            # Calculate the today's event utc time or  if not available take
            # next
            before_event_date = get_astral_event_date(
                self.opp, self._before, nowutc) or get_astral_event_next(
                    self.opp, self._before, nowutc)
            # Before is earlier than after
            if before_event_date < after_event_date:
                # Take next day for before
                before_event_date = get_astral_event_next(
                    self.opp, self._before, after_event_date)
        else:
            # Convert local time provided to UTC today, see above
            before_event_date = self._naive_time_to_utc_datetime(self._before)

            # It is safe to add timedelta days=1 to UTC as there is no DST
            if before_event_date < after_event_date + self._after_offset:
                before_event_date += timedelta(days=1)

        self._time_before = before_event_date

        # Add offset to utc boundaries according to the configuration
        self._time_after += self._after_offset
        self._time_before += self._before_offset
Exemple #4
0
    def calc_time_for_light_when_sunset():
        """Calculate the time when to start fading lights in when sun sets.

        Returns None if no next_setting data available.

        Async friendly.
        """
        next_setting = get_astral_event_next(opp, SUN_EVENT_SUNSET)
        if not next_setting:
            return None
        return next_setting - LIGHT_TRANSITION_TIME * len(light_ids)
Exemple #5
0
def test_norway_in_june(opp):
    """Test location in Norway where the sun doesn't set in summer."""
    opp.config.latitude = 69.6
    opp.config.longitude = 18.8

    june = datetime(2016, 6, 1, tzinfo=dt_util.UTC)

    print(
        sun.get_astral_event_date(opp, SUN_EVENT_SUNRISE,
                                  datetime(2017, 7, 25)))
    print(
        sun.get_astral_event_date(opp, SUN_EVENT_SUNSET, datetime(2017, 7,
                                                                  25)))

    print(
        sun.get_astral_event_date(opp, SUN_EVENT_SUNRISE,
                                  datetime(2017, 7, 26)))
    print(
        sun.get_astral_event_date(opp, SUN_EVENT_SUNSET, datetime(2017, 7,
                                                                  26)))

    assert sun.get_astral_event_next(opp, SUN_EVENT_SUNRISE,
                                     june) == datetime(2016,
                                                       7,
                                                       24,
                                                       22,
                                                       59,
                                                       45,
                                                       689645,
                                                       tzinfo=dt_util.UTC)
    assert sun.get_astral_event_next(opp, SUN_EVENT_SUNSET,
                                     june) == datetime(2016,
                                                       7,
                                                       25,
                                                       22,
                                                       17,
                                                       13,
                                                       503932,
                                                       tzinfo=dt_util.UTC)
    assert sun.get_astral_event_date(opp, SUN_EVENT_SUNRISE, june) is None
    assert sun.get_astral_event_date(opp, SUN_EVENT_SUNSET, june) is None
Exemple #6
0
    def check_light_on_dev_state_change(entity, old_state, new_state):
        """Handle tracked device state changes."""
        lights_are_on = any_light_on()
        light_needed = not (lights_are_on or is_up(opp))

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

        # Do we need lights?
        if light_needed:
            logger.info("Home coming event for %s. Turning lights on", entity)
            opp.async_create_task(
                opp.services.async_call(
                    DOMAIN_LIGHT,
                    SERVICE_TURN_ON,
                    {
                        ATTR_ENTITY_ID: light_ids,
                        ATTR_PROFILE: light_profile
                    },
                ))

        # Are we in the time span were we would turn on the lights
        # if someone would be home?
        # Check this by seeing if current time is later then the point
        # in time when we would start putting the lights on.
        elif start_point and start_point < now < get_astral_event_next(
                opp, SUN_EVENT_SUNSET):

            # Check for every light if it would be on if someone was home
            # when the fading in started and turn it on if so
            for index, light_id in enumerate(light_ids):
                if now > start_point + index * LIGHT_TRANSITION_TIME:
                    opp.async_create_task(
                        opp.services.async_call(DOMAIN_LIGHT, SERVICE_TURN_ON,
                                                {ATTR_ENTITY_ID: light_id}))

                else:
                    # If this light didn't happen to be turned on yet so
                    # will all the following then, break.
                    break
Exemple #7
0
async def activate_automation(  # noqa: C901
        opp, device_group, light_group, light_profile, disable_turn_off):
    """Activate the automation."""
    logger = logging.getLogger(__name__)
    device_tracker = opp.components.device_tracker
    group = opp.components.group
    light = opp.components.light
    person = opp.components.person

    if device_group is None:
        device_entity_ids = opp.states.async_entity_ids(device_tracker.DOMAIN)
    else:
        device_entity_ids = group.get_entity_ids(device_group,
                                                 device_tracker.DOMAIN)
        device_entity_ids.extend(
            group.get_entity_ids(device_group, person.DOMAIN))

    if not device_entity_ids:
        logger.error("No devices found to track")
        return

    # Get the light IDs from the specified group
    if light_group is None:
        light_ids = opp.states.async_entity_ids(light.DOMAIN)
    else:
        light_ids = group.get_entity_ids(light_group, light.DOMAIN)

    if not light_ids:
        logger.error("No lights found to turn on")
        return

    @callback
    def anyone_home():
        """Test if anyone is home."""
        return any(device_tracker.is_on(dt_id) for dt_id in device_entity_ids)

    @callback
    def any_light_on():
        """Test if any light on."""
        return any(light.is_on(light_id) for light_id in light_ids)

    def calc_time_for_light_when_sunset():
        """Calculate the time when to start fading lights in when sun sets.

        Returns None if no next_setting data available.

        Async friendly.
        """
        next_setting = get_astral_event_next(opp, SUN_EVENT_SUNSET)
        if not next_setting:
            return None
        return next_setting - LIGHT_TRANSITION_TIME * len(light_ids)

    async def async_turn_on_before_sunset(light_id):
        """Turn on lights."""
        if not anyone_home() or light.is_on(light_id):
            return
        await opp.services.async_call(
            DOMAIN_LIGHT,
            SERVICE_TURN_ON,
            {
                ATTR_ENTITY_ID: light_id,
                ATTR_TRANSITION: LIGHT_TRANSITION_TIME.total_seconds(),
                ATTR_PROFILE: light_profile,
            },
        )

    @callback
    def async_turn_on_factory(light_id):
        """Generate turn on callbacks as factory."""
        async def async_turn_on_light(now):
            """Turn on specific light."""
            await async_turn_on_before_sunset(light_id)

        return async_turn_on_light

    # Track every time sun rises so we can schedule a time-based
    # pre-sun set event
    @callback
    def schedule_light_turn_on(now):
        """Turn on all the lights at the moment sun sets.

        We will schedule to have each light start after one another
        and slowly transition in.
        """
        start_point = calc_time_for_light_when_sunset()
        if not start_point:
            return

        for index, light_id in enumerate(light_ids):
            async_track_point_in_utc_time(
                opp,
                async_turn_on_factory(light_id),
                start_point + index * LIGHT_TRANSITION_TIME,
            )

    async_track_point_in_utc_time(
        opp, schedule_light_turn_on,
        get_astral_event_next(opp, SUN_EVENT_SUNRISE))

    # If the sun is already above horizon schedule the time-based pre-sun set
    # event.
    if is_up(opp):
        schedule_light_turn_on(None)

    @callback
    def check_light_on_dev_state_change(entity, old_state, new_state):
        """Handle tracked device state changes."""
        lights_are_on = any_light_on()
        light_needed = not (lights_are_on or is_up(opp))

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

        # Do we need lights?
        if light_needed:
            logger.info("Home coming event for %s. Turning lights on", entity)
            opp.async_create_task(
                opp.services.async_call(
                    DOMAIN_LIGHT,
                    SERVICE_TURN_ON,
                    {
                        ATTR_ENTITY_ID: light_ids,
                        ATTR_PROFILE: light_profile
                    },
                ))

        # Are we in the time span were we would turn on the lights
        # if someone would be home?
        # Check this by seeing if current time is later then the point
        # in time when we would start putting the lights on.
        elif start_point and start_point < now < get_astral_event_next(
                opp, SUN_EVENT_SUNSET):

            # Check for every light if it would be on if someone was home
            # when the fading in started and turn it on if so
            for index, light_id in enumerate(light_ids):
                if now > start_point + index * LIGHT_TRANSITION_TIME:
                    opp.async_create_task(
                        opp.services.async_call(DOMAIN_LIGHT, SERVICE_TURN_ON,
                                                {ATTR_ENTITY_ID: light_id}))

                else:
                    # If this light didn't happen to be turned on yet so
                    # will all the following then, break.
                    break

    async_track_state_change(
        opp,
        device_entity_ids,
        check_light_on_dev_state_change,
        STATE_NOT_HOME,
        STATE_HOME,
    )

    if disable_turn_off:
        return

    @callback
    def turn_off_lights_when_all_leave(entity, old_state, new_state):
        """Handle device group state change."""
        # Make sure there is not someone home
        if anyone_home():
            return

        # Check if any light is on
        if not any_light_on():
            return

        logger.info(
            "Everyone has left but there are lights on. Turning them off")
        opp.async_create_task(
            opp.services.async_call(DOMAIN_LIGHT, SERVICE_TURN_OFF,
                                    {ATTR_ENTITY_ID: light_ids}))

    async_track_state_change(
        opp,
        device_entity_ids,
        turn_off_lights_when_all_leave,
        STATE_HOME,
        STATE_NOT_HOME,
    )

    return
Exemple #8
0
def test_next_events(opp):
    """Test retrieving next sun events."""
    utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
    from astral import LocationInfo
    import astral.sun

    utc_today = utc_now.date()

    location = LocationInfo(latitude=opp.config.latitude,
                            longitude=opp.config.longitude)

    mod = -1
    while True:
        next_dawn = astral.sun.dawn(location.observer,
                                    date=utc_today + timedelta(days=mod))
        if next_dawn > utc_now:
            break
        mod += 1

    mod = -1
    while True:
        next_dusk = astral.sun.dusk(location.observer,
                                    date=utc_today + timedelta(days=mod))
        if next_dusk > utc_now:
            break
        mod += 1

    mod = -1
    while True:
        next_midnight = astral.sun.midnight(location.observer,
                                            date=utc_today +
                                            timedelta(days=mod))
        if next_midnight > utc_now:
            break
        mod += 1

    mod = -1
    while True:
        next_noon = astral.sun.noon(location.observer,
                                    date=utc_today + timedelta(days=mod))
        if next_noon > utc_now:
            break
        mod += 1

    mod = -1
    while True:
        next_rising = astral.sun.sunrise(location.observer,
                                         date=utc_today + timedelta(days=mod))
        if next_rising > utc_now:
            break
        mod += 1

    mod = -1
    while True:
        next_setting = astral.sun.sunset(location.observer,
                                         utc_today + timedelta(days=mod))
        if next_setting > utc_now:
            break
        mod += 1

    with patch("openpeerpower.helpers.condition.dt_util.utcnow",
               return_value=utc_now):
        assert next_dawn == sun.get_astral_event_next(opp, "dawn")
        assert next_dusk == sun.get_astral_event_next(opp, "dusk")
        assert next_midnight == sun.get_astral_event_next(opp, "midnight")
        assert next_noon == sun.get_astral_event_next(opp, "noon")
        assert next_rising == sun.get_astral_event_next(opp, SUN_EVENT_SUNRISE)
        assert next_setting == sun.get_astral_event_next(opp, SUN_EVENT_SUNSET)