def test_setting_rising(self):
        """ Test retrieving sun setting and rising. """
        # Compare it with the real data
        self.hass.config.latitude = '32.87336'
        self.hass.config.longitude = '117.22743'
        sun.setup(self.hass, None)

        observer = ephem.Observer()
        observer.lat = '32.87336'  # pylint: disable=assigning-non-slot
        observer.long = '117.22743'  # pylint: disable=assigning-non-slot

        utc_now = dt_util.utcnow()
        body_sun = ephem.Sun()  # pylint: disable=no-member
        next_rising_dt = observer.next_rising(
            body_sun, start=utc_now).datetime().replace(tzinfo=dt_util.UTC)
        next_setting_dt = observer.next_setting(
            body_sun, start=utc_now).datetime().replace(tzinfo=dt_util.UTC)

        # Home Assistant strips out microseconds
        # strip it out of the datetime objects
        next_rising_dt = dt_util.strip_microseconds(next_rising_dt)
        next_setting_dt = dt_util.strip_microseconds(next_setting_dt)

        self.assertEqual(next_rising_dt, sun.next_rising_utc(self.hass))
        self.assertEqual(next_setting_dt, sun.next_setting_utc(self.hass))

        # Point it at a state without the proper attributes
        self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
        self.assertIsNone(sun.next_rising(self.hass))
        self.assertIsNone(sun.next_setting(self.hass))

        # Point it at a non-existing state
        self.assertIsNone(sun.next_rising(self.hass, 'non.existing'))
        self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))
Example #2
0
    def test_setting_rising(self):
        """ Test retrieving sun setting and rising. """
        # Compare it with the real data
        self.hass.config.latitude = '32.87336'
        self.hass.config.longitude = '117.22743'
        sun.setup(self.hass, None)

        observer = ephem.Observer()
        observer.lat = '32.87336'  # pylint: disable=assigning-non-slot
        observer.long = '117.22743'  # pylint: disable=assigning-non-slot

        utc_now = dt_util.utcnow()
        body_sun = ephem.Sun()  # pylint: disable=no-member
        next_rising_dt = observer.next_rising(
            body_sun, start=utc_now).datetime().replace(tzinfo=dt_util.UTC)
        next_setting_dt = observer.next_setting(
            body_sun, start=utc_now).datetime().replace(tzinfo=dt_util.UTC)

        # Home Assistant strips out microseconds
        # strip it out of the datetime objects
        next_rising_dt = dt_util.strip_microseconds(next_rising_dt)
        next_setting_dt = dt_util.strip_microseconds(next_setting_dt)

        self.assertEqual(next_rising_dt, sun.next_rising_utc(self.hass))
        self.assertEqual(next_setting_dt, sun.next_setting_utc(self.hass))

        # Point it at a state without the proper attributes
        self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
        self.assertIsNone(sun.next_rising(self.hass))
        self.assertIsNone(sun.next_setting(self.hass))

        # Point it at a non-existing state
        self.assertIsNone(sun.next_rising(self.hass, 'non.existing'))
        self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))
    def test_state_change(self):
        """ Test if the state changes at next setting/rising. """
        self.assertTrue(
            sun.setup(
                self.hass, {
                    ha.DOMAIN: {
                        CONF_LATITUDE: '32.87336',
                        CONF_LONGITUDE: '117.22743'
                    }
                }))

        if sun.is_on(self.hass):
            test_state = sun.STATE_BELOW_HORIZON
            test_time = sun.next_setting(self.hass)
        else:
            test_state = sun.STATE_ABOVE_HORIZON
            test_time = sun.next_rising(self.hass)

        self.assertIsNotNone(test_time)

        self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
                           {ha.ATTR_NOW: test_time + dt.timedelta(seconds=5)})

        self.hass.pool.block_till_done()

        self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
Example #4
0
 def find_start_time(self, now):
     """Return sunrise or start_time if given."""
     if self._start_time:
         sunrise = now.replace(
             hour=self._start_time.hour, minute=self._start_time.minute,
             second=0)
     else:
         sunrise = next_rising(self.hass, SUN).replace(
             day=now.day, month=now.month, year=now.year)
     return sunrise
Example #5
0
def sun(hass, before=None, after=None, before_offset=None, after_offset=None):
    """Test if current time matches sun requirements."""
    now = dt_util.now().time()
    before_offset = before_offset or timedelta(0)
    after_offset = after_offset or timedelta(0)

    if before == SUN_EVENT_SUNRISE and now > (sun_cmp.next_rising(hass) + before_offset).time():
        return False

    elif before == SUN_EVENT_SUNSET and now > (sun_cmp.next_setting(hass) + before_offset).time():
        return False

    if after == SUN_EVENT_SUNRISE and now < (sun_cmp.next_rising(hass) + after_offset).time():
        return False

    elif after == SUN_EVENT_SUNSET and now < (sun_cmp.next_setting(hass) + after_offset).time():
        return False

    return True
Example #6
0
    def test_setting_rising(self):
        """Test retrieving sun setting and rising."""
        setup_component(self.hass, sun.DOMAIN,
                        {sun.DOMAIN: {
                            sun.CONF_ELEVATION: 0
                        }})

        from astral import Astral

        astral = Astral()
        utc_now = dt_util.utcnow()

        latitude = self.hass.config.latitude
        longitude = self.hass.config.longitude

        mod = -1
        while True:
            next_rising = (astral.sunrise_utc(utc_now + timedelta(days=mod),
                                              latitude, longitude))
            if next_rising > utc_now:
                break
            mod += 1

        mod = -1
        while True:
            next_setting = (astral.sunset_utc(utc_now + timedelta(days=mod),
                                              latitude, longitude))
            if next_setting > utc_now:
                break
            mod += 1

        self.assertEqual(next_rising, sun.next_rising_utc(self.hass))
        self.assertEqual(next_setting, sun.next_setting_utc(self.hass))

        # Point it at a state without the proper attributes
        self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
        self.assertIsNone(sun.next_rising(self.hass))
        self.assertIsNone(sun.next_setting(self.hass))

        # Point it at a non-existing state
        self.assertIsNone(sun.next_rising(self.hass, 'non.existing'))
        self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))
Example #7
0
 def find_start_time(self, now):
     """Return sunrise or start_time if given."""
     if self._start_time:
         sunrise = now.replace(hour=self._start_time.hour,
                               minute=self._start_time.minute,
                               second=0)
     else:
         sunrise = next_rising(self.hass, SUN).replace(day=now.day,
                                                       month=now.month,
                                                       year=now.year)
     return sunrise
    def test_setting_rising(self):
        """ Test retrieving sun setting and rising. """
        # Compare it with the real data
        self.assertTrue(
            sun.setup(
                self.hass, {
                    ha.DOMAIN: {
                        CONF_LATITUDE: '32.87336',
                        CONF_LONGITUDE: '117.22743'
                    }
                }))

        observer = ephem.Observer()
        observer.lat = '32.87336'  # pylint: disable=assigning-non-slot
        observer.long = '117.22743'  # pylint: disable=assigning-non-slot

        utc_now = dt.datetime.utcnow()
        body_sun = ephem.Sun()  # pylint: disable=no-member
        next_rising_dt = ephem.localtime(
            observer.next_rising(body_sun, start=utc_now))
        next_setting_dt = ephem.localtime(
            observer.next_setting(body_sun, start=utc_now))

        # Home Assistant strips out microseconds
        # strip it out of the datetime objects
        next_rising_dt = next_rising_dt - dt.timedelta(
            microseconds=next_rising_dt.microsecond)
        next_setting_dt = next_setting_dt - dt.timedelta(
            microseconds=next_setting_dt.microsecond)

        self.assertEqual(next_rising_dt, sun.next_rising(self.hass))
        self.assertEqual(next_setting_dt, sun.next_setting(self.hass))

        # Point it at a state without the proper attributes
        self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
        self.assertIsNone(sun.next_rising(self.hass))
        self.assertIsNone(sun.next_setting(self.hass))

        # Point it at a non-existing state
        self.assertIsNone(sun.next_rising(self.hass, 'non.existing'))
        self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))
Example #9
0
    def test_setting_rising(self):
        """Test retrieving sun setting and rising."""
        setup_component(self.hass, sun.DOMAIN, {
            sun.DOMAIN: {sun.CONF_ELEVATION: 0}})

        from astral import Astral

        astral = Astral()
        utc_now = dt_util.utcnow()

        latitude = self.hass.config.latitude
        longitude = self.hass.config.longitude

        mod = -1
        while True:
            next_rising = (astral.sunrise_utc(utc_now +
                           timedelta(days=mod), latitude, longitude))
            if next_rising > utc_now:
                break
            mod += 1

        mod = -1
        while True:
            next_setting = (astral.sunset_utc(utc_now +
                            timedelta(days=mod), latitude, longitude))
            if next_setting > utc_now:
                break
            mod += 1

        self.assertEqual(next_rising, sun.next_rising_utc(self.hass))
        self.assertEqual(next_setting, sun.next_setting_utc(self.hass))

        # Point it at a state without the proper attributes
        self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
        self.assertIsNone(sun.next_rising(self.hass))
        self.assertIsNone(sun.next_setting(self.hass))

        # Point it at a non-existing state
        self.assertIsNone(sun.next_rising(self.hass, 'non.existing'))
        self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))
    def test_setting_rising(self):
        """ Test retrieving sun setting and rising. """
        # Compare it with the real data
        self.assertTrue(sun.setup(
            self.hass,
            {ha.DOMAIN: {
                CONF_LATITUDE: '32.87336',
                CONF_LONGITUDE: '117.22743'
            }}))

        observer = ephem.Observer()
        observer.lat = '32.87336'  # pylint: disable=assigning-non-slot
        observer.long = '117.22743'  # pylint: disable=assigning-non-slot

        utc_now = dt.datetime.utcnow()
        body_sun = ephem.Sun()  # pylint: disable=no-member
        next_rising_dt = ephem.localtime(
            observer.next_rising(body_sun, start=utc_now))
        next_setting_dt = ephem.localtime(
            observer.next_setting(body_sun, start=utc_now))

        # Home Assistant strips out microseconds
        # strip it out of the datetime objects
        next_rising_dt = next_rising_dt - dt.timedelta(
            microseconds=next_rising_dt.microsecond)
        next_setting_dt = next_setting_dt - dt.timedelta(
            microseconds=next_setting_dt.microsecond)

        self.assertEqual(next_rising_dt, sun.next_rising(self.hass))
        self.assertEqual(next_setting_dt, sun.next_setting(self.hass))

        # Point it at a state without the proper attributes
        self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
        self.assertIsNone(sun.next_rising(self.hass))
        self.assertIsNone(sun.next_setting(self.hass))

        # Point it at a non-existing state
        self.assertIsNone(sun.next_rising(self.hass, 'non.existing'))
        self.assertIsNone(sun.next_setting(self.hass, 'non.existing'))
Example #11
0
    def test_setting_rising(self):
        """ Test retrieving sun setting and rising. """
        latitude = 32.87336
        longitude = 117.22743

        # Compare it with the real data
        self.hass.config.latitude = latitude
        self.hass.config.longitude = longitude
        sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})

        astral = Astral()
        utc_now = dt_util.utcnow()

        mod = -1
        while True:
            next_rising = astral.sunrise_utc(utc_now + timedelta(days=mod), latitude, longitude)
            if next_rising > utc_now:
                break
            mod += 1

        mod = -1
        while True:
            next_setting = astral.sunset_utc(utc_now + timedelta(days=mod), latitude, longitude)
            if next_setting > utc_now:
                break
            mod += 1

        self.assertEqual(next_rising, sun.next_rising_utc(self.hass))
        self.assertEqual(next_setting, sun.next_setting_utc(self.hass))

        # Point it at a state without the proper attributes
        self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
        self.assertIsNone(sun.next_rising(self.hass))
        self.assertIsNone(sun.next_setting(self.hass))

        # Point it at a non-existing state
        self.assertIsNone(sun.next_rising(self.hass, "non.existing"))
        self.assertIsNone(sun.next_setting(self.hass, "non.existing"))
Example #12
0
def sun(hass, before=None, after=None, before_offset=None, after_offset=None):
    """Test if current time matches sun requirements."""
    now = dt_util.now().time()
    before_offset = before_offset or timedelta(0)
    after_offset = after_offset or timedelta(0)

    if before == SUN_EVENT_SUNRISE and now > (sun_cmp.next_rising(hass) +
                                              before_offset).time():
        return False

    elif before == SUN_EVENT_SUNSET and now > (sun_cmp.next_setting(hass) +
                                               before_offset).time():
        return False

    if after == SUN_EVENT_SUNRISE and now < (sun_cmp.next_rising(hass) +
                                             after_offset).time():
        return False

    elif after == SUN_EVENT_SUNSET and now < (sun_cmp.next_setting(hass) +
                                              after_offset).time():
        return False

    return True
Example #13
0
    def test_state_change(self):
        """Test if the state changes at next setting/rising."""
        sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})

        if sun.is_on(self.hass):
            test_state = sun.STATE_BELOW_HORIZON
            test_time = sun.next_setting(self.hass)
        else:
            test_state = sun.STATE_ABOVE_HORIZON
            test_time = sun.next_rising(self.hass)

        self.assertIsNotNone(test_time)

        self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: test_time + timedelta(seconds=5)})

        self.hass.pool.block_till_done()

        self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
Example #14
0
    def test_state_change(self):
        """Test if the state changes at next setting/rising."""
        sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})

        if sun.is_on(self.hass):
            test_state = sun.STATE_BELOW_HORIZON
            test_time = sun.next_setting(self.hass)
        else:
            test_state = sun.STATE_ABOVE_HORIZON
            test_time = sun.next_rising(self.hass)

        self.assertIsNotNone(test_time)

        self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
                           {ha.ATTR_NOW: test_time + timedelta(seconds=5)})

        self.hass.pool.block_till_done()

        self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
Example #15
0
    def calculatecolor():
        sunrise = sun.next_rising(hass)
        sunset = sun.next_setting(hass)
        currenttime = dt_util.as_local(datetime.datetime.today())
        midday = sunrise + datetime.timedelta(
            seconds=(sunset - sunrise).total_seconds() / 2)
        brightness = 1
        colortemp = 2000

        if sunrise > sunset:
            if currenttime < midday:
                colortemp = 2000 + ((currenttime - sunrise) /
                                    (midday - sunrise) * 3800)
                brightness = ((currenttime - sunrise) / (midday - sunrise))
            else:
                colortemp = 6500 - ((currenttime - midday).total_seconds() /
                                    sunset.timestamp() * 3800)
                brightness = 1 - ((currenttime - midday).total_seconds() /
                                  sunset.timestamp())
        return ColorInfo(colortemp, max(brightness, 1))
Example #16
0
    def test_state_change(self):
        """ Test if the state changes at next setting/rising. """
        self.hass.config.latitude = "32.87336"
        self.hass.config.longitude = "117.22743"
        sun.setup(self.hass, {})

        if sun.is_on(self.hass):
            test_state = sun.STATE_BELOW_HORIZON
            test_time = sun.next_setting(self.hass)
        else:
            test_state = sun.STATE_ABOVE_HORIZON
            test_time = sun.next_rising(self.hass)

        self.assertIsNotNone(test_time)

        self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: test_time + timedelta(seconds=5)})

        self.hass.pool.block_till_done()

        self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
Example #17
0
    def test_state_change(self):
        """ Test if the state changes at next setting/rising. """
        self.hass.config.latitude = '32.87336'
        self.hass.config.longitude = '117.22743'
        sun.setup(self.hass, {})

        if sun.is_on(self.hass):
            test_state = sun.STATE_BELOW_HORIZON
            test_time = sun.next_setting(self.hass)
        else:
            test_state = sun.STATE_ABOVE_HORIZON
            test_time = sun.next_rising(self.hass)

        self.assertIsNotNone(test_time)

        self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
                           {ha.ATTR_NOW: test_time + timedelta(seconds=5)})

        self.hass.pool.block_till_done()

        self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
    def test_state_change(self):
        """ Test if the state changes at next setting/rising. """
        self.assertTrue(sun.setup(
            self.hass,
            {ha.DOMAIN: {
                CONF_LATITUDE: '32.87336',
                CONF_LONGITUDE: '117.22743'
            }}))

        if sun.is_on(self.hass):
            test_state = sun.STATE_BELOW_HORIZON
            test_time = sun.next_setting(self.hass)
        else:
            test_state = sun.STATE_ABOVE_HORIZON
            test_time = sun.next_rising(self.hass)

        self.assertIsNotNone(test_time)

        self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
                           {ha.ATTR_NOW: test_time + dt.timedelta(seconds=5)})

        self.hass._pool.block_till_done()

        self.assertEqual(test_state, self.hass.states.get(sun.ENTITY_ID).state)
Example #19
0
 def before_func():
     """Return time before sunrise."""
     return sun.next_rising(hass) + before_offset
Example #20
0
 def after_func():
     """Return time after sunrise."""
     return sun.next_rising(hass) + after_offset