From da96e2077bdc317068e16ddbcef6fa006eaad152 Mon Sep 17 00:00:00 2001 From: Maikel Punie Date: Sun, 29 Dec 2024 11:55:52 +0100 Subject: [PATCH] Add Velbus Button tests (#134186) Co-authored-by: Joost Lekkerkerker --- tests/components/velbus/conftest.py | 2 +- .../velbus/snapshots/test_button.ambr | 47 +++++++++++++++++++ tests/components/velbus/test_button.py | 43 +++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/components/velbus/snapshots/test_button.ambr create mode 100644 tests/components/velbus/test_button.py diff --git a/tests/components/velbus/conftest.py b/tests/components/velbus/conftest.py index 254ae470599..c539db1f46f 100644 --- a/tests/components/velbus/conftest.py +++ b/tests/components/velbus/conftest.py @@ -28,6 +28,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] yield controller @@ -47,7 +48,6 @@ def mock_button() -> AsyncMock: return channel -# moddify this one to set the runtime_data correctly @pytest.fixture(name="config_entry") async def mock_config_entry( hass: HomeAssistant, diff --git a/tests/components/velbus/snapshots/test_button.ambr b/tests/components/velbus/snapshots/test_button.ambr new file mode 100644 index 00000000000..afe79466a44 --- /dev/null +++ b/tests/components/velbus/snapshots/test_button.ambr @@ -0,0 +1,47 @@ +# serializer version: 1 +# name: test_entities[button.buttonon-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.buttonon', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'ButtonOn', + 'platform': 'velbus', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'a1b2c3d4e5f6-1', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[button.buttonon-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'ButtonOn', + }), + 'context': , + 'entity_id': 'button.buttonon', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- diff --git a/tests/components/velbus/test_button.py b/tests/components/velbus/test_button.py new file mode 100644 index 00000000000..d1079497879 --- /dev/null +++ b/tests/components/velbus/test_button.py @@ -0,0 +1,43 @@ +"""Velbus button platform tests.""" + +from unittest.mock import AsyncMock, patch + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.const import ATTR_ENTITY_ID, 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 + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +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.BUTTON]): + await init_integration(hass, config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_button_press( + hass: HomeAssistant, + mock_button: AsyncMock, + config_entry: MockConfigEntry, +) -> None: + """Test button press.""" + await init_integration(hass, config_entry) + await hass.services.async_call( + BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: "button.buttonon"}, blocking=True + ) + mock_button.press.assert_called_once_with()