Compare commits
1 commit
dev
...
revert-102
Author | SHA1 | Date | |
---|---|---|---|
|
ae71cc5fc5 |
2 changed files with 35 additions and 99 deletions
|
@ -2,21 +2,16 @@
|
|||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
from pyfibaro.fibaro_scene import SceneModel
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.fibaro import CONF_IMPORT_PLUGINS, DOMAIN
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME
|
||||
from homeassistant.components.fibaro import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
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
|
||||
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
||||
|
@ -27,10 +22,10 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
|||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_scene() -> Mock:
|
||||
@pytest.fixture(name="fibaro_scene")
|
||||
def mock_scene() -> SceneModel:
|
||||
"""Fixture for an individual scene."""
|
||||
scene = Mock()
|
||||
scene = Mock(SceneModel)
|
||||
scene.fibaro_id = 1
|
||||
scene.name = "Test scene"
|
||||
scene.room_id = 1
|
||||
|
@ -38,57 +33,23 @@ def mock_scene() -> Mock:
|
|||
return scene
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_room() -> Mock:
|
||||
"""Fixture for an individual room."""
|
||||
room = Mock()
|
||||
room.fibaro_id = 1
|
||||
room.name = "Room 1"
|
||||
return room
|
||||
async def setup_platform(
|
||||
hass: HomeAssistant,
|
||||
platform: Platform,
|
||||
room_name: str | None,
|
||||
scenes: list[SceneModel],
|
||||
) -> ConfigEntry:
|
||||
"""Set up the fibaro platform and prerequisites."""
|
||||
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
|
||||
|
||||
@pytest.fixture
|
||||
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)
|
||||
hass.data[DOMAIN] = {config_entry.entry_id: controller_mock}
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, platform)
|
||||
await hass.async_block_till_done()
|
||||
return config_entry
|
||||
|
|
|
@ -1,30 +1,21 @@
|
|||
"""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.const import ATTR_ENTITY_ID, SERVICE_TURN_ON
|
||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from .conftest import init_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from .conftest import setup_platform
|
||||
|
||||
|
||||
async def test_entity_attributes(
|
||||
hass: HomeAssistant,
|
||||
mock_fibaro_client: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_scene: Mock,
|
||||
mock_room: Mock,
|
||||
) -> None:
|
||||
async def test_entity_attributes(hass: HomeAssistant, fibaro_scene: SceneModel) -> None:
|
||||
"""Test that the attributes of the entity are correct."""
|
||||
# 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)
|
||||
# Act
|
||||
await init_integration(hass, mock_config_entry)
|
||||
await setup_platform(hass, Platform.SCENE, "Room 1", [fibaro_scene])
|
||||
# Assert
|
||||
entry = entity_registry.async_get("scene.room_1_test_scene")
|
||||
|
||||
|
@ -34,20 +25,13 @@ async def test_entity_attributes(
|
|||
|
||||
|
||||
async def test_entity_attributes_without_room(
|
||||
hass: HomeAssistant,
|
||||
mock_fibaro_client: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_scene: Mock,
|
||||
mock_room: Mock,
|
||||
hass: HomeAssistant, fibaro_scene: SceneModel
|
||||
) -> None:
|
||||
"""Test that the attributes of the entity are correct."""
|
||||
# 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)
|
||||
# Act
|
||||
await init_integration(hass, mock_config_entry)
|
||||
await setup_platform(hass, Platform.SCENE, None, [fibaro_scene])
|
||||
# Assert
|
||||
entry = entity_registry.async_get("scene.unknown_test_scene")
|
||||
|
||||
|
@ -55,19 +39,10 @@ async def test_entity_attributes_without_room(
|
|||
assert entry.unique_id == "hc2_111111.scene.1"
|
||||
|
||||
|
||||
async def test_activate_scene(
|
||||
hass: HomeAssistant,
|
||||
mock_fibaro_client: Mock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_scene: Mock,
|
||||
mock_room: Mock,
|
||||
) -> None:
|
||||
async def test_activate_scene(hass: HomeAssistant, fibaro_scene: SceneModel) -> None:
|
||||
"""Test activate scene is called."""
|
||||
# Arrange
|
||||
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)
|
||||
await setup_platform(hass, Platform.SCENE, "Room 1", [fibaro_scene])
|
||||
# Act
|
||||
await hass.services.async_call(
|
||||
SCENE_DOMAIN,
|
||||
|
@ -76,4 +51,4 @@ async def test_activate_scene(
|
|||
blocking=True,
|
||||
)
|
||||
# Assert
|
||||
assert mock_scene.start.call_count == 1
|
||||
assert fibaro_scene.start.call_count == 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue