diff --git a/custom_components/circadian_lighting/__init__.py b/custom_components/circadian_lighting/__init__.py index 2d2f8253..11342f8f 100755 --- a/custom_components/circadian_lighting/__init__.py +++ b/custom_components/circadian_lighting/__init__.py @@ -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): diff --git a/custom_components/circadian_lighting/sensor.py b/custom_components/circadian_lighting/sensor.py index 6bd28b2a..082250d3 100755 --- a/custom_components/circadian_lighting/sensor.py +++ b/custom_components/circadian_lighting/sensor.py @@ -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") diff --git a/custom_components/circadian_lighting/switch.py b/custom_components/circadian_lighting/switch.py index f3be1dd3..729046c4 100755 --- a/custom_components/circadian_lighting/switch.py +++ b/custom_components/circadian_lighting/switch.py @@ -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)