This commit is contained in:
Bas Nijholt 2020-09-29 23:39:46 +02:00
commit 1c88ecc572
3 changed files with 41 additions and 19 deletions

View file

@ -40,8 +40,7 @@ UNDO_UPDATE_LISTENER = "undo_update_listener"
NONE_STR = "None"
SERVICE_APPLY = "apply"
CONF_COLORS_ONLY = "colors_only"
CONF_ON_LIGHTS_ONLY = "on_lights_only"
CONF_TURN_ON_LIGHTS = "turn_on_lights"
TURNING_OFF_DELAY = 5

View file

@ -10,9 +10,15 @@ apply:
transition:
description: Transition of the lights.
example: 10
colors_only:
description: Only change the color of the lights and leave the brightness as is.
example: false
on_lights_only:
description: Only adjust the lights that are already on, otherwise turn the lights on.
adapt_brightness:
description: "Adapt the 'brightness', default: true"
example: true
adapt_color_temp:
description: "Adapt the 'color_temp', default: true"
example: true
adapt_rgb_color:
description: "Adapt the 'rgb_color', default: true"
example: true
turn_on_lights:
description: "Turn on the lights that are off, default: false"
example: false

View file

@ -62,7 +62,6 @@ from .const import (
CONF_ADAPT_BRIGHTNESS,
CONF_ADAPT_COLOR_TEMP,
CONF_ADAPT_RGB_COLOR,
CONF_COLORS_ONLY,
CONF_DISABLE_ENTITY,
CONF_DISABLE_STATE,
CONF_INITIAL_TRANSITION,
@ -72,7 +71,6 @@ from .const import (
CONF_MAX_COLOR_TEMP,
CONF_MIN_BRIGHTNESS,
CONF_MIN_COLOR_TEMP,
CONF_ON_LIGHTS_ONLY,
CONF_ONLY_ONCE,
CONF_PREFER_RGB_COLOR,
CONF_SLEEP_BRIGHTNESS,
@ -84,6 +82,7 @@ from .const import (
CONF_SUNSET_OFFSET,
CONF_SUNSET_TIME,
CONF_TRANSITION,
CONF_TURN_ON_LIGHTS,
DOMAIN,
EXTRA_VALIDATION,
ICON,
@ -119,10 +118,12 @@ async def handle_apply(switch, service_call):
await switch._adapt_light( # pylint: disable=protected-access
light,
data[CONF_TRANSITION],
data[CONF_COLORS_ONLY],
data[CONF_ADAPT_BRIGHTNESS],
data[CONF_ADAPT_COLOR_TEMP],
data[CONF_ADAPT_RGB_COLOR],
)
for light in data[CONF_LIGHTS]
if not data[CONF_ON_LIGHTS_ONLY] or is_on(switch.hass, light)
if data[CONF_TURN_ON_LIGHTS] or is_on(switch.hass, light)
]
if tasks:
await asyncio.wait(tasks)
@ -149,8 +150,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
CONF_TRANSITION,
default=switch._initial_transition, # pylint: disable=protected-access
): VALID_TRANSITION,
vol.Optional(CONF_COLORS_ONLY, default=False): cv.boolean,
vol.Optional(CONF_ON_LIGHTS_ONLY, default=False): cv.boolean,
vol.Optional(CONF_ADAPT_BRIGHTNESS, default=True): cv.boolean,
vol.Optional(CONF_ADAPT_COLOR_TEMP, default=True): cv.boolean,
vol.Optional(CONF_ADAPT_RGB_COLOR, default=True): cv.boolean,
vol.Optional(CONF_TURN_ON_LIGHTS, default=False): cv.boolean,
},
handle_apply,
)
@ -482,28 +485,42 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
and self.hass.states.get(self._disable_entity).state in self._disable_state
)
async def _adapt_light(self, light, transition, colors_only=False):
async def _adapt_light(
self,
light,
transition=None,
adapt_brightness=None,
adapt_color_temp=None,
adapt_rgb_color=None,
):
service_data = {ATTR_ENTITY_ID: light}
features = self._supported_features(light)
if transition is None:
transition = self._transition
if adapt_brightness is None:
adapt_brightness = self._adapt_brightness
if adapt_color_temp is None:
adapt_color_temp = self._adapt_color_temp
if adapt_rgb_color is None:
adapt_rgb_color = self._adapt_rgb_color
if "transition" in features:
if transition is None:
transition = self._transition
service_data[ATTR_TRANSITION] = transition
if "brightness" in features and self._adapt_brightness and not colors_only:
if "brightness" in features and adapt_brightness:
service_data[ATTR_BRIGHTNESS_PCT] = self._brightness
if (
"color_temp" in features
and self._adapt_color_temp
and adapt_color_temp
and not (self._prefer_rgb_color and "color" in features)
):
attributes = self.hass.states.get(light).attributes
min_mireds, max_mireds = attributes["min_mireds"], attributes["max_mireds"]
color_temp_mired = max(min(self._color_temp_mired, max_mireds), min_mireds)
service_data[ATTR_COLOR_TEMP] = color_temp_mired
elif "color" in features and self._adapt_rgb_color:
elif "color" in features and adapt_rgb_color:
service_data[ATTR_RGB_COLOR] = self._rgb_color
_LOGGER.debug(