Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add light tests for Niko Home Control #133750

Merged
merged 3 commits into from
Dec 22, 2024
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
1 change: 1 addition & 0 deletions homeassistant/components/niko_home_control/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def __init__(
if action.is_dimmable:
self._attr_color_mode = ColorMode.BRIGHTNESS
self._attr_supported_color_modes = {ColorMode.BRIGHTNESS}
self._attr_brightness = round(action.state * 2.55)

def turn_on(self, **kwargs: Any) -> None:
"""Instruct the light to turn on."""
Expand Down
44 changes: 40 additions & 4 deletions tests/components/niko_home_control/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections.abc import Generator
from unittest.mock import AsyncMock, patch

from nhc.light import NHCLight
import pytest

from homeassistant.components.niko_home_control.const import DOMAIN
Expand All @@ -22,22 +23,57 @@ def mock_setup_entry() -> Generator[AsyncMock]:


@pytest.fixture
def mock_niko_home_control_connection() -> Generator[AsyncMock]:
def light() -> NHCLight:
"""Return a light mock."""
mock = AsyncMock(spec=NHCLight)
mock.id = 1
mock.type = 1
mock.is_dimmable = False
mock.name = "light"
mock.suggested_area = "room"
mock.state = 100
return mock


@pytest.fixture
def dimmable_light() -> NHCLight:
"""Return a dimmable light mock."""
mock = AsyncMock(spec=NHCLight)
mock.id = 2
mock.type = 2
mock.is_dimmable = True
mock.name = "dimmable light"
mock.suggested_area = "room"
mock.state = 100
return mock


@pytest.fixture
def mock_niko_home_control_connection(
light: NHCLight, dimmable_light: NHCLight
) -> Generator[AsyncMock]:
"""Mock a NHC client."""
with (
patch(
"homeassistant.components.niko_home_control.config_flow.NHCController",
"homeassistant.components.niko_home_control.NHCController",
autospec=True,
) as mock_client,
patch(
"homeassistant.components.niko_home_control.config_flow.NHCController",
new=mock_client,
),
):
client = mock_client.return_value
client.return_value = True
client.lights = [light, dimmable_light]
yield client


@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
return MockConfigEntry(
domain=DOMAIN, title="Niko Home Control", data={CONF_HOST: "192.168.0.123"}
domain=DOMAIN,
title="Niko Home Control",
data={CONF_HOST: "192.168.0.123"},
entry_id="01JFN93M7KRA38V5AMPCJ2JYYV",
)
112 changes: 112 additions & 0 deletions tests/components/niko_home_control/snapshots/test_light.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# serializer version: 1
# name: test_entities[light.dimmable_light-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.dimmable_light',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': None,
'platform': 'niko_home_control',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '01JFN93M7KRA38V5AMPCJ2JYYV-2',
'unit_of_measurement': None,
})
# ---
# name: test_entities[light.dimmable_light-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'brightness': 255,
'color_mode': <ColorMode.BRIGHTNESS: 'brightness'>,
'friendly_name': 'dimmable light',
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
'supported_features': <LightEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'light.dimmable_light',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_entities[light.light-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.ONOFF: 'onoff'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.light',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': None,
'platform': 'niko_home_control',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '01JFN93M7KRA38V5AMPCJ2JYYV-1',
'unit_of_measurement': None,
})
# ---
# name: test_entities[light.light-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'color_mode': <ColorMode.ONOFF: 'onoff'>,
'friendly_name': 'light',
'supported_color_modes': list([
<ColorMode.ONOFF: 'onoff'>,
]),
'supported_features': <LightEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'light.light',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
138 changes: 138 additions & 0 deletions tests/components/niko_home_control/test_light.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"""Tests for the Niko Home Control Light platform."""

from typing import Any
from unittest.mock import AsyncMock, patch

import pytest
from syrupy import SnapshotAssertion

from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN as LIGHT_DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_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_niko_home_control_connection: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
with patch(
"homeassistant.components.niko_home_control.PLATFORMS", [Platform.LIGHT]
):
Comment on lines +34 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this an autouse fixture?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only want this for one test, i dont think we did add it for others

await setup_integration(hass, mock_config_entry)

await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)


@pytest.mark.parametrize(
("light_id", "data", "set_brightness"),
[
(0, {ATTR_ENTITY_ID: "light.light"}, 100.0),
(
1,
{ATTR_ENTITY_ID: "light.dimmable_light", ATTR_BRIGHTNESS: 50},
19.607843137254903,
),
],
)
async def test_turning_on(
hass: HomeAssistant,
mock_niko_home_control_connection: AsyncMock,
mock_config_entry: MockConfigEntry,
light_id: int,
data: dict[str, Any],
set_brightness: int,
) -> None:
"""Test turning on the light."""
await setup_integration(hass, mock_config_entry)

await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
data,
blocking=True,
)
mock_niko_home_control_connection.lights[light_id].turn_on.assert_called_once_with(
set_brightness
)


@pytest.mark.parametrize(
("light_id", "entity_id"),
[
(0, "light.light"),
(1, "light.dimmable_light"),
],
)
async def test_turning_off(
hass: HomeAssistant,
mock_niko_home_control_connection: AsyncMock,
mock_config_entry: MockConfigEntry,
light_id: int,
entity_id: str,
) -> None:
"""Test turning on the light."""
await setup_integration(hass, mock_config_entry)

await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
mock_niko_home_control_connection.lights[
light_id
].turn_off.assert_called_once_with()


async def test_updating(
hass: HomeAssistant,
mock_niko_home_control_connection: AsyncMock,
mock_config_entry: MockConfigEntry,
light: AsyncMock,
dimmable_light: AsyncMock,
) -> None:
"""Test turning on the light."""
await setup_integration(hass, mock_config_entry)

assert hass.states.get("light.light").state == STATE_ON

light.state = 0
await mock_niko_home_control_connection.register_callback.call_args_list[0][0][1](0)
await hass.async_block_till_done()

assert hass.states.get("light.light").state == STATE_OFF

assert hass.states.get("light.dimmable_light").state == STATE_ON
assert hass.states.get("light.dimmable_light").attributes[ATTR_BRIGHTNESS] == 255

dimmable_light.state = 80
await mock_niko_home_control_connection.register_callback.call_args_list[1][0][1](
80
)
await hass.async_block_till_done()

assert hass.states.get("light.dimmable_light").state == STATE_ON
assert hass.states.get("light.dimmable_light").attributes[ATTR_BRIGHTNESS] == 204

dimmable_light.state = 0
await mock_niko_home_control_connection.register_callback.call_args_list[1][0][1](0)
await hass.async_block_till_done()

assert hass.states.get("light.dimmable_light").state == STATE_OFF
assert hass.states.get("light.dimmable_light").attributes[ATTR_BRIGHTNESS] is None