-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
tests/components/niko_home_control/snapshots/test_light.ambr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
): | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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