From 1f8324c065c45749738f992a3c8fe48e131aeebb Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 31 Aug 2020 17:42:16 +0200 Subject: [PATCH] more simplifications --- .../circadian_lighting/__init__.py | 28 ++++++++----------- .../circadian_lighting/manifest.json | 2 +- .../circadian_lighting/sensor.py | 3 -- .../circadian_lighting/switch.py | 10 ++----- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/custom_components/circadian_lighting/__init__.py b/custom_components/circadian_lighting/__init__.py index 5d12a934..36824779 100755 --- a/custom_components/circadian_lighting/__init__.py +++ b/custom_components/circadian_lighting/__init__.py @@ -30,8 +30,10 @@ Technical notes: I had to make a lot of assumptions when writing this app import logging from datetime import timedelta -import homeassistant.helpers.config_validation as cv import voluptuous as vol + +import astral +import homeassistant.helpers.config_validation as cv from homeassistant.components.light import ATTR_TRANSITION, VALID_TRANSITION from homeassistant.const import ( CONF_ELEVATION, @@ -51,8 +53,7 @@ from homeassistant.util.color import ( ) from homeassistant.util.dt import get_time_zone from homeassistant.util.dt import now as dt_now - -VERSION = "1.0.13" +from timezonefinder import TimezoneFinder _LOGGER = logging.getLogger(__name__) @@ -61,16 +62,13 @@ CIRCADIAN_LIGHTING_UPDATE_TOPIC = f"{DOMAIN}_update" SUN_EVENT_NOON = "solar_noon" SUN_EVENT_MIDNIGHT = "solar_midnight" -CONF_MIN_CT = "min_colortemp" -DEFAULT_MIN_CT = 2500 -CONF_MAX_CT = "max_colortemp" -DEFAULT_MAX_CT = 5500 +CONF_MIN_CT, DEFAULT_MIN_CT = "min_colortemp", 2500 +CONF_MAX_CT, DEFAULT_MAX_CT = "max_colortemp", 5500 +CONF_INTERVAL, DEFAULT_INTERVAL = "interval", 300 CONF_SUNRISE_OFFSET = "sunrise_offset" CONF_SUNSET_OFFSET = "sunset_offset" CONF_SUNRISE_TIME = "sunrise_time" CONF_SUNSET_TIME = "sunset_time" -CONF_INTERVAL = "interval" -DEFAULT_INTERVAL = 300 DEFAULT_TRANSITION = 60 CONFIG_SCHEMA = vol.Schema( @@ -181,8 +179,6 @@ class CircadianLighting: track_sunset(self.hass, self._update, self._sunset_offset) def get_timezone(self): - from timezonefinder import TimezoneFinder - tf = TimezoneFinder() timezone_string = tf.timezone_at(lng=self._longitude, lat=self._latitude) timezone = get_time_zone(timezone_string) @@ -201,13 +197,13 @@ class CircadianLighting: if self._time["sunrise"] is not None and self._time["sunset"] is not None: if date is None: date = dt_now(self._timezone) - sunrise = date.replace(**self._time_dict("sunrise")) + sunrise = date.replace( + **self._time_dict("sunrise") + ) # XXX: redefine _time_dict to do the replace! sunset = date.replace(**self._time_dict("sunset")) 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" @@ -291,7 +287,7 @@ class CircadianLighting: # We're also (obviously) generating a different parabola for sunrise-sunset # sunrise-sunset parabola - if now_ts > today[SUN_EVENT_SUNRISE] and now_ts < today[SUN_EVENT_SUNSET]: + if today[SUN_EVENT_SUNRISE] < now_ts < today[SUN_EVENT_SUNSET]: h = today[SUN_EVENT_NOON] k = 100 # parabola before solar_noon else after solar_noon @@ -303,7 +299,7 @@ class CircadianLighting: y = 0 # sunset_sunrise parabola - elif now_ts > today[SUN_EVENT_SUNSET] and now_ts < today[SUN_EVENT_SUNRISE]: + elif today[SUN_EVENT_SUNSET] < now_ts < today[SUN_EVENT_SUNRISE]: h = today[SUN_EVENT_MIDNIGHT] k = -100 # parabola before solar_midnight else after solar_midnight diff --git a/custom_components/circadian_lighting/manifest.json b/custom_components/circadian_lighting/manifest.json index 4832a25b..0008ce6b 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": ["timezonefinder==4.2.0"] + "requirements": ["timezonefinder==4.2.0", "astral==1.10.1"] } diff --git a/custom_components/circadian_lighting/sensor.py b/custom_components/circadian_lighting/sensor.py index e5f67282..6bd28b2a 100755 --- a/custom_components/circadian_lighting/sensor.py +++ b/custom_components/circadian_lighting/sensor.py @@ -2,9 +2,6 @@ Circadian Lighting Sensor for Home-Assistant. """ -DEPENDENCIES = ["circadian_lighting"] - -import datetime import logging from homeassistant.helpers.dispatcher import dispatcher_connect diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index d0a781aa..5788da4e 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -2,10 +2,7 @@ Circadian Lighting Switch for Home-Assistant. """ -DEPENDENCIES = ["circadian_lighting", "light"] - import logging -from contextlib import suppress import homeassistant.helpers.config_validation as cv import voluptuous as vol @@ -365,11 +362,8 @@ class CircadianSwitch(SwitchEntity, RestoreEntity): def sleep_state_changed(self, entity_id, from_state, to_state): _LOGGER.debug(f"{entity_id} change from {from_state} to {to_state}") - if ( - to_state.state == self._sleep_state - or from_state.state == self._sleep_state - ): - self._update_switch(self._initial_transition, force=True) + if to_state.state == self._sleep_state or from_state.state == self._sleep_state: + self._update_switch(self._initial_transition, force=True) def disable_state_changed(self, entity_id, from_state, to_state): _LOGGER.debug("{entity_id} change from {from_state} to {to_state}")