mirror of
https://github.com/home-assistant/core.git
synced 2025-08-06 06:05:10 +02:00
Refactor fibaro scene test (#102452)
This commit is contained in:
@@ -2,16 +2,21 @@
|
|||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
|
||||||
from pyfibaro.fibaro_scene import SceneModel
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.fibaro import DOMAIN
|
from homeassistant.components.fibaro import CONF_IMPORT_PLUGINS, DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME
|
||||||
from homeassistant.const import Platform
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
TEST_SERIALNUMBER = "HC2-111111"
|
||||||
|
TEST_NAME = "my_fibaro_home_center"
|
||||||
|
TEST_URL = "http://192.168.1.1/api/"
|
||||||
|
TEST_USERNAME = "user"
|
||||||
|
TEST_PASSWORD = "password"
|
||||||
|
TEST_VERSION = "4.360"
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
||||||
@@ -22,10 +27,10 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
|||||||
yield mock_setup_entry
|
yield mock_setup_entry
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="fibaro_scene")
|
@pytest.fixture
|
||||||
def mock_scene() -> SceneModel:
|
def mock_scene() -> Mock:
|
||||||
"""Fixture for an individual scene."""
|
"""Fixture for an individual scene."""
|
||||||
scene = Mock(SceneModel)
|
scene = Mock()
|
||||||
scene.fibaro_id = 1
|
scene.fibaro_id = 1
|
||||||
scene.name = "Test scene"
|
scene.name = "Test scene"
|
||||||
scene.room_id = 1
|
scene.room_id = 1
|
||||||
@@ -33,23 +38,57 @@ def mock_scene() -> SceneModel:
|
|||||||
return scene
|
return scene
|
||||||
|
|
||||||
|
|
||||||
async def setup_platform(
|
@pytest.fixture
|
||||||
hass: HomeAssistant,
|
def mock_room() -> Mock:
|
||||||
platform: Platform,
|
"""Fixture for an individual room."""
|
||||||
room_name: str | None,
|
room = Mock()
|
||||||
scenes: list[SceneModel],
|
room.fibaro_id = 1
|
||||||
) -> ConfigEntry:
|
room.name = "Room 1"
|
||||||
"""Set up the fibaro platform and prerequisites."""
|
return room
|
||||||
hass.config.components.add(DOMAIN)
|
|
||||||
config_entry = MockConfigEntry(domain=DOMAIN, title="Test")
|
|
||||||
config_entry.add_to_hass(hass)
|
|
||||||
|
|
||||||
controller_mock = Mock()
|
|
||||||
controller_mock.hub_serial = "HC2-111111"
|
|
||||||
controller_mock.get_room_name.return_value = room_name
|
|
||||||
controller_mock.read_scenes.return_value = scenes
|
|
||||||
|
|
||||||
hass.data[DOMAIN] = {config_entry.entry_id: controller_mock}
|
@pytest.fixture
|
||||||
await hass.config_entries.async_forward_entry_setup(config_entry, platform)
|
def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
|
||||||
|
"""Return the default mocked config entry."""
|
||||||
|
mock_config_entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
data={
|
||||||
|
CONF_URL: TEST_URL,
|
||||||
|
CONF_USERNAME: TEST_USERNAME,
|
||||||
|
CONF_PASSWORD: TEST_PASSWORD,
|
||||||
|
CONF_IMPORT_PLUGINS: True,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
return mock_config_entry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_fibaro_client() -> Generator[Mock, None, None]:
|
||||||
|
"""Return a mocked FibaroClient."""
|
||||||
|
info_mock = Mock()
|
||||||
|
info_mock.serial_number = TEST_SERIALNUMBER
|
||||||
|
info_mock.hc_name = TEST_NAME
|
||||||
|
info_mock.current_version = TEST_VERSION
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.fibaro.FibaroClient", autospec=True
|
||||||
|
) as fibaro_client_mock:
|
||||||
|
client = fibaro_client_mock.return_value
|
||||||
|
client.set_authentication.return_value = None
|
||||||
|
client.connect.return_value = True
|
||||||
|
client.read_info.return_value = info_mock
|
||||||
|
client.read_rooms.return_value = []
|
||||||
|
client.read_scenes.return_value = []
|
||||||
|
client.read_devices.return_value = []
|
||||||
|
client.register_update_handler.return_value = None
|
||||||
|
client.unregister_update_handler.return_value = None
|
||||||
|
yield client
|
||||||
|
|
||||||
|
|
||||||
|
async def init_integration(
|
||||||
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||||
|
) -> None:
|
||||||
|
"""Set up the fibaro integration for testing."""
|
||||||
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
return config_entry
|
|
||||||
|
@@ -1,21 +1,30 @@
|
|||||||
"""Test the Fibaro scene platform."""
|
"""Test the Fibaro scene platform."""
|
||||||
|
from unittest.mock import Mock
|
||||||
from pyfibaro.fibaro_scene import SceneModel
|
|
||||||
|
|
||||||
from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN
|
from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON, Platform
|
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
from .conftest import setup_platform
|
from .conftest import init_integration
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def test_entity_attributes(hass: HomeAssistant, fibaro_scene: SceneModel) -> None:
|
async def test_entity_attributes(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_fibaro_client: Mock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
mock_scene: Mock,
|
||||||
|
mock_room: Mock,
|
||||||
|
) -> None:
|
||||||
"""Test that the attributes of the entity are correct."""
|
"""Test that the attributes of the entity are correct."""
|
||||||
# Arrange
|
# Arrange
|
||||||
|
mock_fibaro_client.read_rooms.return_value = [mock_room]
|
||||||
|
mock_fibaro_client.read_scenes.return_value = [mock_scene]
|
||||||
entity_registry = er.async_get(hass)
|
entity_registry = er.async_get(hass)
|
||||||
# Act
|
# Act
|
||||||
await setup_platform(hass, Platform.SCENE, "Room 1", [fibaro_scene])
|
await init_integration(hass, mock_config_entry)
|
||||||
# Assert
|
# Assert
|
||||||
entry = entity_registry.async_get("scene.room_1_test_scene")
|
entry = entity_registry.async_get("scene.room_1_test_scene")
|
||||||
|
|
||||||
@@ -25,13 +34,20 @@ async def test_entity_attributes(hass: HomeAssistant, fibaro_scene: SceneModel)
|
|||||||
|
|
||||||
|
|
||||||
async def test_entity_attributes_without_room(
|
async def test_entity_attributes_without_room(
|
||||||
hass: HomeAssistant, fibaro_scene: SceneModel
|
hass: HomeAssistant,
|
||||||
|
mock_fibaro_client: Mock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
mock_scene: Mock,
|
||||||
|
mock_room: Mock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that the attributes of the entity are correct."""
|
"""Test that the attributes of the entity are correct."""
|
||||||
# Arrange
|
# Arrange
|
||||||
|
mock_room.name = None
|
||||||
|
mock_fibaro_client.read_rooms.return_value = [mock_room]
|
||||||
|
mock_fibaro_client.read_scenes.return_value = [mock_scene]
|
||||||
entity_registry = er.async_get(hass)
|
entity_registry = er.async_get(hass)
|
||||||
# Act
|
# Act
|
||||||
await setup_platform(hass, Platform.SCENE, None, [fibaro_scene])
|
await init_integration(hass, mock_config_entry)
|
||||||
# Assert
|
# Assert
|
||||||
entry = entity_registry.async_get("scene.unknown_test_scene")
|
entry = entity_registry.async_get("scene.unknown_test_scene")
|
||||||
|
|
||||||
@@ -39,10 +55,19 @@ async def test_entity_attributes_without_room(
|
|||||||
assert entry.unique_id == "hc2_111111.scene.1"
|
assert entry.unique_id == "hc2_111111.scene.1"
|
||||||
|
|
||||||
|
|
||||||
async def test_activate_scene(hass: HomeAssistant, fibaro_scene: SceneModel) -> None:
|
async def test_activate_scene(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_fibaro_client: Mock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
mock_scene: Mock,
|
||||||
|
mock_room: Mock,
|
||||||
|
) -> None:
|
||||||
"""Test activate scene is called."""
|
"""Test activate scene is called."""
|
||||||
# Arrange
|
# Arrange
|
||||||
await setup_platform(hass, Platform.SCENE, "Room 1", [fibaro_scene])
|
mock_fibaro_client.read_rooms.return_value = [mock_room]
|
||||||
|
mock_fibaro_client.read_scenes.return_value = [mock_scene]
|
||||||
|
# Act
|
||||||
|
await init_integration(hass, mock_config_entry)
|
||||||
# Act
|
# Act
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
SCENE_DOMAIN,
|
SCENE_DOMAIN,
|
||||||
@@ -51,4 +76,4 @@ async def test_activate_scene(hass: HomeAssistant, fibaro_scene: SceneModel) ->
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
# Assert
|
# Assert
|
||||||
assert fibaro_scene.start.call_count == 1
|
assert mock_scene.start.call_count == 1
|
||||||
|
Reference in New Issue
Block a user