diff --git a/README.md b/README.md index fa4425e0..5b2d148c 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ adaptive_lighting: | option | description | required | default | type | | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | -------------- | ------- | | `name` | The name to use when displaying this switch. | False | default | string | +| `include_config_in_attributes` | When set to `true`, will list all of the below options as attributes on the switch in Home Assistant. | False | False | boolean | | `lights` | List of light entities for Adaptive Lighting to control (may be empty). | False | list | [] | | `prefer_rgb_color` | Whether to use RGB color adjustment instead of native light color temperature. | False | False | boolean | | `initial_transition` | How long the first transition is when the lights go from `off` to `on`. | False | 1 | time | diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 62c71fa2..e9d6f01c 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -17,6 +17,10 @@ CONF_DETECT_NON_HA_CHANGES, DEFAULT_DETECT_NON_HA_CHANGES = ( "detect_non_ha_changes", False, ) +CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, DEFAULT_INCLUDE_CONFIG_IN_ATTRIBUTES = ( + "include_config_in_attributes", + False, +) CONF_INITIAL_TRANSITION, DEFAULT_INITIAL_TRANSITION = "initial_transition", 1 CONF_SLEEP_TRANSITION, DEFAULT_SLEEP_TRANSITION = "sleep_transition", 1 CONF_INTERVAL, DEFAULT_INTERVAL = "interval", 90 @@ -73,6 +77,7 @@ def int_between(min_int, max_int): VALIDATION_TUPLES = [ (CONF_LIGHTS, DEFAULT_LIGHTS, cv.entity_ids), (CONF_PREFER_RGB_COLOR, DEFAULT_PREFER_RGB_COLOR, bool), + (CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, DEFAULT_INCLUDE_CONFIG_IN_ATTRIBUTES, bool), (CONF_INITIAL_TRANSITION, DEFAULT_INITIAL_TRANSITION, VALID_TRANSITION), (CONF_SLEEP_TRANSITION, DEFAULT_SLEEP_TRANSITION, VALID_TRANSITION), (CONF_TRANSITION, DEFAULT_TRANSITION, VALID_TRANSITION), diff --git a/custom_components/adaptive_lighting/strings.json b/custom_components/adaptive_lighting/strings.json index 74099754..18b5a438 100644 --- a/custom_components/adaptive_lighting/strings.json +++ b/custom_components/adaptive_lighting/strings.json @@ -21,6 +21,7 @@ "data": { "lights": "lights", "initial_transition": "initial_transition: When lights turn 'off' to 'on'. (seconds)", + "include_config_in_attributes": "include_config_in_attributes: All config options will be listed as attributes under the adaptive-lighting switch this integration creates. (default: false)", "sleep_transition": "sleep_transition: When 'sleep_state' changes. (seconds)", "interval": "interval: Time between switch updates. (seconds)", "max_brightness": "max_brightness: Highest brightness of lights during a cycle. (%)", diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py old mode 100755 new mode 100644 index ac1acc96..5c228ade --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -100,6 +100,7 @@ from .const import ( ATTR_TURN_ON_OFF_LISTENER, CONF_ADAPT_DELAY, CONF_DETECT_NON_HA_CHANGES, + CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, CONF_INITIAL_TRANSITION, CONF_INTERVAL, CONF_LIGHTS, @@ -568,6 +569,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): self._lights = data[CONF_LIGHTS] self._detect_non_ha_changes = data[CONF_DETECT_NON_HA_CHANGES] + self._include_config_in_attributes = data[CONF_INCLUDE_CONFIG_IN_ATTRIBUTES] self._initial_transition = data[CONF_INITIAL_TRANSITION] self._sleep_transition = data[CONF_SLEEP_TRANSITION] self._interval = data[CONF_INTERVAL] @@ -623,6 +625,16 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): # Set in self._update_attrs_and_maybe_adapt_lights self._settings: dict[str, Any] = {} + self._config: dict[str, Any] = {} + if self._include_config_in_attributes: + attrdata = deepcopy(data) + for k, v in attrdata.items(): + if isinstance(v, (datetime.date, datetime.datetime)): + attrdata[k] = v.isoformat() + if isinstance(v, (datetime.timedelta)): + attrdata[k] = v.total_seconds() + self._config.update(attrdata) + # Set and unset tracker in async_turn_on and async_turn_off self.remove_listeners = [] _LOGGER.debug( @@ -715,14 +727,18 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): @property def extra_state_attributes(self) -> dict[str, Any]: """Return the attributes of the switch.""" + extra_state_attributes = {"configuration": self._config} if not self.is_on: - return {key: None for key in self._settings} - manual_control = [ + for key in self._settings: + extra_state_attributes[key] = None + return extra_state_attributes + extra_state_attributes["manual_control"] = [ light for light in self._lights if self.turn_on_off_listener.manual_control.get(light) ] - return dict(self._settings, manual_control=manual_control) + extra_state_attributes.update(self._settings) + return extra_state_attributes def create_context( self, which: str = "default", parent: Context | None = None @@ -895,8 +911,10 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): context.id, ) assert self.is_on - self._settings = self._sun_light_settings.get_settings( - self.sleep_mode_switch.is_on, transition + self._settings.update( + self._sun_light_settings.get_settings( + self.sleep_mode_switch.is_on, transition + ) ) self.async_write_ha_state() if lights is None: diff --git a/custom_components/adaptive_lighting/translations/en.json b/custom_components/adaptive_lighting/translations/en.json index be556ec0..8b2ba140 100644 --- a/custom_components/adaptive_lighting/translations/en.json +++ b/custom_components/adaptive_lighting/translations/en.json @@ -22,6 +22,7 @@ "data": { "lights": "lights", "initial_transition": "initial_transition: When lights turn 'off' to 'on'. (seconds)", + "include_config_in_attributes": "include_config_in_attributes: All config options will be listed as attributes under the adaptive-lighting switch this integration creates. (default: false)", "sleep_transition": "sleep_transition: When 'sleep_state' changes. (seconds)", "interval": "interval: Time between switch updates. (seconds)", "max_brightness": "max_brightness: Highest brightness of lights during a cycle. (%)",