From 5cff79ce5089147ed625afea6d97741bf6acafb0 Mon Sep 17 00:00:00 2001
From: Maikel Punie <maikel.punie@gmail.com>
Date: Wed, 1 Jan 2025 12:11:27 +0100
Subject: [PATCH] Add velbus switch platform testcases (#134207)

---
 tests/components/velbus/conftest.py           | 23 +++++++-
 .../velbus/snapshots/test_switch.ambr         | 47 +++++++++++++++
 tests/components/velbus/test_switch.py        | 57 +++++++++++++++++++
 3 files changed, 125 insertions(+), 2 deletions(-)
 create mode 100644 tests/components/velbus/snapshots/test_switch.ambr
 create mode 100644 tests/components/velbus/test_switch.py

diff --git a/tests/components/velbus/conftest.py b/tests/components/velbus/conftest.py
index c539db1f46f..8e5322e6e05 100644
--- a/tests/components/velbus/conftest.py
+++ b/tests/components/velbus/conftest.py
@@ -4,7 +4,7 @@ from collections.abc import Generator
 from unittest.mock import AsyncMock, MagicMock, patch
 
 import pytest
-from velbusaio.channels import Button
+from velbusaio.channels import Button, Relay
 
 from homeassistant.components.velbus import VelbusConfigEntry
 from homeassistant.components.velbus.const import DOMAIN
@@ -17,7 +17,9 @@ from tests.common import MockConfigEntry
 
 
 @pytest.fixture(name="controller")
-def mock_controller(mock_button: AsyncMock) -> Generator[AsyncMock]:
+def mock_controller(
+    mock_button: AsyncMock, mock_relay: AsyncMock
+) -> Generator[AsyncMock]:
     """Mock a successful velbus controller."""
     with (
         patch("homeassistant.components.velbus.Velbus", autospec=True) as controller,
@@ -29,6 +31,7 @@ def mock_controller(mock_button: AsyncMock) -> Generator[AsyncMock]:
         cont = controller.return_value
         cont.get_all_binary_sensor.return_value = [mock_button]
         cont.get_all_button.return_value = [mock_button]
+        cont.get_all_switch.return_value = [mock_relay]
         yield controller
 
 
@@ -48,6 +51,22 @@ def mock_button() -> AsyncMock:
     return channel
 
 
+@pytest.fixture
+def mock_relay() -> AsyncMock:
+    """Mock a successful velbus channel."""
+    channel = AsyncMock(spec=Relay)
+    channel.get_categories.return_value = ["switch"]
+    channel.get_name.return_value = "RelayName"
+    channel.get_module_address.return_value = 99
+    channel.get_channel_number.return_value = 55
+    channel.get_module_type_name.return_value = "VMB4RYNO"
+    channel.get_full_name.return_value = "Full relay name"
+    channel.get_module_sw_version.return_value = "1.0.1"
+    channel.get_module_serial.return_value = "qwerty123"
+    channel.is_on.return_value = True
+    return channel
+
+
 @pytest.fixture(name="config_entry")
 async def mock_config_entry(
     hass: HomeAssistant,
diff --git a/tests/components/velbus/snapshots/test_switch.ambr b/tests/components/velbus/snapshots/test_switch.ambr
new file mode 100644
index 00000000000..87f0f7eac02
--- /dev/null
+++ b/tests/components/velbus/snapshots/test_switch.ambr
@@ -0,0 +1,47 @@
+# serializer version: 1
+# name: test_entities[switch.relayname-entry]
+  EntityRegistryEntrySnapshot({
+    'aliases': set({
+    }),
+    'area_id': None,
+    'capabilities': None,
+    'config_entry_id': <ANY>,
+    'device_class': None,
+    'device_id': <ANY>,
+    'disabled_by': None,
+    'domain': 'switch',
+    'entity_category': None,
+    'entity_id': 'switch.relayname',
+    'has_entity_name': False,
+    'hidden_by': None,
+    'icon': None,
+    'id': <ANY>,
+    'labels': set({
+    }),
+    'name': None,
+    'options': dict({
+    }),
+    'original_device_class': None,
+    'original_icon': None,
+    'original_name': 'RelayName',
+    'platform': 'velbus',
+    'previous_unique_id': None,
+    'supported_features': 0,
+    'translation_key': None,
+    'unique_id': 'qwerty123-55',
+    'unit_of_measurement': None,
+  })
+# ---
+# name: test_entities[switch.relayname-state]
+  StateSnapshot({
+    'attributes': ReadOnlyDict({
+      'friendly_name': 'RelayName',
+    }),
+    'context': <ANY>,
+    'entity_id': 'switch.relayname',
+    'last_changed': <ANY>,
+    'last_reported': <ANY>,
+    'last_updated': <ANY>,
+    'state': 'on',
+  })
+# ---
diff --git a/tests/components/velbus/test_switch.py b/tests/components/velbus/test_switch.py
new file mode 100644
index 00000000000..9efb65af68d
--- /dev/null
+++ b/tests/components/velbus/test_switch.py
@@ -0,0 +1,57 @@
+"""Velbus switch platform tests."""
+
+from unittest.mock import AsyncMock, patch
+
+from syrupy.assertion import SnapshotAssertion
+
+from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
+from homeassistant.const import (
+    ATTR_ENTITY_ID,
+    SERVICE_TURN_OFF,
+    SERVICE_TURN_ON,
+    Platform,
+)
+from homeassistant.core import HomeAssistant
+from homeassistant.helpers import entity_registry as er
+
+from . import init_integration
+
+from tests.common import MockConfigEntry, snapshot_platform
+
+
+async def test_entities(
+    hass: HomeAssistant,
+    snapshot: SnapshotAssertion,
+    config_entry: MockConfigEntry,
+    entity_registry: er.EntityRegistry,
+) -> None:
+    """Test all entities."""
+    with patch("homeassistant.components.velbus.PLATFORMS", [Platform.SWITCH]):
+        await init_integration(hass, config_entry)
+
+    await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
+
+
+async def test_switch_on_off(
+    hass: HomeAssistant,
+    mock_relay: AsyncMock,
+    config_entry: MockConfigEntry,
+) -> None:
+    """Test switching relay on and off press."""
+    await init_integration(hass, config_entry)
+    # turn off
+    await hass.services.async_call(
+        SWITCH_DOMAIN,
+        SERVICE_TURN_OFF,
+        {ATTR_ENTITY_ID: "switch.relayname"},
+        blocking=True,
+    )
+    mock_relay.turn_off.assert_called_once_with()
+    # turn on
+    await hass.services.async_call(
+        SWITCH_DOMAIN,
+        SERVICE_TURN_ON,
+        {ATTR_ENTITY_ID: "switch.relayname"},
+        blocking=True,
+    )
+    mock_relay.turn_on.assert_called_once_with()
-- 
GitLab