Use SnapshotAssertion in 1-wire tests (#90782)
This commit is contained in:
parent
b568dd3060
commit
60d7ea6f0d
6 changed files with 6366 additions and 118 deletions
1422
tests/components/onewire/snapshots/test_binary_sensor.ambr
Normal file
1422
tests/components/onewire/snapshots/test_binary_sensor.ambr
Normal file
File diff suppressed because it is too large
Load diff
2619
tests/components/onewire/snapshots/test_sensor.ambr
Normal file
2619
tests/components/onewire/snapshots/test_sensor.ambr
Normal file
File diff suppressed because it is too large
Load diff
2218
tests/components/onewire/snapshots/test_switch.ambr
Normal file
2218
tests/components/onewire/snapshots/test_switch.ambr
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,23 +1,16 @@
|
||||||
"""Tests for 1-Wire binary sensors."""
|
"""Tests for 1-Wire binary sensors."""
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
import logging
|
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.config_validation import ensure_list
|
|
||||||
|
|
||||||
from . import (
|
from . import setup_owproxy_mock_devices
|
||||||
check_and_enable_disabled_entities,
|
|
||||||
check_device_registry,
|
|
||||||
check_entities,
|
|
||||||
setup_owproxy_mock_devices,
|
|
||||||
)
|
|
||||||
from .const import ATTR_DEVICE_INFO, ATTR_UNKNOWN_DEVICE, MOCK_OWPROXY_DEVICES
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
|
@ -32,33 +25,34 @@ async def test_binary_sensors(
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
owproxy: MagicMock,
|
owproxy: MagicMock,
|
||||||
device_id: str,
|
device_id: str,
|
||||||
caplog: pytest.LogCaptureFixture,
|
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
entity_registry: er.EntityRegistry,
|
entity_registry: er.EntityRegistry,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test for 1-Wire binary sensor.
|
"""Test for 1-Wire binary sensors."""
|
||||||
|
setup_owproxy_mock_devices(owproxy, Platform.BINARY_SENSOR, [device_id])
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
This test forces all entities to be enabled.
|
# Ensure devices are correctly registered
|
||||||
"""
|
device_entries = dr.async_entries_for_config_entry(
|
||||||
mock_device = MOCK_OWPROXY_DEVICES[device_id]
|
device_registry, config_entry.entry_id
|
||||||
expected_entities = mock_device.get(Platform.BINARY_SENSOR, [])
|
)
|
||||||
expected_devices = ensure_list(mock_device.get(ATTR_DEVICE_INFO))
|
assert device_entries == snapshot
|
||||||
|
|
||||||
setup_owproxy_mock_devices(owproxy, Platform.BINARY_SENSOR, [device_id])
|
# Ensure entities are correctly registered
|
||||||
with caplog.at_level(logging.WARNING, logger="homeassistant.components.onewire"):
|
entity_entries = er.async_entries_for_config_entry(
|
||||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
entity_registry, config_entry.entry_id
|
||||||
await hass.async_block_till_done()
|
)
|
||||||
if mock_device.get(ATTR_UNKNOWN_DEVICE):
|
assert entity_entries == snapshot
|
||||||
assert "Ignoring unknown device family/type" in caplog.text
|
|
||||||
else:
|
|
||||||
assert "Ignoring unknown device family/type" not in caplog.text
|
|
||||||
|
|
||||||
check_device_registry(device_registry, expected_devices)
|
|
||||||
assert len(entity_registry.entities) == len(expected_entities)
|
|
||||||
check_and_enable_disabled_entities(entity_registry, expected_entities)
|
|
||||||
|
|
||||||
setup_owproxy_mock_devices(owproxy, Platform.BINARY_SENSOR, [device_id])
|
setup_owproxy_mock_devices(owproxy, Platform.BINARY_SENSOR, [device_id])
|
||||||
|
# Some entities are disabled, enable them and reload before checking states
|
||||||
|
for ent in entity_entries:
|
||||||
|
entity_registry.async_update_entity(ent.entity_id, **{"disabled_by": None})
|
||||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
check_entities(hass, entity_registry, expected_entities)
|
# Ensure entity states are correct
|
||||||
|
states = [hass.states.get(ent.entity_id) for ent in entity_entries]
|
||||||
|
assert states == snapshot
|
||||||
|
|
|
@ -1,30 +1,19 @@
|
||||||
"""Tests for 1-Wire sensors."""
|
"""Tests for 1-Wire sensors."""
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
import logging
|
|
||||||
from unittest.mock import MagicMock, _patch_dict, patch
|
from unittest.mock import MagicMock, _patch_dict, patch
|
||||||
|
|
||||||
from pyownet.protocol import OwnetError
|
from pyownet.protocol import OwnetError
|
||||||
import pytest
|
import pytest
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.config_validation import ensure_list
|
|
||||||
|
|
||||||
from . import (
|
from . import setup_owproxy_mock_devices
|
||||||
check_and_enable_disabled_entities,
|
from .const import ATTR_INJECT_READS, MOCK_OWPROXY_DEVICES
|
||||||
check_device_registry,
|
|
||||||
check_entities,
|
|
||||||
setup_owproxy_mock_devices,
|
|
||||||
)
|
|
||||||
from .const import (
|
|
||||||
ATTR_DEVICE_INFO,
|
|
||||||
ATTR_INJECT_READS,
|
|
||||||
ATTR_UNKNOWN_DEVICE,
|
|
||||||
MOCK_OWPROXY_DEVICES,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
|
@ -39,40 +28,37 @@ async def test_sensors(
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
owproxy: MagicMock,
|
owproxy: MagicMock,
|
||||||
device_id: str,
|
device_id: str,
|
||||||
caplog: pytest.LogCaptureFixture,
|
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
entity_registry: er.EntityRegistry,
|
entity_registry: er.EntityRegistry,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test for 1-Wire device.
|
"""Test for 1-Wire sensors."""
|
||||||
|
setup_owproxy_mock_devices(owproxy, Platform.SENSOR, [device_id])
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
As they would be on a clean setup: all binary-sensors and switches disabled.
|
# Ensure devices are correctly registered
|
||||||
"""
|
device_entries = dr.async_entries_for_config_entry(
|
||||||
mock_device = MOCK_OWPROXY_DEVICES[device_id]
|
device_registry, config_entry.entry_id
|
||||||
expected_entities = mock_device.get(Platform.SENSOR, [])
|
)
|
||||||
if "branches" in mock_device:
|
assert device_entries == snapshot
|
||||||
for branch_details in mock_device["branches"].values():
|
|
||||||
for sub_device in branch_details.values():
|
# Ensure entities are correctly registered
|
||||||
expected_entities += sub_device[Platform.SENSOR]
|
entity_entries = er.async_entries_for_config_entry(
|
||||||
expected_devices = ensure_list(mock_device.get(ATTR_DEVICE_INFO))
|
entity_registry, config_entry.entry_id
|
||||||
|
)
|
||||||
setup_owproxy_mock_devices(owproxy, Platform.SENSOR, [device_id])
|
assert entity_entries == snapshot
|
||||||
with caplog.at_level(logging.WARNING, logger="homeassistant.components.onewire"):
|
|
||||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
if mock_device.get(ATTR_UNKNOWN_DEVICE):
|
|
||||||
assert "Ignoring unknown device family/type" in caplog.text
|
|
||||||
else:
|
|
||||||
assert "Ignoring unknown device family/type" not in caplog.text
|
|
||||||
|
|
||||||
check_device_registry(device_registry, expected_devices)
|
|
||||||
assert len(entity_registry.entities) == len(expected_entities)
|
|
||||||
check_and_enable_disabled_entities(entity_registry, expected_entities)
|
|
||||||
|
|
||||||
setup_owproxy_mock_devices(owproxy, Platform.SENSOR, [device_id])
|
setup_owproxy_mock_devices(owproxy, Platform.SENSOR, [device_id])
|
||||||
|
# Some entities are disabled, enable them and reload before checking states
|
||||||
|
for ent in entity_entries:
|
||||||
|
entity_registry.async_update_entity(ent.entity_id, **{"disabled_by": None})
|
||||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
check_entities(hass, entity_registry, expected_entities)
|
# Ensure entity states are correct
|
||||||
|
states = [hass.states.get(ent.entity_id) for ent in entity_entries]
|
||||||
|
assert states == snapshot
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("device_id", ["12.111111111111"])
|
@pytest.mark.parametrize("device_id", ["12.111111111111"])
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
"""Tests for 1-Wire switches."""
|
"""Tests for 1-Wire switches."""
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
import logging
|
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
ATTR_STATE,
|
|
||||||
SERVICE_TOGGLE,
|
SERVICE_TOGGLE,
|
||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
|
@ -17,15 +16,8 @@ from homeassistant.const import (
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.config_validation import ensure_list
|
|
||||||
|
|
||||||
from . import (
|
from . import setup_owproxy_mock_devices
|
||||||
check_and_enable_disabled_entities,
|
|
||||||
check_device_registry,
|
|
||||||
check_entities,
|
|
||||||
setup_owproxy_mock_devices,
|
|
||||||
)
|
|
||||||
from .const import ATTR_DEVICE_INFO, ATTR_UNKNOWN_DEVICE, MOCK_OWPROXY_DEVICES
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
|
@ -40,55 +32,72 @@ async def test_switches(
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
owproxy: MagicMock,
|
owproxy: MagicMock,
|
||||||
device_id: str,
|
device_id: str,
|
||||||
caplog: pytest.LogCaptureFixture,
|
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
entity_registry: er.EntityRegistry,
|
entity_registry: er.EntityRegistry,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test for 1-Wire switch.
|
"""Test for 1-Wire switches."""
|
||||||
|
setup_owproxy_mock_devices(owproxy, Platform.SWITCH, [device_id])
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
This test forces all entities to be enabled.
|
# Ensure devices are correctly registered
|
||||||
"""
|
device_entries = dr.async_entries_for_config_entry(
|
||||||
mock_device = MOCK_OWPROXY_DEVICES[device_id]
|
device_registry, config_entry.entry_id
|
||||||
expected_entities = mock_device.get(Platform.SWITCH, [])
|
)
|
||||||
expected_devices = ensure_list(mock_device.get(ATTR_DEVICE_INFO))
|
assert device_entries == snapshot
|
||||||
|
|
||||||
setup_owproxy_mock_devices(owproxy, Platform.SWITCH, [device_id])
|
# Ensure entities are correctly registered
|
||||||
with caplog.at_level(logging.WARNING, logger="homeassistant.components.onewire"):
|
entity_entries = er.async_entries_for_config_entry(
|
||||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
entity_registry, config_entry.entry_id
|
||||||
await hass.async_block_till_done()
|
)
|
||||||
if mock_device.get(ATTR_UNKNOWN_DEVICE):
|
assert entity_entries == snapshot
|
||||||
assert "Ignoring unknown device family/type" in caplog.text
|
|
||||||
else:
|
|
||||||
assert "Ignoring unknown device family/type" not in caplog.text
|
|
||||||
|
|
||||||
check_device_registry(device_registry, expected_devices)
|
|
||||||
assert len(entity_registry.entities) == len(expected_entities)
|
|
||||||
check_and_enable_disabled_entities(entity_registry, expected_entities)
|
|
||||||
|
|
||||||
setup_owproxy_mock_devices(owproxy, Platform.SWITCH, [device_id])
|
setup_owproxy_mock_devices(owproxy, Platform.SWITCH, [device_id])
|
||||||
|
# Some entities are disabled, enable them and reload before checking states
|
||||||
|
for ent in entity_entries:
|
||||||
|
entity_registry.async_update_entity(ent.entity_id, **{"disabled_by": None})
|
||||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
check_entities(hass, entity_registry, expected_entities)
|
# Ensure entity states are correct
|
||||||
|
states = [hass.states.get(ent.entity_id) for ent in entity_entries]
|
||||||
|
assert states == snapshot
|
||||||
|
|
||||||
# Test TOGGLE service
|
|
||||||
for expected_entity in expected_entities:
|
|
||||||
entity_id = expected_entity[ATTR_ENTITY_ID]
|
|
||||||
|
|
||||||
if expected_entity[ATTR_STATE] == STATE_ON:
|
@pytest.mark.parametrize("device_id", ["05.111111111111"])
|
||||||
owproxy.return_value.read.side_effect = [b" 0"]
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||||
expected_entity[ATTR_STATE] = STATE_OFF
|
async def test_switch_toggle(
|
||||||
elif expected_entity[ATTR_STATE] == STATE_OFF:
|
hass: HomeAssistant,
|
||||||
owproxy.return_value.read.side_effect = [b" 1"]
|
config_entry: ConfigEntry,
|
||||||
expected_entity[ATTR_STATE] = STATE_ON
|
owproxy: MagicMock,
|
||||||
|
device_id: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test for 1-Wire switch TOGGLE service."""
|
||||||
|
setup_owproxy_mock_devices(owproxy, Platform.SWITCH, [device_id])
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
await hass.services.async_call(
|
entity_id = "switch.05_111111111111_programmed_input_output"
|
||||||
SWITCH_DOMAIN,
|
|
||||||
SERVICE_TOGGLE,
|
|
||||||
{ATTR_ENTITY_ID: entity_id},
|
|
||||||
blocking=True,
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
state = hass.states.get(entity_id)
|
# Test TOGGLE service to off
|
||||||
assert state.state == expected_entity[ATTR_STATE]
|
owproxy.return_value.read.side_effect = [b" 0"]
|
||||||
|
await hass.services.async_call(
|
||||||
|
SWITCH_DOMAIN,
|
||||||
|
SERVICE_TOGGLE,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get(entity_id).state == STATE_OFF
|
||||||
|
|
||||||
|
# Test TOGGLE service to on
|
||||||
|
owproxy.return_value.read.side_effect = [b" 1"]
|
||||||
|
await hass.services.async_call(
|
||||||
|
SWITCH_DOMAIN,
|
||||||
|
SERVICE_TOGGLE,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get(entity_id).state == STATE_ON
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue