Example #1
0
def ensure_sun_risen(hass):
    """ Trigger sun to rise if below horizon. """
    if not sun.is_on(hass):
        hass.bus.fire(
            EVENT_TIME_CHANGED,
            {'now':
             sun.next_rising_utc(hass) + timedelta(seconds=10)})
    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 #3
0
def ensure_sun_risen(hass):
    """ Trigger sun to rise if below horizon. """
    if not sun.is_on(hass):
        hass.bus.fire(
            EVENT_TIME_CHANGED,
            {'now':
             sun.next_rising_utc(hass) + timedelta(seconds=10)})
Example #4
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'))
Example #5
0
    def next_rise():
        """Return the next sunrise."""
        next_time = sun.next_rising_utc(hass) + offset

        while next_time < dt_util.utcnow():
            next_time = next_time + timedelta(days=1)

        return next_time
Example #6
0
    def next_rise():
        """Return the next sunrise."""
        next_time = sun.next_rising_utc(hass) + offset

        while next_time < dt_util.utcnow():
            next_time = next_time + timedelta(days=1)

        return next_time
Example #7
0
    def test_norway_in_june(self):
        """Test location in Norway where the sun doesn't set in summer."""
        self.hass.config.latitude = 69.6
        self.hass.config.longitude = 18.8

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

        with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=june):
            assert sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})

        state = self.hass.states.get(sun.ENTITY_ID)

        assert state is not None
        assert sun.next_rising_utc(self.hass) == datetime(2016, 7, 25, 23, 38, 21, tzinfo=dt_util.UTC)
        assert sun.next_setting_utc(self.hass) == datetime(2016, 7, 26, 22, 4, 18, tzinfo=dt_util.UTC)
Example #8
0
    def test_norway_in_june(self):
        """Test location in Norway where the sun doesn't set in summer."""
        self.hass.config.latitude = 69.6
        self.hass.config.longitude = 18.8

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

        with patch('homeassistant.helpers.condition.dt_util.utcnow',
                   return_value=june):
            assert sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})

        state = self.hass.states.get(sun.ENTITY_ID)

        assert state is not None
        assert sun.next_rising_utc(self.hass) == \
            datetime(2016, 7, 25, 23, 38, 21, tzinfo=dt_util.UTC)
        assert sun.next_setting_utc(self.hass) == \
            datetime(2016, 7, 26, 22, 4, 18, tzinfo=dt_util.UTC)
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'))
Example #10
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 #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 ensure_sun_risen(hass):
    """ Trigger sun to rise if below horizon. """
    if sun.is_on(hass):
        return
    fire_time_changed(hass, sun.next_rising_utc(hass) + timedelta(seconds=10))
Example #13
0
def ensure_sun_risen(hass):
    """Trigger sun to rise if below horizon."""
    if sun.is_on(hass):
        return
    fire_time_changed(hass, sun.next_rising_utc(hass) + timedelta(seconds=10))