From 9e1d140e191f7f8fdc4ea8e77ab6cd8c845eedc1 Mon Sep 17 00:00:00 2001 From: Clayton Nummer Date: Thu, 19 Mar 2020 15:48:52 -0400 Subject: [PATCH] Use the same timezone for "now" time and configured lat/long --- .../circadian_lighting/__init__.py | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/custom_components/circadian_lighting/__init__.py b/custom_components/circadian_lighting/__init__.py index 360819cd..a2d60951 100644 --- a/custom_components/circadian_lighting/__init__.py +++ b/custom_components/circadian_lighting/__init__.py @@ -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)