async def test_track_sunset(hass): """Test track the sunset.""" latitude = 32.87336 longitude = 117.22743 # Setup sun component hass.config.latitude = latitude hass.config.longitude = longitude assert await async_setup_component(hass, sun.DOMAIN, {sun.DOMAIN: { sun.CONF_ELEVATION: 0 }}) # Get next sunrise/sunset 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_setting = astral.sunset_utc(utc_today + timedelta(days=mod), latitude, longitude) if next_setting > utc_now: break mod += 1 # Track sunset runs = [] with patch("homeassistant.util.dt.utcnow", return_value=utc_now): unsub = async_track_sunset(hass, lambda: runs.append(1)) offset_runs = [] offset = timedelta(minutes=30) with patch("homeassistant.util.dt.utcnow", return_value=utc_now): unsub2 = async_track_sunset(hass, lambda: offset_runs.append(1), offset) # Run tests _send_time_changed(hass, next_setting - offset) await hass.async_block_till_done() assert len(runs) == 0 assert len(offset_runs) == 0 _send_time_changed(hass, next_setting) await hass.async_block_till_done() assert len(runs) == 1 assert len(offset_runs) == 0 _send_time_changed(hass, next_setting + offset) await hass.async_block_till_done() assert len(runs) == 1 assert len(offset_runs) == 1 unsub() unsub2() _send_time_changed(hass, next_setting + offset) await hass.async_block_till_done() assert len(runs) == 1 assert len(offset_runs) == 1
def __init__( self, hass, min_colortemp, max_colortemp, sunrise_offset, sunset_offset, sunrise_time, sunset_time, latitude, longitude, elevation, interval, transition, ): self.hass = hass self._min_colortemp = min_colortemp self._max_colortemp = max_colortemp self._sunrise_offset = sunrise_offset self._sunset_offset = sunset_offset self._manual_sunset = sunset_time self._manual_sunrise = sunrise_time self._latitude = latitude self._longitude = longitude self._elevation = elevation self._transition = transition self._percent = self.calc_percent() self._colortemp = self.calc_colortemp() self._rgb_color = self.calc_rgb() self._xy_color = self.calc_xy() self._hs_color = self.calc_hs() if self._manual_sunrise is not None: async_track_time_change( self.hass, self.update, hour=self._manual_sunrise.hour, minute=self._manual_sunrise.minute, second=self._manual_sunrise.second, ) else: async_track_sunrise(self.hass, self.update, self._sunrise_offset) if self._manual_sunset is not None: async_track_time_change( self.hass, self.update, hour=self._manual_sunset.hour, minute=self._manual_sunset.minute, second=self._manual_sunset.second, ) else: async_track_sunset(self.hass, self.update, self._sunset_offset) async_track_time_interval(self.hass, self.update, interval)
async def async_attach_trigger(hass, config, action, automation_info): """Listen for events based on configuration.""" event = config.get(CONF_EVENT) offset = config.get(CONF_OFFSET) description = event if offset: description = f"{description} with offset" job = HassJob(action) @callback def call_action(): """Call action with right context.""" hass.async_run_hass_job( job, { "trigger": { "platform": "sun", "event": event, "offset": offset, "description": description, } }, ) if event == SUN_EVENT_SUNRISE: return async_track_sunrise(hass, call_action, offset) return async_track_sunset(hass, call_action, offset)
async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: TriggerActionType, trigger_info: TriggerInfo, ) -> CALLBACK_TYPE: """Listen for events based on configuration.""" trigger_data = trigger_info["trigger_data"] event = config.get(CONF_EVENT) offset = config.get(CONF_OFFSET) description = event if offset: description = f"{description} with offset" job = HassJob(action) @callback def call_action(): """Call action with right context.""" hass.async_run_hass_job( job, { "trigger": { **trigger_data, "platform": "sun", "event": event, "offset": offset, "description": description, } }, ) if event == SUN_EVENT_SUNRISE: return async_track_sunrise(hass, call_action, offset) return async_track_sunset(hass, call_action, offset)
async def async_trigger(hass, config, action, automation_info): """Listen for events based on configuration.""" event = config.get(CONF_EVENT) offset = config.get(CONF_OFFSET) @callback def call_action(): """Call action with right context.""" hass.async_run_job( action, {"trigger": {"platform": "sun", "event": event, "offset": offset}} ) if event == SUN_EVENT_SUNRISE: return async_track_sunrise(hass, call_action, offset) return async_track_sunset(hass, call_action, offset)
def async_trigger(hass, config, action): """Listen for events based on configuration.""" event = config.get(CONF_EVENT) offset = config.get(CONF_OFFSET) @callback def call_action(): """Call action with right context.""" hass.async_run_job(action, { 'trigger': { 'platform': 'sun', 'event': event, 'offset': offset, }, }) if event == SUN_EVENT_SUNRISE: return async_track_sunrise(hass, call_action, offset) return async_track_sunset(hass, call_action, offset)
def async_trigger(hass, config, action): """Listen for events based on configuration.""" event = config.get(CONF_EVENT) offset = config.get(CONF_OFFSET) @asyncio.coroutine def call_action(): """Call action with right context.""" hass.async_add_job(action, { 'trigger': { 'platform': 'sun', 'event': event, 'offset': offset, }, }) # Do something to call action if event == SUN_EVENT_SUNRISE: return async_track_sunrise(hass, call_action, offset) else: return async_track_sunset(hass, call_action, offset)