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
|
@ -2,102 +2,18 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from pyownet.protocol import ProtocolError
|
||||
|
||||
from homeassistant.components.onewire.const import (
|
||||
CONF_MOUNT_DIR,
|
||||
CONF_NAMES,
|
||||
CONF_TYPE_OWSERVER,
|
||||
CONF_TYPE_SYSBUS,
|
||||
DEFAULT_SYSBUS_MOUNT_DIR,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE
|
||||
from homeassistant.components.onewire.const import DEFAULT_SYSBUS_MOUNT_DIR
|
||||
|
||||
from .const import MOCK_OWPROXY_DEVICES, MOCK_SYSBUS_DEVICES
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def setup_onewire_sysbus_integration(hass):
|
||||
"""Create the 1-Wire integration."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=SOURCE_USER,
|
||||
data={
|
||||
CONF_TYPE: CONF_TYPE_SYSBUS,
|
||||
CONF_MOUNT_DIR: DEFAULT_SYSBUS_MOUNT_DIR,
|
||||
"names": {
|
||||
"10-111111111111": "My DS18B20",
|
||||
},
|
||||
},
|
||||
unique_id=f"{CONF_TYPE_SYSBUS}:{DEFAULT_SYSBUS_MOUNT_DIR}",
|
||||
options={},
|
||||
entry_id="1",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.onewire.onewirehub.os.path.isdir", return_value=True
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return config_entry
|
||||
|
||||
|
||||
async def setup_onewire_owserver_integration(hass):
|
||||
"""Create the 1-Wire integration."""
|
||||
config_entry = 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.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.onewire.onewirehub.protocol.proxy",
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return config_entry
|
||||
|
||||
|
||||
async def setup_onewire_patched_owserver_integration(hass):
|
||||
"""Create the 1-Wire integration."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=SOURCE_USER,
|
||||
data={
|
||||
CONF_TYPE: CONF_TYPE_OWSERVER,
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_PORT: 1234,
|
||||
CONF_NAMES: {
|
||||
"10.111111111111": "My DS18B20",
|
||||
},
|
||||
},
|
||||
options={},
|
||||
entry_id="2",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return config_entry
|
||||
|
||||
|
||||
def setup_owproxy_mock_devices(owproxy, domain, device_ids) -> None:
|
||||
def setup_owproxy_mock_devices(
|
||||
owproxy: MagicMock, platform: str, device_ids: list(str)
|
||||
) -> None:
|
||||
"""Set up mock for owproxy."""
|
||||
dir_return_value = []
|
||||
main_read_side_effect = []
|
||||
|
@ -115,7 +31,7 @@ def setup_owproxy_mock_devices(owproxy, domain, device_ids) -> None:
|
|||
main_read_side_effect += mock_device["inject_reads"]
|
||||
|
||||
# Setup sub-device reads
|
||||
device_sensors = mock_device.get(domain, [])
|
||||
device_sensors = mock_device.get(platform, [])
|
||||
for expected_sensor in device_sensors:
|
||||
sub_read_side_effect.append(expected_sensor["injected_value"])
|
||||
|
||||
|
@ -130,7 +46,7 @@ def setup_owproxy_mock_devices(owproxy, domain, device_ids) -> None:
|
|||
|
||||
|
||||
def setup_sysbus_mock_devices(
|
||||
domain: str, device_ids: list[str]
|
||||
platform: str, device_ids: list[str]
|
||||
) -> tuple[list[str], list[Any]]:
|
||||
"""Set up mock for sysbus."""
|
||||
glob_result = []
|
||||
|
@ -143,7 +59,7 @@ def setup_sysbus_mock_devices(
|
|||
glob_result += [f"/{DEFAULT_SYSBUS_MOUNT_DIR}/{device_id}"]
|
||||
|
||||
# Setup sub-device reads
|
||||
device_sensors = mock_device.get(domain, [])
|
||||
device_sensors = mock_device.get(platform, [])
|
||||
for expected_sensor in device_sensors:
|
||||
if isinstance(expected_sensor["injected_value"], list):
|
||||
read_side_effect += expected_sensor["injected_value"]
|
||||
|
|
95
tests/components/onewire/conftest.py
Normal file
95
tests/components/onewire/conftest.py
Normal file
|
@ -0,0 +1,95 @@
|
|||
"""Provide common 1-Wire fixtures."""
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from pyownet.protocol import ConnError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.onewire.const import (
|
||||
CONF_MOUNT_DIR,
|
||||
CONF_NAMES,
|
||||
CONF_TYPE_OWSERVER,
|
||||
CONF_TYPE_SYSBUS,
|
||||
DEFAULT_SYSBUS_MOUNT_DIR,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_USER, ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import MOCK_OWPROXY_DEVICES
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture(name="device_id", params=MOCK_OWPROXY_DEVICES.keys())
|
||||
def get_device_id(request: pytest.FixtureRequest) -> str:
|
||||
"""Parametrize device id."""
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def get_config_entry(hass: HomeAssistant) -> ConfigEntry:
|
||||
"""Create and register mock config entry."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=SOURCE_USER,
|
||||
data={
|
||||
CONF_TYPE: CONF_TYPE_OWSERVER,
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_PORT: 1234,
|
||||
CONF_NAMES: {
|
||||
"10.111111111111": "My DS18B20",
|
||||
},
|
||||
},
|
||||
options={},
|
||||
entry_id="2",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
return config_entry
|
||||
|
||||
|
||||
@pytest.fixture(name="sysbus_config_entry")
|
||||
def get_sysbus_config_entry(hass: HomeAssistant) -> ConfigEntry:
|
||||
"""Create and register mock config entry."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=SOURCE_USER,
|
||||
data={
|
||||
CONF_TYPE: CONF_TYPE_SYSBUS,
|
||||
CONF_MOUNT_DIR: DEFAULT_SYSBUS_MOUNT_DIR,
|
||||
"names": {
|
||||
"10-111111111111": "My DS18B20",
|
||||
},
|
||||
},
|
||||
unique_id=f"{CONF_TYPE_SYSBUS}:{DEFAULT_SYSBUS_MOUNT_DIR}",
|
||||
options={},
|
||||
entry_id="3",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
return config_entry
|
||||
|
||||
|
||||
@pytest.fixture(name="owproxy")
|
||||
def get_owproxy() -> MagicMock:
|
||||
"""Mock owproxy."""
|
||||
with patch("homeassistant.components.onewire.onewirehub.protocol.proxy") as owproxy:
|
||||
yield owproxy
|
||||
|
||||
|
||||
@pytest.fixture(name="owproxy_with_connerror")
|
||||
def get_owproxy_with_connerror() -> MagicMock:
|
||||
"""Mock owproxy."""
|
||||
with patch(
|
||||
"homeassistant.components.onewire.onewirehub.protocol.proxy",
|
||||
side_effect=ConnError,
|
||||
) as owproxy:
|
||||
yield owproxy
|
||||
|
||||
|
||||
@pytest.fixture(name="sysbus")
|
||||
def get_sysbus() -> MagicMock:
|
||||
"""Mock sysbus."""
|
||||
with patch(
|
||||
"homeassistant.components.onewire.onewirehub.os.path.isdir", return_value=True
|
||||
):
|
||||
yield
|
|
@ -1,51 +1,52 @@
|
|||
"""Tests for 1-Wire devices connected on OWServer."""
|
||||
import copy
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
||||
from homeassistant.components.onewire.binary_sensor import DEVICE_BINARY_SENSORS
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_onewire_patched_owserver_integration, setup_owproxy_mock_devices
|
||||
from . import setup_owproxy_mock_devices
|
||||
from .const import MOCK_OWPROXY_DEVICES
|
||||
|
||||
from tests.common import mock_registry
|
||||
|
||||
MOCK_BINARY_SENSORS = {
|
||||
key: value
|
||||
for (key, value) in MOCK_OWPROXY_DEVICES.items()
|
||||
if BINARY_SENSOR_DOMAIN in value
|
||||
}
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def override_platforms():
|
||||
"""Override PLATFORMS."""
|
||||
with patch("homeassistant.components.onewire.PLATFORMS", [BINARY_SENSOR_DOMAIN]):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_id", MOCK_BINARY_SENSORS.keys())
|
||||
@patch("homeassistant.components.onewire.onewirehub.protocol.proxy")
|
||||
async def test_owserver_binary_sensor(owproxy, hass, device_id):
|
||||
async def test_owserver_binary_sensor(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, owproxy: MagicMock, device_id: str
|
||||
):
|
||||
"""Test for 1-Wire binary sensor.
|
||||
|
||||
This test forces all entities to be enabled.
|
||||
"""
|
||||
|
||||
entity_registry = mock_registry(hass)
|
||||
|
||||
setup_owproxy_mock_devices(owproxy, BINARY_SENSOR_DOMAIN, [device_id])
|
||||
|
||||
mock_device = MOCK_BINARY_SENSORS[device_id]
|
||||
expected_entities = mock_device[BINARY_SENSOR_DOMAIN]
|
||||
mock_device = MOCK_OWPROXY_DEVICES[device_id]
|
||||
expected_entities = mock_device.get(BINARY_SENSOR_DOMAIN, [])
|
||||
|
||||
# Force enable binary sensors
|
||||
patch_device_binary_sensors = copy.deepcopy(DEVICE_BINARY_SENSORS)
|
||||
for item in patch_device_binary_sensors[device_id[0:2]]:
|
||||
if device_binary_sensor := patch_device_binary_sensors.get(device_id[0:2]):
|
||||
for item in device_binary_sensor:
|
||||
item.entity_registry_enabled_default = True
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.onewire.PLATFORMS", [BINARY_SENSOR_DOMAIN]
|
||||
), patch.dict(
|
||||
with patch.dict(
|
||||
"homeassistant.components.onewire.binary_sensor.DEVICE_BINARY_SENSORS",
|
||||
patch_device_binary_sensors,
|
||||
):
|
||||
await setup_onewire_patched_owserver_integration(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(entity_registry.entities) == len(expected_entities)
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
"""Tests for 1-Wire config flow."""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from pyownet import protocol
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.onewire.const import (
|
||||
CONF_MOUNT_DIR,
|
||||
|
@ -10,18 +11,26 @@ from homeassistant.components.onewire.const import (
|
|||
DEFAULT_SYSBUS_MOUNT_DIR,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.config_entries import SOURCE_USER, ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import (
|
||||
RESULT_TYPE_ABORT,
|
||||
RESULT_TYPE_CREATE_ENTRY,
|
||||
RESULT_TYPE_FORM,
|
||||
)
|
||||
|
||||
from . import setup_onewire_owserver_integration, setup_onewire_sysbus_integration
|
||||
|
||||
@pytest.fixture(autouse=True, name="mock_setup_entry")
|
||||
def override_async_setup_entry() -> AsyncMock:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.onewire.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
async def test_user_owserver(hass):
|
||||
async def test_user_owserver(hass: HomeAssistant, mock_setup_entry: AsyncMock):
|
||||
"""Test OWServer user flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
|
@ -53,10 +62,9 @@ async def test_user_owserver(hass):
|
|||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
# Valid server
|
||||
with patch("homeassistant.components.onewire.onewirehub.protocol.proxy",), patch(
|
||||
"homeassistant.components.onewire.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
with patch(
|
||||
"homeassistant.components.onewire.onewirehub.protocol.proxy",
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 1234},
|
||||
|
@ -73,13 +81,12 @@ async def test_user_owserver(hass):
|
|||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_user_owserver_duplicate(hass):
|
||||
async def test_user_owserver_duplicate(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, mock_setup_entry: AsyncMock
|
||||
):
|
||||
"""Test OWServer flow."""
|
||||
with patch(
|
||||
"homeassistant.components.onewire.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
await setup_onewire_owserver_integration(hass)
|
||||
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
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
@ -108,7 +115,7 @@ async def test_user_owserver_duplicate(hass):
|
|||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_user_sysbus(hass):
|
||||
async def test_user_sysbus(hass: HomeAssistant, mock_setup_entry: AsyncMock):
|
||||
"""Test SysBus flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
|
@ -143,10 +150,7 @@ async def test_user_sysbus(hass):
|
|||
with patch(
|
||||
"homeassistant.components.onewire.onewirehub.os.path.isdir",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.onewire.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_MOUNT_DIR: "/sys/bus/directory"},
|
||||
|
@ -162,13 +166,12 @@ async def test_user_sysbus(hass):
|
|||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_user_sysbus_duplicate(hass):
|
||||
async def test_user_sysbus_duplicate(
|
||||
hass: HomeAssistant, sysbus_config_entry: ConfigEntry, mock_setup_entry: AsyncMock
|
||||
):
|
||||
"""Test SysBus duplicate flow."""
|
||||
with patch(
|
||||
"homeassistant.components.onewire.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
await setup_onewire_sysbus_integration(hass)
|
||||
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
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
|
|
@ -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.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_owserver.state is ConfigEntryState.SETUP_RETRY
|
||||
assert config_entry.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):
|
||||
@pytest.mark.usefixtures("owproxy")
|
||||
async def test_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
|
||||
"""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.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)
|
||||
|
||||
|
||||
@patch("homeassistant.components.onewire.onewirehub.protocol.proxy")
|
||||
async def test_registry_cleanup(owproxy, hass):
|
||||
@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,8 +74,7 @@ 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.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
|
||||
|
@ -117,7 +89,6 @@ 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()
|
||||
|
||||
|
|
|
@ -1,15 +1,12 @@
|
|||
"""Tests for 1-Wire sensor platform."""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from pyownet.protocol import Error as ProtocolError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.onewire.const import (
|
||||
DEFAULT_SYSBUS_MOUNT_DIR,
|
||||
DOMAIN,
|
||||
PLATFORMS,
|
||||
)
|
||||
from homeassistant.components.onewire.const import DEFAULT_SYSBUS_MOUNT_DIR, DOMAIN
|
||||
from homeassistant.components.sensor import ATTR_STATE_CLASS, DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_MANUFACTURER,
|
||||
|
@ -17,14 +14,10 @@ from homeassistant.const import (
|
|||
ATTR_NAME,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import (
|
||||
setup_onewire_patched_owserver_integration,
|
||||
setup_onewire_sysbus_integration,
|
||||
setup_owproxy_mock_devices,
|
||||
setup_sysbus_mock_devices,
|
||||
)
|
||||
from . import setup_owproxy_mock_devices, setup_sysbus_mock_devices
|
||||
from .const import MOCK_OWPROXY_DEVICES, MOCK_SYSBUS_DEVICES
|
||||
|
||||
from tests.common import assert_setup_component, mock_device_registry, mock_registry
|
||||
|
@ -34,7 +27,14 @@ MOCK_COUPLERS = {
|
|||
}
|
||||
|
||||
|
||||
async def test_setup_minimum(hass):
|
||||
@pytest.fixture(autouse=True)
|
||||
def override_platforms():
|
||||
"""Override PLATFORMS."""
|
||||
with patch("homeassistant.components.onewire.PLATFORMS", [SENSOR_DOMAIN]):
|
||||
yield
|
||||
|
||||
|
||||
async def test_setup_minimum(hass: HomeAssistant):
|
||||
"""Test old platform setup with minimum configuration."""
|
||||
config = {"sensor": {"platform": "onewire"}}
|
||||
with assert_setup_component(1, "sensor"):
|
||||
|
@ -42,7 +42,7 @@ async def test_setup_minimum(hass):
|
|||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_setup_sysbus(hass):
|
||||
async def test_setup_sysbus(hass: HomeAssistant):
|
||||
"""Test old platform setup with SysBus configuration."""
|
||||
config = {
|
||||
"sensor": {
|
||||
|
@ -55,7 +55,7 @@ async def test_setup_sysbus(hass):
|
|||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_setup_owserver(hass):
|
||||
async def test_setup_owserver(hass: HomeAssistant):
|
||||
"""Test old platform setup with OWServer configuration."""
|
||||
config = {"sensor": {"platform": "onewire", "host": "localhost"}}
|
||||
with assert_setup_component(1, "sensor"):
|
||||
|
@ -63,7 +63,7 @@ async def test_setup_owserver(hass):
|
|||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_setup_owserver_with_port(hass):
|
||||
async def test_setup_owserver_with_port(hass: HomeAssistant):
|
||||
"""Test old platform setup with OWServer configuration."""
|
||||
config = {"sensor": {"platform": "onewire", "host": "localhost", "port": "1234"}}
|
||||
with assert_setup_component(1, "sensor"):
|
||||
|
@ -71,9 +71,10 @@ async def test_setup_owserver_with_port(hass):
|
|||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_id", ["1F.111111111111"])
|
||||
@patch("homeassistant.components.onewire.onewirehub.protocol.proxy")
|
||||
async def test_sensors_on_owserver_coupler(owproxy, hass, device_id):
|
||||
@pytest.mark.parametrize("device_id", ["1F.111111111111"], indirect=True)
|
||||
async def test_sensors_on_owserver_coupler(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, owproxy: MagicMock, device_id: str
|
||||
):
|
||||
"""Test for 1-Wire sensors connected to DS2409 coupler."""
|
||||
|
||||
entity_registry = mock_registry(hass)
|
||||
|
@ -111,8 +112,7 @@ async def test_sensors_on_owserver_coupler(owproxy, hass, device_id):
|
|||
owproxy.return_value.dir.side_effect = dir_side_effect
|
||||
owproxy.return_value.read.side_effect = read_side_effect
|
||||
|
||||
with patch("homeassistant.components.onewire.PLATFORMS", [SENSOR_DOMAIN]):
|
||||
await setup_onewire_patched_owserver_integration(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(entity_registry.entities) == len(expected_sensors)
|
||||
|
@ -130,25 +130,22 @@ async def test_sensors_on_owserver_coupler(owproxy, hass, device_id):
|
|||
assert state.attributes["device_file"] == expected_sensor["device_file"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_id", MOCK_OWPROXY_DEVICES.keys())
|
||||
@pytest.mark.parametrize("platform", PLATFORMS)
|
||||
@patch("homeassistant.components.onewire.onewirehub.protocol.proxy")
|
||||
async def test_owserver_setup_valid_device(owproxy, hass, device_id, platform):
|
||||
async def test_owserver_setup_valid_device(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, owproxy: MagicMock, device_id: str
|
||||
):
|
||||
"""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)
|
||||
|
||||
setup_owproxy_mock_devices(owproxy, platform, [device_id])
|
||||
setup_owproxy_mock_devices(owproxy, SENSOR_DOMAIN, [device_id])
|
||||
|
||||
mock_device = MOCK_OWPROXY_DEVICES[device_id]
|
||||
expected_entities = mock_device.get(platform, [])
|
||||
expected_entities = mock_device.get(SENSOR_DOMAIN, [])
|
||||
|
||||
with patch("homeassistant.components.onewire.PLATFORMS", [platform]):
|
||||
await setup_onewire_patched_owserver_integration(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(entity_registry.entities) == len(expected_entities)
|
||||
|
@ -181,8 +178,11 @@ async def test_owserver_setup_valid_device(owproxy, hass, device_id, platform):
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_id", MOCK_SYSBUS_DEVICES.keys())
|
||||
async def test_onewiredirect_setup_valid_device(hass, device_id):
|
||||
@pytest.mark.usefixtures("sysbus")
|
||||
@pytest.mark.parametrize("device_id", MOCK_SYSBUS_DEVICES.keys(), indirect=True)
|
||||
async def test_onewiredirect_setup_valid_device(
|
||||
hass: HomeAssistant, sysbus_config_entry: ConfigEntry, device_id: str
|
||||
):
|
||||
"""Test that sysbus config entry works correctly."""
|
||||
|
||||
entity_registry = mock_registry(hass)
|
||||
|
@ -199,7 +199,7 @@ async def test_onewiredirect_setup_valid_device(hass, device_id):
|
|||
"pi1wire.OneWire.get_temperature",
|
||||
side_effect=read_side_effect,
|
||||
):
|
||||
assert await setup_onewire_sysbus_integration(hass)
|
||||
await hass.config_entries.async_setup(sysbus_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(entity_registry.entities) == len(expected_entities)
|
||||
|
|
|
@ -1,51 +1,52 @@
|
|||
"""Tests for 1-Wire devices connected on OWServer."""
|
||||
import copy
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.onewire.switch import DEVICE_SWITCHES
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TOGGLE, STATE_OFF, STATE_ON
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_onewire_patched_owserver_integration, setup_owproxy_mock_devices
|
||||
from . import setup_owproxy_mock_devices
|
||||
from .const import MOCK_OWPROXY_DEVICES
|
||||
|
||||
from tests.common import mock_registry
|
||||
|
||||
MOCK_SWITCHES = {
|
||||
key: value
|
||||
for (key, value) in MOCK_OWPROXY_DEVICES.items()
|
||||
if SWITCH_DOMAIN in value
|
||||
}
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def override_platforms():
|
||||
"""Override PLATFORMS."""
|
||||
with patch("homeassistant.components.onewire.PLATFORMS", [SWITCH_DOMAIN]):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_id", MOCK_SWITCHES.keys())
|
||||
@patch("homeassistant.components.onewire.onewirehub.protocol.proxy")
|
||||
async def test_owserver_switch(owproxy, hass, device_id):
|
||||
async def test_owserver_switch(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, owproxy: MagicMock, device_id: str
|
||||
):
|
||||
"""Test for 1-Wire switch.
|
||||
|
||||
This test forces all entities to be enabled.
|
||||
"""
|
||||
|
||||
entity_registry = mock_registry(hass)
|
||||
|
||||
setup_owproxy_mock_devices(owproxy, SWITCH_DOMAIN, [device_id])
|
||||
|
||||
mock_device = MOCK_SWITCHES[device_id]
|
||||
expected_entities = mock_device[SWITCH_DOMAIN]
|
||||
mock_device = MOCK_OWPROXY_DEVICES[device_id]
|
||||
expected_entities = mock_device.get(SWITCH_DOMAIN, [])
|
||||
|
||||
# Force enable switches
|
||||
patch_device_switches = copy.deepcopy(DEVICE_SWITCHES)
|
||||
for item in patch_device_switches[device_id[0:2]]:
|
||||
if device_switch := patch_device_switches.get(device_id[0:2]):
|
||||
for item in device_switch:
|
||||
item.entity_registry_enabled_default = True
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.onewire.PLATFORMS", [SWITCH_DOMAIN]
|
||||
), patch.dict(
|
||||
with patch.dict(
|
||||
"homeassistant.components.onewire.switch.DEVICE_SWITCHES", patch_device_switches
|
||||
):
|
||||
await setup_onewire_patched_owserver_integration(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(entity_registry.entities) == len(expected_entities)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue