Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions homeassistant/components/yeelight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
CONF_MODE_MUSIC = "use_music_mode"
CONF_FLOW_PARAMS = "flow_params"
CONF_CUSTOM_EFFECTS = "custom_effects"
CONF_NIGHTLIGHT_SWITCH_TYPE = "nightlight_switch_type"

ATTR_COUNT = "count"
ATTR_ACTION = "action"
Expand All @@ -48,6 +49,8 @@

ACTIVE_MODE_NIGHTLIGHT = "1"

NIGHTLIGHT_SWITCH_TYPE_LIGHT = "light"

SCAN_INTERVAL = timedelta(seconds=30)

YEELIGHT_RGB_TRANSITION = "RGBTransition"
Expand Down Expand Up @@ -84,6 +87,9 @@
vol.Optional(CONF_TRANSITION, default=DEFAULT_TRANSITION): cv.positive_int,
vol.Optional(CONF_MODE_MUSIC, default=False): cv.boolean,
vol.Optional(CONF_SAVE_ON_CHANGE, default=False): cv.boolean,
vol.Optional(CONF_NIGHTLIGHT_SWITCH_TYPE): vol.Any(
NIGHTLIGHT_SWITCH_TYPE_LIGHT
),
vol.Optional(CONF_MODEL): cv.string,
}
)
Expand Down Expand Up @@ -256,10 +262,12 @@ def type(self):

return self._device_type

def turn_on(self, duration=DEFAULT_TRANSITION, light_type=None):
def turn_on(self, duration=DEFAULT_TRANSITION, light_type=None, power_mode=None):
"""Turn on device."""
try:
self.bulb.turn_on(duration=duration, light_type=light_type)
self.bulb.turn_on(
duration=duration, light_type=light_type, power_mode=power_mode
)
except BulbException as ex:
_LOGGER.error("Unable to turn the bulb on: %s", ex)

Expand Down
100 changes: 94 additions & 6 deletions homeassistant/components/yeelight/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
CONF_FLOW_PARAMS,
ATTR_ACTION,
ATTR_COUNT,
NIGHTLIGHT_SWITCH_TYPE_LIGHT,
CONF_NIGHTLIGHT_SWITCH_TYPE,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -177,6 +179,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
_LOGGER.debug("Adding %s", device.name)

custom_effects = _parse_custom_effects(discovery_info[CONF_CUSTOM_EFFECTS])
nl_switch_light = (
discovery_info.get(CONF_NIGHTLIGHT_SWITCH_TYPE) == NIGHTLIGHT_SWITCH_TYPE_LIGHT
)

lights = []

Expand All @@ -193,9 +198,17 @@ def _lights_setup_helper(klass):
elif device_type == BulbType.Color:
_lights_setup_helper(YeelightColorLight)
elif device_type == BulbType.WhiteTemp:
_lights_setup_helper(YeelightWhiteTempLight)
if nl_switch_light and device.is_nightlight_supported:
_lights_setup_helper(YeelightWithNightLight)
_lights_setup_helper(YeelightNightLightMode)
else:
_lights_setup_helper(YeelightWhiteTempWithoutNightlightSwitch)
elif device_type == BulbType.WhiteTempMood:
_lights_setup_helper(YeelightWithAmbientLight)
if nl_switch_light and device.is_nightlight_supported:
_lights_setup_helper(YeelightNightLightMode)
_lights_setup_helper(YeelightWithAmbientAndNightlight)
else:
_lights_setup_helper(YeelightWithAmbientWithoutNightlight)
_lights_setup_helper(YeelightAmbientLight)
else:
_lights_setup_helper(YeelightGenericLight)
Expand Down Expand Up @@ -376,6 +389,10 @@ def _brightness_property(self):
def _power_property(self):
return "power"

@property
def _turn_on_power_mode(self):
return PowerMode.LAST
Comment thread
rytilahti marked this conversation as resolved.

@property
def _predefined_effects(self):
return YEELIGHT_MONO_EFFECT_LIST
Expand Down Expand Up @@ -559,7 +576,11 @@ def turn_on(self, **kwargs) -> None:
if ATTR_TRANSITION in kwargs: # passed kwarg overrides config
duration = int(kwargs.get(ATTR_TRANSITION) * 1000) # kwarg in s

self.device.turn_on(duration=duration, light_type=self.light_type)
self.device.turn_on(
duration=duration,
light_type=self.light_type,
power_mode=self._turn_on_power_mode,
)

if self.config[CONF_MODE_MUSIC] and not self._bulb.music_mode:
try:
Expand Down Expand Up @@ -632,25 +653,92 @@ def _predefined_effects(self):
return YEELIGHT_COLOR_EFFECT_LIST


class YeelightWhiteTempLight(YeelightGenericLight):
class YeelightWhiteTempLightsupport:
"""Representation of a Color Yeelight light."""

@property
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_YEELIGHT_WHITE_TEMP

@property
def _predefined_effects(self):
return YEELIGHT_TEMP_ONLY_EFFECT_LIST


class YeelightWhiteTempWithoutNightlightSwitch(
YeelightGenericLight, YeelightWhiteTempLightsupport
):
"""White temp light, when nightlight switch is not set to light."""

@property
def _brightness_property(self):
return "current_brightness"


class YeelightWithNightLight(YeelightGenericLight, YeelightWhiteTempLightsupport):
"""Representation of a Yeelight with nightlight support.

It represents case when nightlight switch is set to light.
"""

@property
def is_on(self) -> bool:
"""Return true if device is on."""
return super().is_on and not self.device.is_nightlight_enabled

@property
def _turn_on_power_mode(self):
return PowerMode.NORMAL


class YeelightNightLightMode(YeelightGenericLight):
"""Representation of a Yeelight when in nightlight mode."""

@property
def name(self) -> str:
"""Return the name of the device if any."""
return f"{self.device.name} nightlight"

@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return "mdi:weather-night"

@property
def is_on(self) -> bool:
"""Return true if device is on."""
return super().is_on and self.device.is_nightlight_enabled

@property
def _brightness_property(self):
return "nl_br"

@property
def _turn_on_power_mode(self):
return PowerMode.MOONLIGHT

@property
def _predefined_effects(self):
return YEELIGHT_TEMP_ONLY_EFFECT_LIST


class YeelightWithAmbientLight(YeelightWhiteTempLight):
"""Representation of a Yeelight which has ambilight support."""
class YeelightWithAmbientWithoutNightlight(YeelightWhiteTempWithoutNightlightSwitch):
"""Representation of a Yeelight which has ambilight support.

And nightlight switch type is none.
"""

@property
def _power_property(self):
return "main_power"


class YeelightWithAmbientAndNightlight(YeelightWithNightLight):
"""Representation of a Yeelight which has ambilight support.

And nightlight switch type is set to light.
"""

@property
def _power_property(self):
Expand Down