mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-11 22:34:04 +02:00
add @log decorator
This commit is contained in:
parent
080aeed541
commit
063a2386af
3 changed files with 36 additions and 27 deletions
|
|
@ -29,6 +29,7 @@ Technical notes: I had to make a lot of assumptions when writing this app
|
|||
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
import inspect
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
|
|
@ -99,6 +100,26 @@ CONFIG_SCHEMA = vol.Schema(
|
|||
)
|
||||
|
||||
|
||||
def log(with_return=False, logger=_LOGGER):
|
||||
def _log(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
func_args = inspect.signature(func).bind(*args, **kwargs).arguments
|
||||
key_value_pairs = (
|
||||
f"{k}={v!r}" for k, v in func_args.items() if k != "self"
|
||||
)
|
||||
func_args_str = ", ".join(key_value_pairs)
|
||||
out = f"{func.__qualname__}({func_args_str})"
|
||||
result = func(*args, **kwargs)
|
||||
if with_return:
|
||||
out += f" -> {result}"
|
||||
logger.debug(out)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
||||
return _log
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Set up the Circadian Lighting component."""
|
||||
conf = config[DOMAIN]
|
||||
|
|
@ -177,11 +198,11 @@ class CircadianLighting:
|
|||
elif which == "sunset":
|
||||
track_sunset(self.hass, self._update, self._sunset_offset)
|
||||
|
||||
@log(with_return=True)
|
||||
def get_timezone(self):
|
||||
tf = TimezoneFinder()
|
||||
timezone_string = tf.timezone_at(lng=self._longitude, lat=self._latitude)
|
||||
timezone = get_time_zone(timezone_string)
|
||||
_LOGGER.debug("Timezone: " + str(timezone))
|
||||
return timezone
|
||||
|
||||
def _time_dict(self, key):
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ from homeassistant.helpers.entity import Entity
|
|||
from custom_components.circadian_lighting import (
|
||||
CIRCADIAN_LIGHTING_UPDATE_TOPIC,
|
||||
DOMAIN,
|
||||
log,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
|
@ -99,10 +100,10 @@ class CircadianSensor(Entity):
|
|||
"""
|
||||
self._circadian_lighting.update()
|
||||
|
||||
@log(logger=_LOGGER)
|
||||
def update_sensor(self):
|
||||
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")
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ from homeassistant.util.color import (
|
|||
from custom_components.circadian_lighting import (
|
||||
CIRCADIAN_LIGHTING_UPDATE_TOPIC,
|
||||
DOMAIN,
|
||||
log,
|
||||
)
|
||||
|
||||
try:
|
||||
|
|
@ -240,17 +241,13 @@ class CircadianSwitch(SwitchEntity, RestoreEntity):
|
|||
self._hs_color = None
|
||||
self._brightness = None
|
||||
|
||||
@log(with_return=True, logger=_LOGGER)
|
||||
def is_sleep(self):
|
||||
is_sleep = (
|
||||
return (
|
||||
self._sleep_entity is not None
|
||||
and self.hass.states.get(self._sleep_entity).state in self._sleep_state
|
||||
)
|
||||
if is_sleep:
|
||||
_LOGGER.debug(f"{self._name} in Sleep mode")
|
||||
|
||||
return is_sleep
|
||||
|
||||
@property
|
||||
def _color_temperature(self):
|
||||
return (
|
||||
self._sleep_colortemp
|
||||
|
|
@ -259,10 +256,10 @@ class CircadianSwitch(SwitchEntity, RestoreEntity):
|
|||
)
|
||||
|
||||
def calc_ct(self):
|
||||
return color_temperature_kelvin_to_mired(self._color_temperature)
|
||||
return color_temperature_kelvin_to_mired(self._color_temperature())
|
||||
|
||||
def calc_rgb(self):
|
||||
return color_temperature_to_rgb(self._color_temperature)
|
||||
return color_temperature_to_rgb(self._color_temperature())
|
||||
|
||||
def calc_xy(self):
|
||||
return color_RGB_to_xy(*self.calc_rgb())
|
||||
|
|
@ -282,27 +279,26 @@ class CircadianSwitch(SwitchEntity, RestoreEntity):
|
|||
procent = (100 + self._circadian_lighting._percent) / 100
|
||||
return (delta_brightness * procent) + self._min_brightness
|
||||
|
||||
@log(logger=_LOGGER)
|
||||
def _update_switch(self, lights=None, transition=None, force=False):
|
||||
if self._once_only and not force:
|
||||
return
|
||||
self._hs_color = self.calc_hs()
|
||||
self._brightness = self.calc_brightness()
|
||||
_LOGGER.debug(f"{self._name} Switch Updated")
|
||||
self._adjust_lights(lights or self._lights, transition)
|
||||
|
||||
@property
|
||||
@log(with_return=True, logger=_LOGGER)
|
||||
def _is_disabled(self):
|
||||
return (
|
||||
self._disable_entity is not None
|
||||
and self.hass.states.get(self._disable_entity).state in self._disable_state
|
||||
)
|
||||
|
||||
@log(with_return=True, logger=_LOGGER)
|
||||
def _should_adjust(self):
|
||||
if self._state is not True:
|
||||
_LOGGER.debug(f"{self._name} off - not adjusting")
|
||||
return False
|
||||
elif self._is_disabled:
|
||||
_LOGGER.debug(f"{self._name} disabled by {self._disable_entity}")
|
||||
elif self._is_disabled():
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
|
@ -353,26 +349,17 @@ class CircadianSwitch(SwitchEntity, RestoreEntity):
|
|||
msg = ", ".join(key_value_strings)
|
||||
_LOGGER.debug(f"{light} {which} Adjusted - {msg}")
|
||||
|
||||
@log(with_return=True, logger=_LOGGER)
|
||||
def light_state_changed(self, entity_id, from_state, to_state):
|
||||
if to_state.state == "on" and from_state.state != "on":
|
||||
_LOGGER.debug(
|
||||
f"light_state_changed for {self._name}: {entity_id} "
|
||||
f"change from {from_state} to {to_state}"
|
||||
)
|
||||
self._update_switch([entity_id], self._initial_transition, force=True)
|
||||
|
||||
@log(with_return=True, logger=_LOGGER)
|
||||
def sleep_state_changed(self, entity_id, from_state, to_state):
|
||||
if to_state.state in self._sleep_state or from_state.state in self._sleep_state:
|
||||
_LOGGER.debug(
|
||||
f"sleep_state_changed for {self._name}: {entity_id} "
|
||||
f"change from {from_state} to {to_state}"
|
||||
)
|
||||
self._update_switch(transition=self._initial_transition, force=True)
|
||||
|
||||
@log(with_return=True, logger=_LOGGER)
|
||||
def disable_state_changed(self, entity_id, from_state, to_state):
|
||||
if from_state.state in self._disable_state:
|
||||
_LOGGER.debug(
|
||||
f"disable_state_changed for {self._name}: {entity_id} "
|
||||
f"change from {from_state} to {to_state}"
|
||||
)
|
||||
self._update_switch(transition=self._initial_transition, force=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue