def test_track_sunrise(self):
        """ Test track sunrise decorator """
        latitude = 32.87336
        longitude = 117.22743

        # setup sun component
        self.hass.config.latitude = latitude
        self.hass.config.longitude = longitude
        sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})

        # get next sunrise/sunset
        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

        # use decorator
        runs = []
        decor = track_sunrise()
        decor(lambda x: runs.append(1))

        offset_runs = []
        offset = timedelta(minutes=30)
        decor = track_sunrise(offset)
        decor(lambda x: offset_runs.append(1))

        # run tests
        self._send_time_changed(next_rising - offset)
        self.hass.pool.block_till_done()
        self.assertEqual(0, len(runs))
        self.assertEqual(0, len(offset_runs))

        self._send_time_changed(next_rising)
        self.hass.pool.block_till_done()
        self.assertEqual(1, len(runs))
        self.assertEqual(0, len(offset_runs))

        self._send_time_changed(next_rising + offset)
        self.hass.pool.block_till_done()
        self.assertEqual(2, len(runs))
        self.assertEqual(1, len(offset_runs))
    def test_track_sunrise(self):
        """Test track sunrise decorator."""
        latitude = 32.87336
        longitude = 117.22743

        # Setup sun component
        self.hass.config.latitude = latitude
        self.hass.config.longitude = longitude
        sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})

        # Get next sunrise/sunset
        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

        # Use decorator
        runs = []
        decor = track_sunrise()
        decor(lambda x: runs.append(1))

        offset_runs = []
        offset = timedelta(minutes=30)
        decor = track_sunrise(offset)
        decor(lambda x: offset_runs.append(1))

        # Run tests
        self._send_time_changed(next_rising - offset)
        self.hass.block_till_done()
        self.assertEqual(0, len(runs))
        self.assertEqual(0, len(offset_runs))

        self._send_time_changed(next_rising)
        self.hass.block_till_done()
        self.assertEqual(1, len(runs))
        self.assertEqual(0, len(offset_runs))

        self._send_time_changed(next_rising + offset)
        self.hass.block_till_done()
        self.assertEqual(2, len(runs))
        self.assertEqual(1, len(offset_runs))