def sunrise_call_action(now=None): """Call action with right context.""" next_setting = get_location_astral_event_next( get_astral_location(self._hass), SUN_EVENT_SUNSET, dt_util.utcnow()) + timedelta(hours=1) _LOGGER.info( f"Good Morning! Time to prepare the day until the sun will set at {next_setting - timedelta(hours=1)}" ) remaining_api_calls = self.get_remaining_API_count() delay = (next_setting - dt_util.utcnow()) / (remaining_api_calls - 1) _LOGGER.info( f"During the day, there will be {remaining_api_calls} updates delayed by {delay} each" ) # Schedule updates over the day (starting on 0 to process early morning update) for i in range(0, remaining_api_calls): exec_delay = delay.total_seconds() * i exec_time = dt_util.utcnow() + timedelta(seconds=exec_delay) _LOGGER.info( f"History update scheduled update at {exec_time.isoformat()}" ) async_call_later(self._hass, exec_delay, self.update_history)
def _check_event(self, utc_point_in_time, event, before): next_utc = get_location_astral_event_next(self.location, event, utc_point_in_time) if next_utc < self._next_change: self._next_change = next_utc self.phase = before return next_utc
async def setup_auto_fetch(self): try: _LOGGER.debug( "registering API auto fetching hourly between sun up and sun set" ) location, elevation = get_astral_location(self._hass) next_setting = get_location_astral_event_next( location, elevation, SUN_EVENT_SUNSET, dt_util.utcnow()) + timedelta(hours=1) self._finishhour = next_setting.astimezone( ).hour # already one hour ahead self._debugData["auto_fetch_end_hour"] = self._finishhour self._auto_fetch_tracker = async_track_utc_time_change( self._hass, self.update_forecast, minute=0, second=0, local=True) self._debugData["auto_fetch_timer_object"] = ( self._auto_fetch_tracker is not None) _LOGGER.debug("created auto forecast fetch hourly timer") _LOGGER.debug( "Remember that even though its every hour, the api will only connect between the hours %s and %s and at midnight", self._starthour, self._finishhour) except Exception: _LOGGER.error("setup_auto_fetch: %s", traceback.format_exc())