From d2dfba3116d3bd537c0f04a367d072f7d9ec76f7 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Sat, 14 Dec 2024 12:00:28 +0100 Subject: [PATCH] Improve Slide Local device tests (#133197) --- .../components/slide_local/entity.py | 10 +++--- tests/components/slide_local/conftest.py | 20 +++++------ .../slide_local/fixtures/slide_1.json | 4 +-- .../slide_local/snapshots/test_init.ambr | 33 +++++++++++++++++++ .../slide_local/test_config_flow.py | 8 ++--- tests/components/slide_local/test_init.py | 29 ++++++++++++++++ 6 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 tests/components/slide_local/snapshots/test_init.ambr create mode 100644 tests/components/slide_local/test_init.py diff --git a/homeassistant/components/slide_local/entity.py b/homeassistant/components/slide_local/entity.py index c1dbc101e6f..51269649add 100644 --- a/homeassistant/components/slide_local/entity.py +++ b/homeassistant/components/slide_local/entity.py @@ -1,6 +1,6 @@ """Entities for slide_local integration.""" -from homeassistant.const import CONF_MAC +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -12,18 +12,16 @@ class SlideEntity(CoordinatorEntity[SlideCoordinator]): _attr_has_entity_name = True - def __init__( - self, - coordinator: SlideCoordinator, - ) -> None: + def __init__(self, coordinator: SlideCoordinator) -> None: """Initialize the Slide device.""" super().__init__(coordinator) self._attr_device_info = DeviceInfo( manufacturer="Innovation in Motion", - connections={(CONF_MAC, coordinator.data["mac"])}, + connections={(dr.CONNECTION_NETWORK_MAC, coordinator.data["mac"])}, name=coordinator.data["device_name"], sw_version=coordinator.api_version, + hw_version=coordinator.data["board_rev"], serial_number=coordinator.data["mac"], configuration_url=f"http://{coordinator.host}", ) diff --git a/tests/components/slide_local/conftest.py b/tests/components/slide_local/conftest.py index 0d70d1989e7..ad2734bbb64 100644 --- a/tests/components/slide_local/conftest.py +++ b/tests/components/slide_local/conftest.py @@ -6,7 +6,7 @@ from unittest.mock import AsyncMock, patch import pytest from homeassistant.components.slide_local.const import CONF_INVERT_POSITION, DOMAIN -from homeassistant.const import CONF_API_VERSION, CONF_HOST +from homeassistant.const import CONF_API_VERSION, CONF_HOST, CONF_MAC from .const import HOST, SLIDE_INFO_DATA @@ -22,6 +22,7 @@ def mock_config_entry() -> MockConfigEntry: data={ CONF_HOST: HOST, CONF_API_VERSION: 2, + CONF_MAC: "12:34:56:78:90:ab", }, options={ CONF_INVERT_POSITION: False, @@ -33,25 +34,22 @@ def mock_config_entry() -> MockConfigEntry: @pytest.fixture -def mock_slide_api(): +def mock_slide_api() -> Generator[AsyncMock]: """Build a fixture for the SlideLocalApi that connects successfully and returns one device.""" - mock_slide_local_api = AsyncMock() - mock_slide_local_api.slide_info.return_value = SLIDE_INFO_DATA - with ( patch( - "homeassistant.components.slide_local.SlideLocalApi", + "homeassistant.components.slide_local.coordinator.SlideLocalApi", autospec=True, - return_value=mock_slide_local_api, - ), + ) as mock_slide_local_api, patch( "homeassistant.components.slide_local.config_flow.SlideLocalApi", - autospec=True, - return_value=mock_slide_local_api, + new=mock_slide_local_api, ), ): - yield mock_slide_local_api + client = mock_slide_local_api.return_value + client.slide_info.return_value = SLIDE_INFO_DATA + yield client @pytest.fixture diff --git a/tests/components/slide_local/fixtures/slide_1.json b/tests/components/slide_local/fixtures/slide_1.json index e8c3c85a324..6367b94f243 100644 --- a/tests/components/slide_local/fixtures/slide_1.json +++ b/tests/components/slide_local/fixtures/slide_1.json @@ -1,6 +1,6 @@ { - "slide_id": "slide_300000000000", - "mac": "300000000000", + "slide_id": "slide_1234567890ab", + "mac": "1234567890ab", "board_rev": 1, "device_name": "slide bedroom", "zone_name": "bedroom", diff --git a/tests/components/slide_local/snapshots/test_init.ambr b/tests/components/slide_local/snapshots/test_init.ambr new file mode 100644 index 00000000000..d90f72e4b05 --- /dev/null +++ b/tests/components/slide_local/snapshots/test_init.ambr @@ -0,0 +1,33 @@ +# serializer version: 1 +# name: test_device_info + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': 'http://127.0.0.2', + 'connections': set({ + tuple( + 'mac', + '12:34:56:78:90:ab', + ), + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': 1, + 'id': , + 'identifiers': set({ + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'Innovation in Motion', + 'model': None, + 'model_id': None, + 'name': 'slide bedroom', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': '1234567890ab', + 'suggested_area': None, + 'sw_version': 2, + 'via_device_id': None, + }) +# --- diff --git a/tests/components/slide_local/test_config_flow.py b/tests/components/slide_local/test_config_flow.py index 35aa99a90d7..025f8c323ff 100644 --- a/tests/components/slide_local/test_config_flow.py +++ b/tests/components/slide_local/test_config_flow.py @@ -63,7 +63,7 @@ async def test_user( assert result2["data"][CONF_HOST] == HOST assert result2["data"][CONF_PASSWORD] == "pwd" assert result2["data"][CONF_API_VERSION] == 2 - assert result2["result"].unique_id == "30:00:00:00:00:00" + assert result2["result"].unique_id == "12:34:56:78:90:ab" assert not result2["options"][CONF_INVERT_POSITION] assert len(mock_setup_entry.mock_calls) == 1 @@ -96,7 +96,7 @@ async def test_user_api_1( assert result2["data"][CONF_HOST] == HOST assert result2["data"][CONF_PASSWORD] == "pwd" assert result2["data"][CONF_API_VERSION] == 1 - assert result2["result"].unique_id == "30:00:00:00:00:00" + assert result2["result"].unique_id == "12:34:56:78:90:ab" assert not result2["options"][CONF_INVERT_POSITION] assert len(mock_setup_entry.mock_calls) == 1 @@ -143,7 +143,7 @@ async def test_user_api_error( assert result2["data"][CONF_HOST] == HOST assert result2["data"][CONF_PASSWORD] == "pwd" assert result2["data"][CONF_API_VERSION] == 1 - assert result2["result"].unique_id == "30:00:00:00:00:00" + assert result2["result"].unique_id == "12:34:56:78:90:ab" assert not result2["options"][CONF_INVERT_POSITION] assert len(mock_setup_entry.mock_calls) == 1 @@ -259,7 +259,7 @@ async def test_abort_if_already_setup( ) -> None: """Test we abort if the device is already setup.""" - MockConfigEntry(domain=DOMAIN, unique_id="30:00:00:00:00:00").add_to_hass(hass) + MockConfigEntry(domain=DOMAIN, unique_id="12:34:56:78:90:ab").add_to_hass(hass) result = await hass.config_entries.flow.async_init( DOMAIN, diff --git a/tests/components/slide_local/test_init.py b/tests/components/slide_local/test_init.py new file mode 100644 index 00000000000..7b0a2d83164 --- /dev/null +++ b/tests/components/slide_local/test_init.py @@ -0,0 +1,29 @@ +"""Tests for the Slide Local integration.""" + +from unittest.mock import AsyncMock + +from syrupy import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr + +from . import setup_platform + +from tests.common import MockConfigEntry + + +async def test_device_info( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_slide_api: AsyncMock, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test device registry integration.""" + await setup_platform(hass, mock_config_entry, [Platform.COVER]) + device_entry = device_registry.async_get_device( + connections={(dr.CONNECTION_NETWORK_MAC, "1234567890ab")} + ) + assert device_entry is not None + assert device_entry == snapshot