Use the same timezone for "now" time and configured lat/long

This commit is contained in:
Clayton Nummer 2020-03-19 15:48:52 -04:00
commit 9e1d140e19

View file

@ -44,9 +44,9 @@ from homeassistant.helpers.event import track_sunrise, track_sunset, track_time_
from homeassistant.util.color import (
color_temperature_to_rgb, color_RGB_to_xy,
color_xy_to_hs)
from homeassistant.util.dt import utcnow as dt_utcnow, as_local
from homeassistant.util.dt import now as dt_now
from datetime import datetime, timedelta
from datetime import timedelta
VERSION = '1.0.11b'
@ -135,6 +135,7 @@ class CircadianLighting(object):
self.data['elevation'] = elevation
self.data['interval'] = interval
self.data['transition'] = transition
self.data['timezone'] = self.get_timezone()
self.data['percent'] = self.calc_percent()
self.data['colortemp'] = self.calc_colortemp()
self.data['rgb_color'] = self.calc_rgb()
@ -152,35 +153,41 @@ class CircadianLighting(object):
else:
track_sunset(self.hass, self._update, self.data['sunset_offset'])
def get_astral_location(self):
import astral
location = astral.Location()
location.name = 'name'
location.region = 'region'
location.latitude = self.data['latitude']
location.longitude = self.data['longitude']
location.elevation = self.data['elevation']
_LOGGER.debug("Astral location: " + str(location))
return location
def get_timezone(self):
timezone = self.get_astral_location().tz
_LOGGER.debug("Timezone: " + str(timezone))
return timezone
def get_sunrise_sunset(self, date = None):
if self.data['sunrise_time'] is not None and self.data['sunset_time'] is not None:
if date is None:
utcdate = dt_utcnow()
date = as_local(utcdate)
date = dt_now(self.data['timezone'])
sunrise = date.replace(hour=int(self.data['sunrise_time'].strftime("%H")), minute=int(self.data['sunrise_time'].strftime("%M")), second=int(self.data['sunrise_time'].strftime("%S")), microsecond=int(self.data['sunrise_time'].strftime("%f")))
sunset = date.replace(hour=int(self.data['sunset_time'].strftime("%H")), minute=int(self.data['sunset_time'].strftime("%M")), second=int(self.data['sunset_time'].strftime("%S")), microsecond=int(self.data['sunset_time'].strftime("%f")))
solar_noon = sunrise + (sunset - sunrise)/2
solar_midnight = sunset + ((sunrise + timedelta(days=1)) - sunset)/2
else:
import astral
location = astral.Location()
location.name = 'name'
location.region = 'region'
location.latitude = self.data['latitude']
location.longitude = self.data['longitude']
location.elevation = self.data['elevation']
_LOGGER.debug("Astral location: " + str(location))
location = self.get_astral_location()
if self.data['sunrise_time'] is not None:
if date is None:
utcdate = dt_utcnow()
date = as_local(utcdate)
date = dt_now(self.data['timezone'])
sunrise = date.replace(hour=int(self.data['sunrise_time'].strftime("%H")), minute=int(self.data['sunrise_time'].strftime("%M")), second=int(self.data['sunrise_time'].strftime("%S")), microsecond=int(self.data['sunrise_time'].strftime("%f")))
else:
sunrise = location.sunrise(date)
if self.data['sunset_time'] is not None:
if date is None:
utcdate = dt_utcnow()
date = as_local(utcdate)
date = dt_now(self.data['timezone'])
sunset = date.replace(hour=int(self.data['sunset_time'].strftime("%H")), minute=int(self.data['sunset_time'].strftime("%M")), second=int(self.data['sunset_time'].strftime("%S")), microsecond=int(self.data['sunset_time'].strftime("%f")))
else:
sunset = location.sunset(date)
@ -198,8 +205,7 @@ class CircadianLighting(object):
}
def calc_percent(self):
utcnow = dt_utcnow()
now = as_local(utcnow)
now = dt_now(self.data['timezone'])
_LOGGER.debug("now: " + str(now))
today_sun_times = self.get_sunrise_sunset(now)