mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-24 11:06:26 +01:00
116 lines
3.2 KiB
Python
116 lines
3.2 KiB
Python
"""Test Tuya valve platform."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from syrupy.assertion import SnapshotAssertion
|
|
from tuya_sharing import CustomerDevice, Manager
|
|
|
|
from homeassistant.components.valve import (
|
|
DOMAIN as VALVE_DOMAIN,
|
|
SERVICE_CLOSE_VALVE,
|
|
SERVICE_OPEN_VALVE,
|
|
)
|
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
from . import initialize_entry
|
|
|
|
from tests.common import MockConfigEntry, snapshot_platform
|
|
|
|
|
|
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.VALVE])
|
|
async def test_platform_setup_and_discovery(
|
|
hass: HomeAssistant,
|
|
mock_manager: Manager,
|
|
mock_config_entry: MockConfigEntry,
|
|
mock_devices: list[CustomerDevice],
|
|
entity_registry: er.EntityRegistry,
|
|
snapshot: SnapshotAssertion,
|
|
) -> None:
|
|
"""Test platform setup and discovery."""
|
|
await initialize_entry(hass, mock_manager, mock_config_entry, mock_devices)
|
|
|
|
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
|
|
|
|
|
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.VALVE])
|
|
@pytest.mark.parametrize(
|
|
"mock_device_code",
|
|
["sfkzq_ed7frwissyqrejic"],
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("service", "expected_commands"),
|
|
[
|
|
(
|
|
SERVICE_OPEN_VALVE,
|
|
[{"code": "switch_1", "value": True}],
|
|
),
|
|
(
|
|
SERVICE_CLOSE_VALVE,
|
|
[{"code": "switch_1", "value": False}],
|
|
),
|
|
],
|
|
)
|
|
async def test_action(
|
|
hass: HomeAssistant,
|
|
mock_manager: Manager,
|
|
mock_config_entry: MockConfigEntry,
|
|
mock_device: CustomerDevice,
|
|
service: str,
|
|
expected_commands: list[dict[str, Any]],
|
|
) -> None:
|
|
"""Test valve action."""
|
|
entity_id = "valve.jie_hashui_fa_valve_1"
|
|
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(
|
|
VALVE_DOMAIN,
|
|
service,
|
|
{
|
|
ATTR_ENTITY_ID: entity_id,
|
|
},
|
|
blocking=True,
|
|
)
|
|
mock_manager.send_commands.assert_called_once_with(
|
|
mock_device.id, expected_commands
|
|
)
|
|
|
|
|
|
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.VALVE])
|
|
@pytest.mark.parametrize(
|
|
"mock_device_code",
|
|
["sfkzq_ed7frwissyqrejic"],
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("initial_status", "expected_state"),
|
|
[
|
|
(True, "open"),
|
|
(False, "closed"),
|
|
(None, STATE_UNKNOWN),
|
|
("some string", STATE_UNKNOWN),
|
|
],
|
|
)
|
|
async def test_state(
|
|
hass: HomeAssistant,
|
|
mock_manager: Manager,
|
|
mock_config_entry: MockConfigEntry,
|
|
mock_device: CustomerDevice,
|
|
initial_status: Any,
|
|
expected_state: str,
|
|
) -> None:
|
|
"""Test valve state."""
|
|
entity_id = "valve.jie_hashui_fa_valve_1"
|
|
mock_device.status["switch_1"] = initial_status
|
|
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"
|
|
assert state.state == expected_state
|