Show config options on the switch as attributes with include_config_in_attributes option (#445)

* Switch attributes now show the config settings.

`sunset_time`, `sunrise_time`, `max_sunrise_time`, and `min_sunset_time` show 'null' when not overridden - TODO

* fixed basnijholt's requested issues

* Run pre-commit

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* 'configuration' attr holds list of config options

returned the `=` from basnijholt's review.

* Small style changes

* Add include_config_in_attributes to en.json

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bas Nijholt <bas@nijho.lt>
This commit is contained in:
Benjamin Auquite 2023-03-25 14:43:51 -05:00 committed by GitHub
commit 231e7c197d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 5 deletions

View file

@ -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 |

View file

@ -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),

View file

@ -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. (%)",

28
custom_components/adaptive_lighting/switch.py Executable file → Normal file
View file

@ -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:

View file

@ -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. (%)",