core/tests/components/deako/test_light.py
Blake Bryant c049129147
Add Deako integration (#121132)
* Deako integration using pydeako

* fix: address feedback

- make unit tests more e2e
- use runtime_data to store connection

* fix: address feedback part 2

- added better type safety for Deako config entries
- refactored the config flow tests to use a conftest mock instead of directly patching
- removed pytest.mark.asyncio test decorators

* fix: address feedback pt 3

- simplify config entry type
- add test for single_instance_allowed
- remove light.py get_state(), only used once, no need to be separate function

* fix: ruff format

* Update homeassistant/components/deako/__init__.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-28 19:16:05 +02:00

193 lines
5.5 KiB
Python

"""Tests for the light module."""
from unittest.mock import MagicMock
import pytest
from syrupy.assertion 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
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry, snapshot_platform
async def test_light_setup_with_device(
hass: HomeAssistant,
pydeako_deako_mock: MagicMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test light platform setup with device returned."""
mock_config_entry.add_to_hass(hass)
pydeako_deako_mock.return_value.get_devices.return_value = {
"some_device": {},
}
pydeako_deako_mock.return_value.get_name.return_value = "some device"
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_light_initial_props(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
pydeako_deako_mock: MagicMock,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test on/off light is setup with accurate initial properties."""
mock_config_entry.add_to_hass(hass)
pydeako_deako_mock.return_value.get_devices.return_value = {
"uuid": {
"name": "kitchen",
}
}
pydeako_deako_mock.return_value.get_name.return_value = "kitchen"
pydeako_deako_mock.return_value.get_state.return_value = {
"power": False,
}
pydeako_deako_mock.return_value.is_dimmable.return_value = False
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_dimmable_light_props(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
pydeako_deako_mock: MagicMock,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test dimmable on/off light is setup with accurate initial properties."""
mock_config_entry.add_to_hass(hass)
pydeako_deako_mock.return_value.get_devices.return_value = {
"uuid": {
"name": "kitchen",
}
}
pydeako_deako_mock.return_value.get_name.return_value = "kitchen"
pydeako_deako_mock.return_value.get_state.return_value = {
"power": True,
"dim": 50,
}
pydeako_deako_mock.return_value.is_dimmable.return_value = True
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_light_power_change_on(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
pydeako_deako_mock: MagicMock,
) -> None:
"""Test turing on a deako device."""
mock_config_entry.add_to_hass(hass)
pydeako_deako_mock.return_value.get_devices.return_value = {
"uuid": {
"name": "kitchen",
}
}
pydeako_deako_mock.return_value.get_name.return_value = "kitchen"
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.kitchen"},
blocking=True,
)
pydeako_deako_mock.return_value.control_device.assert_called_once_with(
"uuid", True, None
)
async def test_light_power_change_off(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
pydeako_deako_mock: MagicMock,
) -> None:
"""Test turing off a deako device."""
mock_config_entry.add_to_hass(hass)
pydeako_deako_mock.return_value.get_devices.return_value = {
"uuid": {
"name": "kitchen",
}
}
pydeako_deako_mock.return_value.get_name.return_value = "kitchen"
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.kitchen"},
blocking=True,
)
pydeako_deako_mock.return_value.control_device.assert_called_once_with(
"uuid", False, None
)
@pytest.mark.parametrize(
("dim_input", "expected_dim_value"),
[
(3, 1),
(255, 100),
(127, 50),
],
)
async def test_light_brightness_change(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
pydeako_deako_mock: MagicMock,
dim_input: int,
expected_dim_value: int,
) -> None:
"""Test turing on a deako device."""
mock_config_entry.add_to_hass(hass)
pydeako_deako_mock.return_value.get_devices.return_value = {
"uuid": {
"name": "kitchen",
}
}
pydeako_deako_mock.return_value.get_name.return_value = "kitchen"
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "light.kitchen",
ATTR_BRIGHTNESS: dim_input,
},
blocking=True,
)
pydeako_deako_mock.return_value.control_device.assert_called_once_with(
"uuid", True, expected_dim_value
)