diff --git a/custom_components/circadian_lighting/__init__.py b/custom_components/circadian_lighting/__init__.py index 62667398..3acf0473 100644 --- a/custom_components/circadian_lighting/__init__.py +++ b/custom_components/circadian_lighting/__init__.py @@ -44,11 +44,11 @@ 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, get_time_zone from datetime import datetime, timedelta -VERSION = '1.0.11' +VERSION = '1.0.13' _LOGGER = logging.getLogger(__name__) @@ -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,11 +153,18 @@ class CircadianLighting(object): else: track_sunset(self.hass, self._update, self.data['sunset_offset']) + def get_timezone(self): + 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 + 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 @@ -172,15 +180,13 @@ class CircadianLighting(object): _LOGGER.debug("Astral location: " + str(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) @@ -191,15 +197,14 @@ 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): - 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) 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"] } diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index f8da28da..3b58c1c5 100644 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -17,7 +17,12 @@ 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 + +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) @@ -105,7 +110,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, diff --git a/custom_updater.json b/custom_updater.json index 867cd59d..73a04bb6 100644 --- a/custom_updater.json +++ b/custom_updater.json @@ -1,7 +1,7 @@ { "circadian_lighting": { - "updated_at": "2020-03-07", - "version": "1.0.11", + "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",