Use pytest fixtures on Onewire tests (#57973)
* Add pytest fixtures * Add sysbus fixtures * Adjust parameter name Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
3d8e802141
commit
24737d4d1d
7 changed files with 262 additions and 275 deletions
|
@ -1,99 +1,72 @@
|
|||
"""Tests for 1-Wire config flow."""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from pyownet.protocol import ConnError, OwnetError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.onewire.const import CONF_TYPE_OWSERVER, DOMAIN
|
||||
from homeassistant.components.onewire.const import DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE
|
||||
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from . import (
|
||||
setup_onewire_owserver_integration,
|
||||
setup_onewire_patched_owserver_integration,
|
||||
setup_onewire_sysbus_integration,
|
||||
setup_owproxy_mock_devices,
|
||||
)
|
||||
from . import setup_owproxy_mock_devices
|
||||
|
||||
from tests.common import MockConfigEntry, mock_device_registry, mock_registry
|
||||
from tests.common import mock_device_registry, mock_registry
|
||||
|
||||
|
||||
async def test_owserver_connect_failure(hass):
|
||||
@pytest.mark.usefixtures("owproxy_with_connerror")
|
||||
async def test_owserver_connect_failure(hass: HomeAssistant, config_entry: ConfigEntry):
|
||||
"""Test connection failure raises ConfigEntryNotReady."""
|
||||
config_entry_owserver = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=SOURCE_USER,
|
||||
data={
|
||||
CONF_TYPE: CONF_TYPE_OWSERVER,
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_PORT: "1234",
|
||||
},
|
||||
options={},
|
||||
entry_id="2",
|
||||
)
|
||||
config_entry_owserver.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.onewire.onewirehub.protocol.proxy",
|
||||
side_effect=ConnError,
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry_owserver.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert config_entry_owserver.state is ConfigEntryState.SETUP_RETRY
|
||||
assert not hass.data.get(DOMAIN)
|
||||
|
||||
|
||||
async def test_failed_owserver_listing(hass):
|
||||
"""Create the 1-Wire integration."""
|
||||
config_entry_owserver = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=SOURCE_USER,
|
||||
data={
|
||||
CONF_TYPE: CONF_TYPE_OWSERVER,
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_PORT: "1234",
|
||||
},
|
||||
options={},
|
||||
entry_id="2",
|
||||
)
|
||||
config_entry_owserver.add_to_hass(hass)
|
||||
|
||||
with patch("homeassistant.components.onewire.onewirehub.protocol.proxy") as owproxy:
|
||||
owproxy.return_value.dir.side_effect = OwnetError
|
||||
await hass.config_entries.async_setup(config_entry_owserver.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return config_entry_owserver
|
||||
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
"""Test being able to unload an entry."""
|
||||
config_entry_owserver = await setup_onewire_owserver_integration(hass)
|
||||
config_entry_sysbus = await setup_onewire_sysbus_integration(hass)
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
|
||||
assert config_entry_owserver.state is ConfigEntryState.LOADED
|
||||
assert config_entry_sysbus.state is ConfigEntryState.LOADED
|
||||
|
||||
assert await hass.config_entries.async_unload(config_entry_owserver.entry_id)
|
||||
assert await hass.config_entries.async_unload(config_entry_sysbus.entry_id)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry_owserver.state is ConfigEntryState.NOT_LOADED
|
||||
assert config_entry_sysbus.state is ConfigEntryState.NOT_LOADED
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
assert not hass.data.get(DOMAIN)
|
||||
|
||||
|
||||
@patch("homeassistant.components.onewire.onewirehub.protocol.proxy")
|
||||
async def test_registry_cleanup(owproxy, hass):
|
||||
@pytest.mark.usefixtures("owproxy")
|
||||
async def test_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
|
||||
"""Test being able to unload an entry."""
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert not hass.data.get(DOMAIN)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("sysbus")
|
||||
async def test_unload_sysbus_entry(
|
||||
hass: HomeAssistant, sysbus_config_entry: ConfigEntry
|
||||
):
|
||||
"""Test being able to unload an entry."""
|
||||
await hass.config_entries.async_setup(sysbus_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert sysbus_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
assert await hass.config_entries.async_unload(sysbus_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert sysbus_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert not hass.data.get(DOMAIN)
|
||||
|
||||
|
||||
@patch("homeassistant.components.onewire.PLATFORMS", [SENSOR_DOMAIN])
|
||||
async def test_registry_cleanup(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, owproxy: MagicMock
|
||||
):
|
||||
"""Test for 1-Wire device.
|
||||
|
||||
As they would be on a clean setup: all binary-sensors and switches disabled.
|
||||
"""
|
||||
|
||||
entity_registry = mock_registry(hass)
|
||||
device_registry = mock_device_registry(hass)
|
||||
|
||||
|
@ -101,9 +74,8 @@ async def test_registry_cleanup(owproxy, hass):
|
|||
setup_owproxy_mock_devices(
|
||||
owproxy, SENSOR_DOMAIN, ["10.111111111111", "28.111111111111"]
|
||||
)
|
||||
with patch("homeassistant.components.onewire.PLATFORMS", [SENSOR_DOMAIN]):
|
||||
await setup_onewire_patched_owserver_integration(hass)
|
||||
await hass.async_block_till_done()
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(dr.async_entries_for_config_entry(device_registry, "2")) == 2
|
||||
assert len(er.async_entries_for_config_entry(entity_registry, "2")) == 2
|
||||
|
@ -117,9 +89,8 @@ async def test_registry_cleanup(owproxy, hass):
|
|||
assert len(dr.async_entries_for_config_entry(device_registry, "2")) == 2
|
||||
|
||||
# Second item has disappeared from bus, and was removed manually from the front-end
|
||||
with patch("homeassistant.components.onewire.PLATFORMS", [SENSOR_DOMAIN]):
|
||||
await hass.config_entries.async_reload("2")
|
||||
await hass.async_block_till_done()
|
||||
await hass.config_entries.async_reload("2")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(er.async_entries_for_config_entry(entity_registry, "2")) == 1
|
||||
assert len(dr.async_entries_for_config_entry(device_registry, "2")) == 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue