mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Refactor Zeversolar init tests (#118551)
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
parent
5166426d0a
commit
a2504dafbc
47
tests/components/zeversolar/conftest.py
Normal file
47
tests/components/zeversolar/conftest.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
"""Define mocks and test objects."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from zeversolar import StatusEnum, ZeverSolarData
|
||||||
|
|
||||||
|
from homeassistant.components.zeversolar.const import DOMAIN
|
||||||
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
MOCK_HOST_ZEVERSOLAR = "zeversolar-fake-host"
|
||||||
|
MOCK_PORT_ZEVERSOLAR = 10200
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def config_entry() -> MockConfigEntry:
|
||||||
|
"""Create a mock config entry."""
|
||||||
|
|
||||||
|
return MockConfigEntry(
|
||||||
|
data={
|
||||||
|
CONF_HOST: MOCK_HOST_ZEVERSOLAR,
|
||||||
|
CONF_PORT: MOCK_PORT_ZEVERSOLAR,
|
||||||
|
},
|
||||||
|
domain=DOMAIN,
|
||||||
|
unique_id="my_id_2",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def zeversolar_data() -> ZeverSolarData:
|
||||||
|
"""Create a ZeverSolarData structure for tests."""
|
||||||
|
|
||||||
|
return ZeverSolarData(
|
||||||
|
wifi_enabled=False,
|
||||||
|
serial_or_registry_id="1223",
|
||||||
|
registry_key="A-2",
|
||||||
|
hardware_version="M10",
|
||||||
|
software_version="123-23",
|
||||||
|
reported_datetime="19900101 23:00",
|
||||||
|
communication_status=StatusEnum.OK,
|
||||||
|
num_inverters=1,
|
||||||
|
serial_number="123456778",
|
||||||
|
pac=1234,
|
||||||
|
energy_today=123,
|
||||||
|
status=StatusEnum.OK,
|
||||||
|
meter_status=StatusEnum.OK,
|
||||||
|
)
|
@ -1,32 +1,39 @@
|
|||||||
"""Test the init file code."""
|
"""Test the init file code."""
|
||||||
|
|
||||||
import pytest
|
from unittest.mock import patch
|
||||||
|
|
||||||
import homeassistant.components.zeversolar.__init__ as init
|
from zeversolar import ZeverSolarData
|
||||||
from homeassistant.components.zeversolar.const import DOMAIN
|
from zeversolar.exceptions import ZeverSolarTimeout
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
|
||||||
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, MockModule, mock_integration
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
MOCK_HOST_ZEVERSOLAR = "zeversolar-fake-host"
|
|
||||||
MOCK_PORT_ZEVERSOLAR = 10200
|
|
||||||
|
|
||||||
|
|
||||||
async def test_async_setup_entry_fails(hass: HomeAssistant) -> None:
|
async def test_async_setup_entry_fails(
|
||||||
"""Test the sensor setup."""
|
hass: HomeAssistant, config_entry: MockConfigEntry, zeversolar_data: ZeverSolarData
|
||||||
mock_integration(hass, MockModule(DOMAIN))
|
) -> None:
|
||||||
|
"""Test to load/unload the integration."""
|
||||||
|
|
||||||
config = MockConfigEntry(
|
config_entry.add_to_hass(hass)
|
||||||
data={
|
|
||||||
CONF_HOST: MOCK_HOST_ZEVERSOLAR,
|
|
||||||
CONF_PORT: MOCK_PORT_ZEVERSOLAR,
|
|
||||||
},
|
|
||||||
domain=DOMAIN,
|
|
||||||
)
|
|
||||||
|
|
||||||
config.add_to_hass(hass)
|
with (
|
||||||
|
patch("zeversolar.ZeverSolarClient.get_data", side_effect=ZeverSolarTimeout),
|
||||||
|
):
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||||
|
|
||||||
with pytest.raises(ConfigEntryNotReady):
|
with (
|
||||||
await init.async_setup_entry(hass, config)
|
patch("homeassistant.components.zeversolar.PLATFORMS", []),
|
||||||
|
patch("zeversolar.ZeverSolarClient.get_data", return_value=zeversolar_data),
|
||||||
|
):
|
||||||
|
hass.config_entries.async_schedule_reload(config_entry.entry_id)
|
||||||
|
assert config_entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
with (
|
||||||
|
patch("homeassistant.components.zeversolar.PLATFORMS", []),
|
||||||
|
):
|
||||||
|
result = await hass.config_entries.async_unload(config_entry.entry_id)
|
||||||
|
assert result is True
|
||||||
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||||
|
Loading…
x
Reference in New Issue
Block a user