From 9e1d140e191f7f8fdc4ea8e77ab6cd8c845eedc1 Mon Sep 17 00:00:00 2001 From: Clayton Nummer Date: Thu, 19 Mar 2020 15:48:52 -0400 Subject: [PATCH 1/3] 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) From 03718f0e3c9bd8b34cacae9de3e3515f45f6338a Mon Sep 17 00:00:00 2001 From: Clayton Nummer Date: Thu, 19 Mar 2020 15:49:30 -0400 Subject: [PATCH 2/3] Version bump for (deprecated) custom updater --- custom_components/circadian_lighting/__init__.py | 2 +- custom_updater.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/circadian_lighting/__init__.py b/custom_components/circadian_lighting/__init__.py index a2d60951..68486800 100644 --- a/custom_components/circadian_lighting/__init__.py +++ b/custom_components/circadian_lighting/__init__.py @@ -48,7 +48,7 @@ from homeassistant.util.dt import now as dt_now from datetime import timedelta -VERSION = '1.0.11b' +VERSION = '1.0.12b' _LOGGER = logging.getLogger(__name__) diff --git a/custom_updater.json b/custom_updater.json index 16b4a1cf..91225bd0 100644 --- a/custom_updater.json +++ b/custom_updater.json @@ -1,7 +1,7 @@ { "circadian_lighting": { - "updated_at": "2020-02-21", - "version": "1.0.11b", + "updated_at": "2020-03-19", + "version": "1.0.12b", "local_location": "/custom_components/circadian_lighting/__init__.py", "remote_location": "https://raw.githubusercontent.com/claytonjn/hass-circadian_lighting/master/custom_components/circadian_lighting/__init__.py", "visit_repo": "https://github.com/claytonjn/hass-circadian_lighting", From 498da0e0db45886a5f5eba6ad72300f61df58efa Mon Sep 17 00:00:00 2001 From: Clayton Nummer Date: Thu, 19 Mar 2020 17:07:27 -0400 Subject: [PATCH 3/3] Use actual timezone to fix configured sunrise/sunset --- .../circadian_lighting/__init__.py | 37 +++++++++---------- .../circadian_lighting/manifest.json | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/custom_components/circadian_lighting/__init__.py b/custom_components/circadian_lighting/__init__.py index 68486800..c6fcecfc 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 now as dt_now +from homeassistant.util.dt import now as dt_now, get_time_zone -from datetime import timedelta +from datetime import datetime, timedelta VERSION = '1.0.12b' @@ -153,19 +153,11 @@ 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 + from timezonefinder import TimezoneFinder + tf = TimezoneFinder() + timezone_string = tf.timezone_at(lng=self.data['longitude'], lat=self.data['latitude']) + timezone = get_time_zone(timezone_string) _LOGGER.debug("Timezone: " + str(timezone)) return timezone @@ -178,7 +170,14 @@ class CircadianLighting(object): solar_noon = sunrise + (sunset - sunrise)/2 solar_midnight = sunset + ((sunrise + timedelta(days=1)) - sunset)/2 else: - location = self.get_astral_location() + 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)) if self.data['sunrise_time'] is not None: if date is None: date = dt_now(self.data['timezone']) @@ -198,10 +197,10 @@ class CircadianLighting(object): if self.data['sunset_offset'] is not None: sunset = sunset + self.data['sunset_offset'] return { - SUN_EVENT_SUNRISE: sunrise, - SUN_EVENT_SUNSET: sunset, - 'solar_noon': solar_noon, - 'solar_midnight': solar_midnight + SUN_EVENT_SUNRISE: sunrise.astimezone(self.data['timezone']), + SUN_EVENT_SUNSET: sunset.astimezone(self.data['timezone']), + 'solar_noon': solar_noon.astimezone(self.data['timezone']), + 'solar_midnight': solar_midnight.astimezone(self.data['timezone']) } def calc_percent(self): diff --git a/custom_components/circadian_lighting/manifest.json b/custom_components/circadian_lighting/manifest.json index b45b39e2..4832a25b 100644 --- a/custom_components/circadian_lighting/manifest.json +++ b/custom_components/circadian_lighting/manifest.json @@ -4,5 +4,5 @@ "documentation": "https://github.com/claytonjn/hass-circadian_lighting", "dependencies": [], "codeowners": ["@claytonjn"], - "requirements": [] + "requirements": ["timezonefinder==4.2.0"] }