hass-core/tests/components/energenie_power_sockets/conftest.py
Mischa Siekmann 6d54f686a6
Add Integration for Energenie Power-Sockets (#113097)
* Integration for Energenie Power-Strips (EGPS)

* cleanups reocommended by reviewer

* Adds missing exception handling when trying to send a command to an unreachable device.

* fix: incorrect handling of already opened devices in pyegps api. bump to pyegps=0.2.4

* Add blank line after file docstring, and other cosmetics

* change asyncio.to_thread to async_add_executer_job

* raises HomeAssistantError EgpsException in switch services.

* switch test parameterized by entity name

* reoved unused device registry

* add translation_key and update_before_add

* bump pyegps dependency to version to 0.2.5

* combined get_device patches and put into conftest.py

* changed switch entity to use _attr_is_on and cleanups

* further cleanup

* Apply suggestions from code review

* refactor: rename egps to energenie_power_sockets

* updated test snapshot

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-03-29 13:29:14 +01:00

83 lines
2.4 KiB
Python

"""Configure tests for Energenie-Power-Sockets."""
from collections.abc import Generator
from typing import Final
from unittest.mock import MagicMock, patch
from pyegps.fakes.powerstrip import FakePowerStrip
import pytest
from homeassistant.components.energenie_power_sockets.const import (
CONF_DEVICE_API_ID,
DOMAIN,
)
from homeassistant.const import CONF_NAME
from tests.common import MockConfigEntry
DEMO_CONFIG_DATA: Final = {
CONF_NAME: "Unit Test",
CONF_DEVICE_API_ID: "DYPS:00:11:22",
}
@pytest.fixture
def demo_config_data() -> dict:
"""Return valid user input."""
return {CONF_DEVICE_API_ID: DEMO_CONFIG_DATA[CONF_DEVICE_API_ID]}
@pytest.fixture
def valid_config_entry() -> MockConfigEntry:
"""Return a valid egps config entry."""
return MockConfigEntry(
domain=DOMAIN,
data=DEMO_CONFIG_DATA,
unique_id=DEMO_CONFIG_DATA[CONF_DEVICE_API_ID],
)
@pytest.fixture(name="pyegps_device_mock")
def get_pyegps_device_mock() -> MagicMock:
"""Fixture for a mocked FakePowerStrip."""
fkObj = FakePowerStrip(
devId=DEMO_CONFIG_DATA[CONF_DEVICE_API_ID], number_of_sockets=4
)
fkObj.release = lambda: True
fkObj._status = [0, 1, 0, 1]
usb_device_mock = MagicMock(wraps=fkObj)
usb_device_mock.get_device_type.return_value = "PowerStrip"
usb_device_mock.numberOfSockets = 4
usb_device_mock.device_id = DEMO_CONFIG_DATA[CONF_DEVICE_API_ID]
usb_device_mock.manufacturer = "Energenie"
usb_device_mock.name = "MockedUSBDevice"
return usb_device_mock
@pytest.fixture(name="mock_get_device")
def patch_get_device(pyegps_device_mock: MagicMock) -> Generator[MagicMock, None, None]:
"""Fixture to patch the `get_device` api method."""
with (
patch("homeassistant.components.energenie_power_sockets.get_device") as m1,
patch(
"homeassistant.components.energenie_power_sockets.config_flow.get_device",
new=m1,
) as mock,
):
mock.return_value = pyegps_device_mock
yield mock
@pytest.fixture(name="mock_search_for_devices")
def patch_search_devices(
pyegps_device_mock: MagicMock,
) -> Generator[MagicMock, None, None]:
"""Fixture to patch the `search_for_devices` api method."""
with patch(
"homeassistant.components.energenie_power_sockets.config_flow.search_for_devices",
return_value=[pyegps_device_mock],
) as mock:
yield mock