'Test track the sunrise.'

def test_track_sunrise(self):

    latitude = 32.87336
    longitude = 117.22743
    self.hass.config.latitude = latitude
    self.hass.config.longitude = longitude
    setup_component(self.hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
    astral = Astral()
    utc_now = datetime(2014, 5, 24, 12, 0, 0, tzinfo=dt_util.UTC)
    utc_today = utc_now.date()
    mod = (-1)
    while True:
        next_rising = astral.sunrise_utc((utc_today + timedelta(days=mod)), latitude, longitude)
        if (next_rising > utc_now):
            break
        mod += 1
    runs = []
    with patch('homeassistant.util.dt.utcnow', return_value=utc_now):
        unsub = track_sunrise(self.hass, (lambda : runs.append(1)))
    offset_runs = []
    offset = timedelta(minutes=30)
    with patch('homeassistant.util.dt.utcnow', return_value=utc_now):
        unsub2 = track_sunrise(self.hass, (lambda : offset_runs.append(1)), offset)
    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(1, len(runs))
    self.assertEqual(1, len(offset_runs))
    unsub()
    unsub2()
    self._send_time_changed((next_rising + offset))
    self.hass.block_till_done()
    self.assertEqual(1, len(runs))
    self.assertEqual(1, len(offset_runs))
