mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Add Nanoleaf 4D screen mirroring modes as light effects (#187408)
This commit is contained in:
@@ -4,7 +4,13 @@ from collections.abc import Callable, Coroutine
|
||||
from functools import wraps
|
||||
from typing import Any, Concatenate
|
||||
|
||||
from aionanoleaf2 import AuthError, InvalidEffect, NanoleafError, NoAuthToken
|
||||
from aionanoleaf2 import (
|
||||
AuthError,
|
||||
InvalidEffect,
|
||||
InvalidEmersion,
|
||||
NanoleafError,
|
||||
NoAuthToken,
|
||||
)
|
||||
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
@@ -42,7 +48,7 @@ def async_handle_nanoleaf_errors[EntityT: NanoleafEntity, **P](
|
||||
async def wrapper(self: EntityT, /, *args: P.args, **kwargs: P.kwargs) -> None:
|
||||
try:
|
||||
await func(self, *args, **kwargs)
|
||||
except InvalidEffect as err:
|
||||
except (InvalidEffect, InvalidEmersion) as err:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="effect_not_supported",
|
||||
|
||||
@@ -21,6 +21,8 @@ from .coordinator import NanoleafConfigEntry, NanoleafCoordinator
|
||||
from .entity import NanoleafEntity, async_handle_nanoleaf_errors
|
||||
|
||||
RESERVED_EFFECTS = ("*Solid*", "*Static*", "*Dynamic*")
|
||||
# Reported as the active effect while a Screen Mirroring mode of a Nanoleaf 4D runs.
|
||||
EMERSION_EFFECT = "*Emersion*"
|
||||
DEFAULT_NAME = "Nanoleaf"
|
||||
|
||||
|
||||
@@ -69,13 +71,25 @@ class NanoleafLight(NanoleafEntity, LightEntity):
|
||||
# These reserved effects are implicitly set and are not in the effect_list.
|
||||
# https://forum.nanoleaf.me/docs/openapi#_byoot0bams8f
|
||||
effect = self._nanoleaf.effect
|
||||
if effect == EMERSION_EFFECT:
|
||||
# Screen Mirroring modes (1D, 2D, 3D, 4D) are offered as effects.
|
||||
return self._nanoleaf.selected_emersion
|
||||
return None if not effect or effect in RESERVED_EFFECTS else effect
|
||||
|
||||
@property
|
||||
@override
|
||||
def effect_list(self) -> list[str]:
|
||||
"""Return the list of supported effects."""
|
||||
return self._nanoleaf.effects_list
|
||||
"""Return the list of supported effects.
|
||||
|
||||
Devices with Screen Mirroring (Nanoleaf 4D) additionally offer their
|
||||
modes (1D, 2D, 3D, 4D) as effects. A regular effect with the same name
|
||||
takes precedence.
|
||||
"""
|
||||
effects = self._nanoleaf.effects_list
|
||||
return [
|
||||
*effects,
|
||||
*(mode for mode in self._nanoleaf.emersion_list if mode not in effects),
|
||||
]
|
||||
|
||||
@property
|
||||
@override
|
||||
@@ -117,7 +131,11 @@ class NanoleafLight(NanoleafEntity, LightEntity):
|
||||
translation_key="invalid_effect",
|
||||
translation_placeholders={"effect": str(effect)},
|
||||
)
|
||||
await self._nanoleaf.set_effect(effect)
|
||||
if effect in self._nanoleaf.effects_list:
|
||||
await self._nanoleaf.set_effect(effect)
|
||||
else:
|
||||
# Not a regular effect, so it is a Screen Mirroring mode.
|
||||
await self._nanoleaf.set_emersion(effect)
|
||||
elif hs_color:
|
||||
hue, saturation = hs_color
|
||||
await self._nanoleaf.set_hue(int(hue))
|
||||
|
||||
@@ -42,6 +42,8 @@ async def mock_nanoleaf() -> AsyncGenerator[AsyncMock]:
|
||||
client.color_mode = "hs"
|
||||
client.effect = "Rainbow"
|
||||
client.effects_list = ["Rainbow", "Sunset", "Nemo"]
|
||||
client.emersion_list = []
|
||||
client.selected_emersion = None
|
||||
client.firmware_version = "4.0.0"
|
||||
client.name = "Nanoleaf"
|
||||
client.manufacturer = "Nanoleaf"
|
||||
|
||||
@@ -6,6 +6,7 @@ from aionanoleaf2 import (
|
||||
AuthError,
|
||||
EndpointNotFoundError,
|
||||
InvalidEffect,
|
||||
InvalidEmersion,
|
||||
InvalidResponseError,
|
||||
InvalidToken,
|
||||
NanoleafConnectionError,
|
||||
@@ -232,3 +233,116 @@ async def test_turn_on_known_effect(
|
||||
await _call(hass, SERVICE_TURN_ON, **{ATTR_EFFECT: "Sunset"})
|
||||
|
||||
mock_nanoleaf.set_effect.assert_awaited_once_with("Sunset")
|
||||
|
||||
|
||||
EMERSION_MODES = ["1D", "2D", "3D", "4D"]
|
||||
|
||||
|
||||
async def test_effect_list_includes_emersion_modes(
|
||||
hass: HomeAssistant,
|
||||
mock_nanoleaf: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the Screen Mirroring modes of a Nanoleaf 4D are offered as effects."""
|
||||
mock_nanoleaf.emersion_list = EMERSION_MODES
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert hass.states.get("light.nanoleaf").attributes[ATTR_EFFECT_LIST] == [
|
||||
"Rainbow",
|
||||
"Sunset",
|
||||
"Nemo",
|
||||
*EMERSION_MODES,
|
||||
]
|
||||
|
||||
|
||||
async def test_effect_list_does_not_duplicate_effect_named_like_a_mode(
|
||||
hass: HomeAssistant,
|
||||
mock_nanoleaf: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test a regular effect named like a mode is listed once."""
|
||||
mock_nanoleaf.effects_list = ["Rainbow", "2D"]
|
||||
mock_nanoleaf.emersion_list = EMERSION_MODES
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert hass.states.get("light.nanoleaf").attributes[ATTR_EFFECT_LIST] == [
|
||||
"Rainbow",
|
||||
"2D",
|
||||
"1D",
|
||||
"3D",
|
||||
"4D",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("selected_emersion", "expected_effect"),
|
||||
[("3D", "3D"), (None, None)],
|
||||
)
|
||||
async def test_effect_reports_active_emersion_mode(
|
||||
hass: HomeAssistant,
|
||||
mock_nanoleaf: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
selected_emersion: str | None,
|
||||
expected_effect: str | None,
|
||||
) -> None:
|
||||
"""Test the active Screen Mirroring mode is reported as the effect."""
|
||||
mock_nanoleaf.is_on = True
|
||||
mock_nanoleaf.effect = "*Emersion*"
|
||||
mock_nanoleaf.emersion_list = EMERSION_MODES
|
||||
mock_nanoleaf.selected_emersion = selected_emersion
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
state = hass.states.get("light.nanoleaf")
|
||||
assert state is not None
|
||||
assert state.attributes.get(ATTR_EFFECT) == expected_effect
|
||||
|
||||
|
||||
async def test_turn_on_emersion_mode(
|
||||
hass: HomeAssistant,
|
||||
mock_nanoleaf: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test a Screen Mirroring mode is activated instead of a regular effect."""
|
||||
mock_nanoleaf.emersion_list = EMERSION_MODES
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await _call(hass, SERVICE_TURN_ON, **{ATTR_EFFECT: "2D"})
|
||||
|
||||
mock_nanoleaf.set_emersion.assert_awaited_once_with("2D")
|
||||
mock_nanoleaf.set_effect.assert_not_called()
|
||||
|
||||
|
||||
async def test_turn_on_emersion_mode_is_rejected_without_screen_mirroring(
|
||||
hass: HomeAssistant,
|
||||
mock_nanoleaf: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test a mode name is not accepted by devices without Screen Mirroring."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
with pytest.raises(ServiceValidationError):
|
||||
await _call(hass, SERVICE_TURN_ON, **{ATTR_EFFECT: "2D"})
|
||||
|
||||
mock_nanoleaf.set_emersion.assert_not_called()
|
||||
mock_nanoleaf.set_effect.assert_not_called()
|
||||
|
||||
|
||||
async def test_turn_on_emersion_mode_rejected_by_library_is_a_validation_error(
|
||||
hass: HomeAssistant,
|
||||
mock_nanoleaf: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test InvalidEmersion from the library maps to a validation error."""
|
||||
mock_nanoleaf.emersion_list = EMERSION_MODES
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
mock_nanoleaf.set_emersion.side_effect = InvalidEmersion
|
||||
|
||||
with pytest.raises(ServiceValidationError) as exc_info:
|
||||
await _call(hass, SERVICE_TURN_ON, **{ATTR_EFFECT: "4D"})
|
||||
|
||||
assert exc_info.value.translation_domain == DOMAIN
|
||||
assert exc_info.value.translation_key == "effect_not_supported"
|
||||
assert isinstance(exc_info.value.__cause__, InvalidEmersion)
|
||||
|
||||
Reference in New Issue
Block a user