Improve Slide Local device tests (#133197)

This commit is contained in:
Joost Lekkerkerker 2024-12-14 12:00:28 +01:00 committed by GitHub
parent bce6127264
commit d2dfba3116
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 81 additions and 23 deletions

View File

@ -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}",
)

View File

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

View File

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

View File

@ -0,0 +1,33 @@
# serializer version: 1
# name: test_device_info
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'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': <ANY>,
'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': <ANY>,
'serial_number': '1234567890ab',
'suggested_area': None,
'sw_version': 2,
'via_device_id': None,
})
# ---

View File

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

View File

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