core/tests/components/onewire/test_switch.py

113 lines
3.5 KiB
Python

"""Tests for 1-Wire switches."""
from collections.abc import Generator
from unittest.mock import MagicMock, patch
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.onewire.onewirehub import _DEVICE_SCAN_INTERVAL
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TOGGLE,
STATE_OFF,
STATE_ON,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import setup_owproxy_mock_devices
from .const import MOCK_OWPROXY_DEVICES
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
@pytest.fixture(autouse=True)
def override_platforms() -> Generator[None]:
"""Override PLATFORMS."""
with patch("homeassistant.components.onewire._PLATFORMS", [Platform.SWITCH]):
yield
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_switches(
hass: HomeAssistant,
config_entry: MockConfigEntry,
owproxy: MagicMock,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test for 1-Wire switch entities."""
setup_owproxy_mock_devices(owproxy, MOCK_OWPROXY_DEVICES.keys())
await hass.config_entries.async_setup(config_entry.entry_id)
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
@pytest.mark.parametrize("device_id", ["05.111111111111"])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_switches_delayed(
hass: HomeAssistant,
config_entry: MockConfigEntry,
owproxy: MagicMock,
device_id: str,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test for delayed 1-Wire switch entities."""
setup_owproxy_mock_devices(owproxy, [])
await hass.config_entries.async_setup(config_entry.entry_id)
assert not er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
setup_owproxy_mock_devices(owproxy, [device_id])
freezer.tick(_DEVICE_SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert (
len(er.async_entries_for_config_entry(entity_registry, config_entry.entry_id))
== 1
)
@pytest.mark.parametrize("device_id", ["05.111111111111"])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_switch_toggle(
hass: HomeAssistant,
config_entry: MockConfigEntry,
owproxy: MagicMock,
device_id: str,
) -> None:
"""Test for 1-Wire switch TOGGLE service."""
setup_owproxy_mock_devices(owproxy, [device_id])
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
entity_id = "switch.05_111111111111_programmed_input_output"
# Test TOGGLE service to off
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