core/tests/components/tuya/test_light.py

126 lines
3.6 KiB
Python

"""Test Tuya light platform."""
from __future__ import annotations
from unittest.mock import patch
import pytest
from syrupy.assertion import SnapshotAssertion
from tuya_sharing import CustomerDevice
from homeassistant.components.light import (
DOMAIN as LIGHT_DOMAIN,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.components.tuya import ManagerCompat
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import DEVICE_MOCKS, initialize_entry
from tests.common import MockConfigEntry, snapshot_platform
@pytest.mark.parametrize(
"mock_device_code",
[k for k, v in DEVICE_MOCKS.items() if Platform.LIGHT in v],
)
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.LIGHT])
async def test_platform_setup_and_discovery(
hass: HomeAssistant,
mock_manager: ManagerCompat,
mock_config_entry: MockConfigEntry,
mock_device: CustomerDevice,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test platform setup and discovery."""
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.parametrize(
"mock_device_code",
[k for k, v in DEVICE_MOCKS.items() if Platform.LIGHT not in v],
)
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.LIGHT])
async def test_platform_setup_no_discovery(
hass: HomeAssistant,
mock_manager: ManagerCompat,
mock_config_entry: MockConfigEntry,
mock_device: CustomerDevice,
entity_registry: er.EntityRegistry,
) -> None:
"""Test platform setup without discovery."""
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
assert not er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
@pytest.mark.parametrize(
"mock_device_code",
["dj_smart_light_bulb"],
)
async def test_turn_on_white(
hass: HomeAssistant,
mock_manager: ManagerCompat,
mock_config_entry: MockConfigEntry,
mock_device: CustomerDevice,
) -> None:
"""Test turn_on service."""
entity_id = "light.garage_light"
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
state = hass.states.get(entity_id)
assert state is not None, f"{entity_id} does not exist"
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
"entity_id": entity_id,
"white": 150,
},
)
await hass.async_block_till_done()
mock_manager.send_commands.assert_called_once_with(
mock_device.id,
[
{"code": "switch_led", "value": True},
{"code": "work_mode", "value": "white"},
],
)
@pytest.mark.parametrize(
"mock_device_code",
["dj_smart_light_bulb"],
)
async def test_turn_off(
hass: HomeAssistant,
mock_manager: ManagerCompat,
mock_config_entry: MockConfigEntry,
mock_device: CustomerDevice,
) -> None:
"""Test turn_off service."""
entity_id = "light.garage_light"
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
state = hass.states.get(entity_id)
assert state is not None, f"{entity_id} does not exist"
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{
"entity_id": entity_id,
},
)
await hass.async_block_till_done()
mock_manager.send_commands.assert_called_once_with(
mock_device.id, [{"code": "switch_led", "value": False}]
)