Add velbus switch platform testcases (#134207)

This commit is contained in:
Maikel Punie 2025-01-01 12:11:27 +01:00 committed by GitHub
parent 513c8487c5
commit 5cff79ce50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 125 additions and 2 deletions

View File

@ -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,

View File

@ -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',
})
# ---

View File

@ -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()