diff --git a/homeassistant/components/nanoleaf/light.py b/homeassistant/components/nanoleaf/light.py index 6d42110d53e..214b63d6668 100644 --- a/homeassistant/components/nanoleaf/light.py +++ b/homeassistant/components/nanoleaf/light.py @@ -125,8 +125,10 @@ class NanoleafLight(NanoleafEntity, LightEntity): await self._nanoleaf.turn_on() if brightness: await self._nanoleaf.set_brightness(int(brightness / 2.55)) + await self.coordinator.async_refresh() async def async_turn_off(self, **kwargs: Any) -> None: """Instruct the light to turn off.""" transition: float | None = kwargs.get(ATTR_TRANSITION) await self._nanoleaf.turn_off(None if transition is None else int(transition)) + await self.coordinator.async_refresh() diff --git a/tests/components/nanoleaf/__init__.py b/tests/components/nanoleaf/__init__.py index ee614fad173..0e6d571e320 100644 --- a/tests/components/nanoleaf/__init__.py +++ b/tests/components/nanoleaf/__init__.py @@ -1 +1,13 @@ """Tests for the Nanoleaf integration.""" + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Set up the component.""" + config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/nanoleaf/conftest.py b/tests/components/nanoleaf/conftest.py new file mode 100644 index 00000000000..5dae7727eec --- /dev/null +++ b/tests/components/nanoleaf/conftest.py @@ -0,0 +1,49 @@ +"""Common fixtures for Nanoleaf tests.""" + +from collections.abc import AsyncGenerator +from unittest.mock import AsyncMock, patch + +import pytest + +from homeassistant.components.nanoleaf import DOMAIN +from homeassistant.const import CONF_HOST, CONF_TOKEN + +from tests.common import MockConfigEntry + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Mock a Nanoleaf config entry.""" + return MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "10.0.0.10", + CONF_TOKEN: "1234567890abcdef", + }, + ) + + +@pytest.fixture +async def mock_nanoleaf() -> AsyncGenerator[AsyncMock]: + """Mock a Nanoleaf device.""" + with patch( + "homeassistant.components.nanoleaf.Nanoleaf", autospec=True + ) as mock_nanoleaf: + client = mock_nanoleaf.return_value + client.model = "NO_TOUCH" + client.host = "10.0.0.10" + client.serial_no = "ABCDEF123456" + client.color_temperature_max = 4500 + client.color_temperature_min = 1200 + client.is_on = False + client.brightness = 50 + client.color_temperature = 2700 + client.hue = 120 + client.saturation = 50 + client.color_mode = "hs" + client.effect = "Rainbow" + client.effects_list = ["Rainbow", "Sunset", "Nemo"] + client.firmware_version = "4.0.0" + client.name = "Nanoleaf" + client.manufacturer = "Nanoleaf" + yield client diff --git a/tests/components/nanoleaf/snapshots/test_light.ambr b/tests/components/nanoleaf/snapshots/test_light.ambr new file mode 100644 index 00000000000..277c24a7365 --- /dev/null +++ b/tests/components/nanoleaf/snapshots/test_light.ambr @@ -0,0 +1,84 @@ +# serializer version: 1 +# name: test_entities[light.nanoleaf-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'effect_list': list([ + 'Rainbow', + 'Sunset', + 'Nemo', + ]), + 'max_color_temp_kelvin': 4500, + 'max_mireds': 833, + 'min_color_temp_kelvin': 1200, + 'min_mireds': 222, + 'supported_color_modes': list([ + , + , + ]), + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'light', + 'entity_category': None, + 'entity_id': 'light.nanoleaf', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'nanoleaf', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': 'light', + 'unique_id': 'ABCDEF123456', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[light.nanoleaf-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'brightness': None, + 'color_mode': None, + 'color_temp': None, + 'color_temp_kelvin': None, + 'effect': None, + 'effect_list': list([ + 'Rainbow', + 'Sunset', + 'Nemo', + ]), + 'friendly_name': 'Nanoleaf', + 'hs_color': None, + 'max_color_temp_kelvin': 4500, + 'max_mireds': 833, + 'min_color_temp_kelvin': 1200, + 'min_mireds': 222, + 'rgb_color': None, + 'supported_color_modes': list([ + , + , + ]), + 'supported_features': , + 'xy_color': None, + }), + 'context': , + 'entity_id': 'light.nanoleaf', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/nanoleaf/test_light.py b/tests/components/nanoleaf/test_light.py new file mode 100644 index 00000000000..bd852ea81e4 --- /dev/null +++ b/tests/components/nanoleaf/test_light.py @@ -0,0 +1,68 @@ +"""Tests for the Nanoleaf light platform.""" + +from unittest.mock import AsyncMock, patch + +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.components.light import ATTR_EFFECT_LIST, DOMAIN as LIGHT_DOMAIN +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + Platform, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +async def test_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_nanoleaf: AsyncMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test all entities.""" + with patch("homeassistant.components.nanoleaf.PLATFORMS", [Platform.LIGHT]): + await setup_integration(hass, mock_config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +@pytest.mark.parametrize("service", [SERVICE_TURN_ON, SERVICE_TURN_OFF]) +async def test_turning_on_or_off_writes_state( + hass: HomeAssistant, + mock_nanoleaf: AsyncMock, + mock_config_entry: MockConfigEntry, + service: str, +) -> None: + """Test turning on or off the light writes the state.""" + await setup_integration(hass, mock_config_entry) + + assert hass.states.get("light.nanoleaf").attributes[ATTR_EFFECT_LIST] == [ + "Rainbow", + "Sunset", + "Nemo", + ] + + mock_nanoleaf.effects_list = ["Rainbow", "Sunset", "Nemo", "Something Else"] + + await hass.services.async_call( + LIGHT_DOMAIN, + service, + { + ATTR_ENTITY_ID: "light.nanoleaf", + }, + blocking=True, + ) + assert hass.states.get("light.nanoleaf").attributes[ATTR_EFFECT_LIST] == [ + "Rainbow", + "Sunset", + "Nemo", + "Something Else", + ]