more simplifications and renames

This commit is contained in:
Bas Nijholt 2020-08-31 00:39:41 +02:00
commit 875542650e
3 changed files with 45 additions and 59 deletions

View file

@ -122,7 +122,7 @@ def setup(hass, config):
return True
class CircadianLighting(object):
class CircadianLighting:
"""Calculate universal Circadian values."""
def __init__(
@ -196,10 +196,7 @@ class CircadianLighting(object):
)
def get_sunrise_sunset(self, date=None):
if (
self._time["sunrise"] is not None
and self._time["sunset"] is not None
):
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"))
@ -334,9 +331,9 @@ class CircadianLighting(object):
def calc_colortemp(self):
if self._percent > 0:
return (
(self._max_colortemp - self._min_colortemp) * (self._percent / 100)
) + self._min_colortemp
delta = self._max_colortemp - self._min_colortemp
percent = self._percent / 100
return (delta * percent) + self._min_colortemp
else:
return self._min_colortemp
@ -344,19 +341,10 @@ class CircadianLighting(object):
return color_temperature_to_rgb(self._colortemp)
def calc_xy(self):
rgb = self.calc_rgb()
iR = rgb[0]
iG = rgb[1]
iB = rgb[2]
return color_RGB_to_xy(iR, iG, iB)
return color_RGB_to_xy(*self.calc_rgb())
def calc_hs(self):
xy = self.calc_xy()
vX = xy[0]
vY = xy[1]
return color_xy_to_hs(vX, vY)
return color_xy_to_hs(*self.calc_xy())
def _update(self, *args, **kwargs):
"""Update Circadian Values."""

View file

@ -22,14 +22,14 @@ ICON = "mdi:theme-light-dark"
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Circadian Lighting sensor."""
cl = hass.data.get(DOMAIN)
if cl:
cs = CircadianSensor(hass, cl)
add_devices([cs])
circadian_lighting = hass.data.get(DOMAIN)
if circadian_lighting is not None:
sensor = CircadianSensor(hass, circadian_lighting)
add_devices([sensor])
def update(call=None):
"""Update component."""
cl._update()
circadian_lighting._update()
service_name = "values_update"
hass.services.register(DOMAIN, service_name, update)
@ -41,18 +41,18 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class CircadianSensor(Entity):
"""Representation of a Circadian Lighting sensor."""
def __init__(self, hass, cl):
def __init__(self, hass, circadian_lighting):
"""Initialize the Circadian Lighting sensor."""
self._cl = cl
self._circadian_lighting = circadian_lighting
self._name = "Circadian Values"
self._entity_id = "sensor.circadian_values"
self._state = self._cl._percent
self._state = self._circadian_lighting._percent
self._unit_of_measurement = "%"
self._icon = ICON
self._hs_color = self._cl._hs_color
self._colortemp = self._cl._colortemp
self._rgb_color = self._cl._rgb_color
self._xy_color = self._cl._xy_color
self._hs_color = self._circadian_lighting._hs_color
self._colortemp = self._circadian_lighting._colortemp
self._rgb_color = self._circadian_lighting._rgb_color
self._xy_color = self._circadian_lighting._xy_color
# Register callbacks
dispatcher_connect(hass, CIRCADIAN_LIGHTING_UPDATE_TOPIC, self.update_sensor)
@ -90,9 +90,9 @@ class CircadianSensor(Entity):
def device_state_attributes(self):
"""Return the attributes of the sensor."""
return {
"colortemp": self._cl._colortemp,
"rgb_color": self._cl._rgb_color,
"xy_color": self._cl._xy_color,
"colortemp": self._circadian_lighting._colortemp,
"rgb_color": self._circadian_lighting._rgb_color,
"xy_color": self._circadian_lighting._xy_color,
}
def update(self):
@ -100,13 +100,12 @@ class CircadianSensor(Entity):
This is the only method that should fetch new data for Home Assistant.
"""
self._cl.update()
self._circadian_lighting.update()
def update_sensor(self):
if self._cl.data is not None:
self._state = self._cl._percent
self._hs_color = self._cl._hs_color
self._colortemp = self._cl._colortemp
self._rgb_color = self._cl._rgb_color
self._xy_color = self._cl._xy_color
_LOGGER.debug("Circadian Lighting Sensor Updated")
self._state = self._circadian_lighting._percent
self._hs_color = self._circadian_lighting._hs_color
self._colortemp = self._circadian_lighting._colortemp
self._rgb_color = self._circadian_lighting._rgb_color
self._xy_color = self._circadian_lighting._xy_color
_LOGGER.debug("Circadian Lighting Sensor Updated")

View file

@ -106,11 +106,11 @@ PLATFORM_SCHEMA = vol.Schema(
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Circadian Lighting switches."""
cl = hass.data.get(DOMAIN)
if cl:
circadian_lighting = hass.data.get(DOMAIN)
if circadian_lighting is not None:
switch = CircadianSwitch(
hass,
cl,
circadian_lighting,
name=config.get(CONF_NAME),
lights_ct=config.get(CONF_LIGHTS_CT, []),
lights_rgb=config.get(CONF_LIGHTS_RGB, []),
@ -141,7 +141,7 @@ class CircadianSwitch(SwitchEntity, RestoreEntity):
def __init__(
self,
hass,
cl,
circadian_lighting,
name,
lights_ct,
lights_rgb,
@ -161,7 +161,7 @@ class CircadianSwitch(SwitchEntity, RestoreEntity):
):
"""Initialize the Circadian Lighting switch."""
self.hass = hass
self._cl = cl
self._circadian_lighting = circadian_lighting
self._name = name
self._entity_id = "switch." + slugify(f"circadian_lighting {name}")
self._state = None
@ -260,7 +260,11 @@ class CircadianSwitch(SwitchEntity, RestoreEntity):
@property
def _color_temperature(self):
return self._sleep_colortemp if self.is_sleep() else self._cl.data["colortemp"]
return (
self._sleep_colortemp
if self.is_sleep()
else self._circadian_lighting._colortemp
)
def calc_ct(self):
return color_temperature_kelvin_to_mired(self._color_temperature)
@ -279,30 +283,25 @@ class CircadianSwitch(SwitchEntity, RestoreEntity):
return None
elif self.is_sleep():
return self._sleep_brightness
elif self._cl.data["percent"] > 0:
elif self._circadian_lighting._percent > 0:
return self._max_brightness
else:
delta_brightness = self._max_brightness - self._min_brightness
procent = (100 + self._cl.data["percent"]) / 100
procent = (100 + self._circadian_lighting._percent) / 100
return (delta_brightness * procent) + self._min_brightness
def _update_switch(self, transition=None, force=False):
if self._once_only and not force:
return
if self._cl.data is not None:
self._hs_color = self.calc_hs()
self._brightness = self.calc_brightness()
_LOGGER.debug(f"{self._name} Switch Updated")
self._hs_color = self.calc_hs()
self._brightness = self.calc_brightness()
_LOGGER.debug(f"{self._name} Switch Updated")
self.adjust_lights(self._lights, transition)
def should_adjust(self):
if self._state is not True:
_LOGGER.debug(f"{self._name} off - not adjusting")
return False
elif self._cl.data is None:
_LOGGER.debug(f"{self._name} could not retrieve Circadian Lighting data")
return False
elif (
self._disable_entity is not None
and self.hass.states.get(self._disable_entity).state == self._disable_state
@ -317,7 +316,7 @@ class CircadianSwitch(SwitchEntity, RestoreEntity):
return
if transition is None:
transition = self._cl.data["transition"]
transition = self._circadian_lighting._transition
for light in lights:
if not is_on(self.hass, light):