Skip to content

Commit

Permalink
Add Velbus binary sensor tests (#134132)
Browse files Browse the repository at this point in the history
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
  • Loading branch information
cereal2nd and joostlek authored Dec 28, 2024
1 parent d9f2140 commit af13979
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
12 changes: 12 additions & 0 deletions tests/components/velbus/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
39 changes: 34 additions & 5 deletions tests/components/velbus/conftest.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -16,14 +17,42 @@


@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,
Expand Down
47 changes: 47 additions & 0 deletions tests/components/velbus/snapshots/test_binary_sensor.ambr
Original file line number Diff line number Diff line change
@@ -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',
})
# ---
26 changes: 26 additions & 0 deletions tests/components/velbus/test_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit af13979

Please sign in to comment.