Add Velbus binary sensor tests (#134132)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Maikel Punie 2024-12-28 14:57:48 +01:00 committed by GitHub
parent d9f2140df3
commit af13979855
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 119 additions and 5 deletions

View File

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

View File

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

View File

@ -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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.buttonon',
'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': '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': <ANY>,
'entity_id': 'binary_sensor.buttonon',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---

View File

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