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 flux_update(self, now=None): """Update all the lights using flux.""" if now is None: now = dt_now() sunset = next_setting(self.hass, SUN).replace(day=now.day, month=now.month, year=now.year) start_time = self.find_start_time(now) stop_time = now.replace(hour=self._stop_time.hour, minute=self._stop_time.minute, second=0) if start_time < now < sunset: # Daytime time_state = 'day' temp_range = abs(self._start_colortemp - self._sunset_colortemp) day_length = int(sunset.timestamp() - start_time.timestamp()) seconds_from_start = int(now.timestamp() - start_time.timestamp()) percentage_complete = seconds_from_start / day_length temp_offset = temp_range * percentage_complete if self._start_colortemp > self._sunset_colortemp: temp = self._start_colortemp - temp_offset else: temp = self._start_colortemp + temp_offset else: # Nightime time_state = 'night' if now < stop_time and now > start_time: now_time = now else: now_time = stop_time temp_range = abs(self._sunset_colortemp - self._stop_colortemp) night_length = int(stop_time.timestamp() - sunset.timestamp()) seconds_from_sunset = int(now_time.timestamp() - sunset.timestamp()) percentage_complete = seconds_from_sunset / night_length temp_offset = temp_range * percentage_complete if self._sunset_colortemp > self._stop_colortemp: temp = self._sunset_colortemp - temp_offset else: temp = self._sunset_colortemp + temp_offset x_val, y_val, b_val = color_RGB_to_xy(*color_temperature_to_rgb(temp)) brightness = self._brightness if self._brightness else b_val if self._mode == MODE_XY: set_lights_xy(self.hass, self._lights, x_val, y_val, brightness) _LOGGER.info("Lights updated to x:%s y:%s brightness:%s, %s%%" " of %s cycle complete at %s", x_val, y_val, brightness, round( percentage_complete * 100), time_state, as_local(now)) else: # Convert to mired and clamp to allowed values mired = color_temperature_kelvin_to_mired(temp) mired = max(HASS_COLOR_MIN, min(mired, HASS_COLOR_MAX)) set_lights_temp(self.hass, self._lights, mired, brightness) _LOGGER.info("Lights updated to mired:%s brightness:%s, %s%%" " of %s cycle complete at %s", mired, brightness, round(percentage_complete * 100), time_state, as_local(now))
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)
def flux_update(self, now=None): """Update all the lights using flux.""" if now is None: now = dt_now() sunset = next_setting(self.hass, SUN).replace(day=now.day, month=now.month, year=now.year) start_time = self.find_start_time(now) stop_time = now.replace(hour=self._stop_time.hour, minute=self._stop_time.minute, second=0) if start_time < now < sunset: # Daytime time_state = 'day' temp_range = abs(self._start_colortemp - self._sunset_colortemp) day_length = int(sunset.timestamp() - start_time.timestamp()) seconds_from_start = int(now.timestamp() - start_time.timestamp()) percentage_complete = seconds_from_start / day_length temp_offset = temp_range * percentage_complete if self._start_colortemp > self._sunset_colortemp: temp = self._start_colortemp - temp_offset else: temp = self._start_colortemp + temp_offset else: # Nightime time_state = 'night' if now < stop_time and now > start_time: now_time = now else: now_time = stop_time temp_range = abs(self._sunset_colortemp - self._stop_colortemp) night_length = int(stop_time.timestamp() - sunset.timestamp()) seconds_from_sunset = int(now_time.timestamp() - sunset.timestamp()) percentage_complete = seconds_from_sunset / night_length temp_offset = temp_range * percentage_complete if self._sunset_colortemp > self._stop_colortemp: temp = self._sunset_colortemp - temp_offset else: temp = self._sunset_colortemp + temp_offset x_val, y_val, b_val = color_RGB_to_xy(*color_temperature_to_rgb(temp)) brightness = self._brightness if self._brightness else b_val if self._disable_brightness_adjust: brightness = None if self._mode == MODE_XY: set_lights_xy(self.hass, self._lights, x_val, y_val, brightness) _LOGGER.info("Lights updated to x:%s y:%s brightness:%s, %s%%" " of %s cycle complete at %s", x_val, y_val, brightness, round( percentage_complete * 100), time_state, now) else: # Convert to mired and clamp to allowed values mired = color_temperature_kelvin_to_mired(temp) mired = max(HASS_COLOR_MIN, min(mired, HASS_COLOR_MAX)) set_lights_temp(self.hass, self._lights, mired, brightness) _LOGGER.info("Lights updated to mired:%s brightness:%s, %s%%" " of %s cycle complete at %s", mired, brightness, round(percentage_complete * 100), time_state, now)
def flux_update(self, now=None): """Update all the lights using flux.""" if now is None: now = dt_now() sunset = next_setting(self.hass, SUN).replace(day=now.day, month=now.month, year=now.year) start_time = self.find_start_time(now) stop_time = now.replace(hour=self._stop_time.hour, minute=self._stop_time.minute, second=0) if start_time < now < sunset: # Daytime time_state = 'day' temp_range = abs(self._start_colortemp - self._sunset_colortemp) day_length = int(sunset.timestamp() - start_time.timestamp()) seconds_from_start = int(now.timestamp() - start_time.timestamp()) percentage_complete = seconds_from_start / day_length temp_offset = temp_range * percentage_complete if self._start_colortemp > self._sunset_colortemp: temp = self._start_colortemp - temp_offset else: temp = self._start_colortemp + temp_offset else: # Nightime time_state = 'night' if now < stop_time and now > start_time: now_time = now else: now_time = stop_time temp_range = abs(self._sunset_colortemp - self._stop_colortemp) night_length = int(stop_time.timestamp() - sunset.timestamp()) seconds_from_sunset = int(now_time.timestamp() - sunset.timestamp()) percentage_complete = seconds_from_sunset / night_length temp_offset = temp_range * percentage_complete if self._sunset_colortemp > self._stop_colortemp: temp = self._sunset_colortemp - temp_offset else: temp = self._sunset_colortemp + temp_offset if self._mode == MODE_XY: x_val, y_val, b_val = color_RGB_to_xy(*temp_to_rgb(temp)) brightness = self._brightness if self._brightness else b_val set_lights_xy(self.hass, self._lights, x_val, y_val, brightness) _LOGGER.info("Lights updated to x:%s y:%s brightness:%s, %s%%" " of %s cycle complete at %s", x_val, y_val, brightness, round( percentage_complete * 100), time_state, as_local(now)) else: set_lights_temp(self.hass, self._lights, temp, self._mode) _LOGGER.info("Lights updated to temp:%s, %s%%" " of %s cycle complete at %s", temp, round(percentage_complete * 100), time_state, as_local(now))
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
def flux_update(self, now=dt_now()): """Update all the lights using flux.""" sunset = next_setting(self.hass, SUN).replace(day=now.day, month=now.month, year=now.year) start_time = self.find_start_time(now) stop_time = now.replace(hour=self._stop_time.hour, minute=self._stop_time.minute, second=0) if start_time < now < sunset: # Daytime time_state = 'day' temp_range = abs(self._start_colortemp - self._sunset_colortemp) day_length = int(sunset.timestamp() - start_time.timestamp()) seconds_from_start = int(now.timestamp() - start_time.timestamp()) percentage_complete = seconds_from_start / day_length temp_offset = temp_range * percentage_complete if self._start_colortemp > self._sunset_colortemp: temp = self._start_colortemp - temp_offset else: temp = self._start_colortemp + temp_offset else: # Nightime time_state = 'night' if now < stop_time and now > start_time: now_time = now else: now_time = stop_time temp_range = abs(self._sunset_colortemp - self._stop_colortemp) night_length = int(stop_time.timestamp() - sunset.timestamp()) seconds_from_sunset = int(now_time.timestamp() - sunset.timestamp()) percentage_complete = seconds_from_sunset / night_length temp_offset = temp_range * percentage_complete if self._sunset_colortemp > self._stop_colortemp: temp = self._sunset_colortemp - temp_offset else: temp = self._sunset_colortemp + temp_offset if self._mode == MODE_XY: x_val, y_val, b_val = color_RGB_to_xy(*temp_to_rgb(temp)) brightness = self._brightness if self._brightness else b_val set_lights_xy(self.hass, self._lights, x_val, y_val, brightness) _LOGGER.info("Lights updated to x:%s y:%s brightness:%s, %s%%" " of %s cycle complete at %s", x_val, y_val, brightness, round( percentage_complete * 100), time_state, as_local(now)) else: set_lights_temp(self.hass, self._lights, temp, self._mode) _LOGGER.info("Lights updated to temp:%s, %s%%" " of %s cycle complete at %s", temp, round(percentage_complete * 100), time_state, as_local(now))
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'))
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'))
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"))
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)
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 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))
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)
def after_func(): """Return time after sunset.""" return sun.next_setting(hass) + after_offset
def before_func(): """Return time before sunset.""" return sun.next_setting(hass) + before_offset