From 9e1d140e191f7f8fdc4ea8e77ab6cd8c845eedc1 Mon Sep 17 00:00:00 2001 From: Clayton Nummer Date: Thu, 19 Mar 2020 15:48:52 -0400 Subject: [PATCH 1/9] 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/9] 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/9] 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"] } From eaa5c0e3dd909941bb7f82104c93f7eaf678b184 Mon Sep 17 00:00:00 2001 From: LJU Date: Thu, 14 May 2020 08:26:29 +0200 Subject: [PATCH 4/9] Update SwitchDevice to SwitchEntity --- custom_components/circadian_lighting/switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index f8da28da..2ac2fc9c 100644 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -17,7 +17,7 @@ from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.components.light import ( is_on, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION, VALID_TRANSITION, ATTR_WHITE_VALUE, ATTR_XY_COLOR, DOMAIN as LIGHT_DOMAIN) -from homeassistant.components.switch import SwitchDevice +from homeassistant.components.switch import SwitchEntity from homeassistant.const import ( ATTR_ENTITY_ID, CONF_NAME, CONF_PLATFORM, STATE_ON, SERVICE_TURN_ON) From d7c27cd107d745fef74897fbc1f99745372338e4 Mon Sep 17 00:00:00 2001 From: LJU Date: Thu, 14 May 2020 08:28:41 +0200 Subject: [PATCH 5/9] Update SwitchDevice to SwitchEntity --- custom_components/circadian_lighting/switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 2ac2fc9c..e85f5041 100644 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -105,7 +105,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return False -class CircadianSwitch(SwitchDevice, RestoreEntity): +class CircadianSwitch(SwitchEntity, RestoreEntity): """Representation of a Circadian Lighting switch.""" def __init__(self, hass, cl, name, lights_ct, lights_rgb, lights_xy, lights_brightness, From 5f3700343b6953847de12bdde6c54ea777400c9d Mon Sep 17 00:00:00 2001 From: LJU Date: Thu, 21 May 2020 15:33:49 +0200 Subject: [PATCH 6/9] Update import Update import and add switchdevice for earlier versions --- custom_components/circadian_lighting/switch.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index e85f5041..332c91e1 100644 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -17,6 +17,7 @@ from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.components.light import ( is_on, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION, VALID_TRANSITION, ATTR_WHITE_VALUE, ATTR_XY_COLOR, DOMAIN as LIGHT_DOMAIN) +from homeassistant.components.switch import SwitchDevice from homeassistant.components.switch import SwitchEntity from homeassistant.const import ( ATTR_ENTITY_ID, CONF_NAME, CONF_PLATFORM, STATE_ON, @@ -105,7 +106,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return False -class CircadianSwitch(SwitchEntity, RestoreEntity): +class CircadianSwitch(SwitchEntity, SwitchDevice, RestoreEntity): """Representation of a Circadian Lighting switch.""" def __init__(self, hass, cl, name, lights_ct, lights_rgb, lights_xy, lights_brightness, From 836e2922a6c66db0b290a9a76f10a6be28913b9f Mon Sep 17 00:00:00 2001 From: LJU Date: Fri, 22 May 2020 23:36:16 +0200 Subject: [PATCH 7/9] Update with try --- custom_components/circadian_lighting/switch.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 332c91e1..3e00037e 100644 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -16,9 +16,13 @@ from homeassistant.helpers.event import track_state_change from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.components.light import ( is_on, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION, - VALID_TRANSITION, ATTR_WHITE_VALUE, ATTR_XY_COLOR, DOMAIN as LIGHT_DOMAIN) -from homeassistant.components.switch import SwitchDevice -from homeassistant.components.switch import SwitchEntity + VALID_TRANSITION, ATTR_WHITE_VALUE, ATTR_XY_COLOR, DOMAIN as LIGHT_DOMAIN + +try: + from homeassistant.components.switch import SwitchEntity +except ImportError: + from homeassistant.components.switch import SwitchDevice as SwitchEntity + from homeassistant.const import ( ATTR_ENTITY_ID, CONF_NAME, CONF_PLATFORM, STATE_ON, SERVICE_TURN_ON) @@ -106,7 +110,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return False -class CircadianSwitch(SwitchEntity, SwitchDevice, RestoreEntity): +class CircadianSwitch(SwitchEntity, RestoreEntity): """Representation of a Circadian Lighting switch.""" def __init__(self, hass, cl, name, lights_ct, lights_rgb, lights_xy, lights_brightness, From a2a1c4574454497e2f7852e87ad299fa43e70b31 Mon Sep 17 00:00:00 2001 From: LJU Date: Fri, 22 May 2020 23:36:45 +0200 Subject: [PATCH 8/9] Update switch.py --- custom_components/circadian_lighting/switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index 3e00037e..3b58c1c5 100644 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -16,7 +16,7 @@ from homeassistant.helpers.event import track_state_change from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.components.light import ( is_on, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION, - VALID_TRANSITION, ATTR_WHITE_VALUE, ATTR_XY_COLOR, DOMAIN as LIGHT_DOMAIN + VALID_TRANSITION, ATTR_WHITE_VALUE, ATTR_XY_COLOR, DOMAIN as LIGHT_DOMAIN) try: from homeassistant.components.switch import SwitchEntity From ff4854e7b72db62252b10a773163588299e06cdc Mon Sep 17 00:00:00 2001 From: Clayton Nummer Date: Thu, 11 Jun 2020 13:09:31 -0400 Subject: [PATCH 9/9] Version bump for master --- 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 219c7333..3acf0473 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, get_time_zone from datetime import datetime, timedelta -VERSION = '1.0.13b' +VERSION = '1.0.13' _LOGGER = logging.getLogger(__name__) diff --git a/custom_updater.json b/custom_updater.json index a3c454e0..73a04bb6 100644 --- a/custom_updater.json +++ b/custom_updater.json @@ -1,7 +1,7 @@ { "circadian_lighting": { - "updated_at": "2020-06-03", - "version": "1.0.13b", + "updated_at": "2020-06-11", + "version": "1.0.13", "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",