diff --git a/tests/components/velbus/__init__.py b/tests/components/velbus/__init__.py index 68bd639f464..102c4a4e777 100644 --- a/tests/components/velbus/__init__.py +++ b/tests/components/velbus/__init__.py @@ -1 +1,13 @@ """Tests for the Velbus component.""" + +from homeassistant.components.velbus import VelbusConfigEntry +from homeassistant.core import HomeAssistant + + +async def init_integration( + hass: HomeAssistant, + config_entry: VelbusConfigEntry, +) -> None: + """Load the Velbus integration.""" + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/velbus/conftest.py b/tests/components/velbus/conftest.py index 402acb821be..254ae470599 100644 --- a/tests/components/velbus/conftest.py +++ b/tests/components/velbus/conftest.py @@ -1,12 +1,13 @@ """Fixtures for the Velbus tests.""" from collections.abc import Generator -from unittest.mock import MagicMock, patch +from unittest.mock import AsyncMock, MagicMock, patch import pytest +from velbusaio.channels import Button +from homeassistant.components.velbus import VelbusConfigEntry from homeassistant.components.velbus.const import DOMAIN -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_NAME, CONF_PORT from homeassistant.core import HomeAssistant @@ -16,14 +17,42 @@ from tests.common import MockConfigEntry @pytest.fixture(name="controller") -def mock_controller() -> Generator[MagicMock]: +def mock_controller(mock_button: AsyncMock) -> Generator[AsyncMock]: """Mock a successful velbus controller.""" - with patch("homeassistant.components.velbus.Velbus", autospec=True) as controller: + with ( + patch("homeassistant.components.velbus.Velbus", autospec=True) as controller, + patch( + "homeassistant.components.velbus.config_flow.velbusaio.controller.Velbus", + new=controller, + ), + ): + cont = controller.return_value + cont.get_all_binary_sensor.return_value = [mock_button] yield controller +@pytest.fixture +def mock_button() -> AsyncMock: + """Mock a successful velbus channel.""" + channel = AsyncMock(spec=Button) + channel.get_categories.return_value = ["binary_sensor", "led", "button"] + channel.get_name.return_value = "ButtonOn" + channel.get_module_address.return_value = 1 + channel.get_channel_number.return_value = 1 + channel.get_module_type_name.return_value = "VMB4RYLD" + channel.get_full_name.return_value = "Channel full name" + channel.get_module_sw_version.return_value = "1.0.0" + channel.get_module_serial.return_value = "a1b2c3d4e5f6" + channel.is_closed.return_value = True + return channel + + +# moddify this one to set the runtime_data correctly @pytest.fixture(name="config_entry") -def mock_config_entry(hass: HomeAssistant) -> ConfigEntry: +async def mock_config_entry( + hass: HomeAssistant, + controller: MagicMock, +) -> VelbusConfigEntry: """Create and register mock config entry.""" config_entry = MockConfigEntry( domain=DOMAIN, diff --git a/tests/components/velbus/snapshots/test_binary_sensor.ambr b/tests/components/velbus/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..998f2528d9c --- /dev/null +++ b/tests/components/velbus/snapshots/test_binary_sensor.ambr @@ -0,0 +1,47 @@ +# serializer version: 1 +# name: test_entities[binary_sensor.buttonon-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.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[binary_sensor.buttonon-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'ButtonOn', + }), + 'context': , + 'entity_id': 'binary_sensor.buttonon', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/velbus/test_binary_sensor.py b/tests/components/velbus/test_binary_sensor.py new file mode 100644 index 00000000000..29497cd36e1 --- /dev/null +++ b/tests/components/velbus/test_binary_sensor.py @@ -0,0 +1,26 @@ +"""Velbus binary_sensor platform tests.""" + +from unittest.mock import patch + +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import 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.BINARY_SENSOR]): + await init_integration(hass, config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)