2022-05-09 11:16:23 +00:00
|
|
|
"""Tests for 1-Wire switches."""
|
2023-01-03 16:32:39 +00:00
|
|
|
from collections.abc import Generator
|
2021-10-18 17:16:53 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
import pytest
|
2023-04-22 15:22:05 +00:00
|
|
|
from syrupy.assertion import SnapshotAssertion
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2022-02-03 13:16:35 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
2021-10-18 17:16:53 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-10-19 19:41:01 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
SERVICE_TOGGLE,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
2021-12-03 17:25:22 +00:00
|
|
|
Platform,
|
2021-10-19 19:41:01 +00:00
|
|
|
)
|
2021-10-18 17:16:53 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-02-09 09:43:45 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2023-04-22 15:22:05 +00:00
|
|
|
from . import setup_owproxy_mock_devices
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
2023-01-03 16:32:39 +00:00
|
|
|
def override_platforms() -> Generator[None, None, None]:
|
2021-10-18 17:16:53 +00:00
|
|
|
"""Override PLATFORMS."""
|
2021-12-03 17:25:22 +00:00
|
|
|
with patch("homeassistant.components.onewire.PLATFORMS", [Platform.SWITCH]):
|
2021-10-18 17:16:53 +00:00
|
|
|
yield
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
|
2022-05-09 11:16:23 +00:00
|
|
|
async def test_switches(
|
2021-11-09 17:30:05 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
owproxy: MagicMock,
|
|
|
|
device_id: str,
|
2023-02-09 09:43:45 +00:00
|
|
|
device_registry: dr.DeviceRegistry,
|
|
|
|
entity_registry: er.EntityRegistry,
|
2023-04-22 15:22:05 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
2023-01-03 16:32:39 +00:00
|
|
|
) -> None:
|
2023-04-22 15:22:05 +00:00
|
|
|
"""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()
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2023-04-22 15:22:05 +00:00
|
|
|
# Ensure devices are correctly registered
|
|
|
|
device_entries = dr.async_entries_for_config_entry(
|
|
|
|
device_registry, config_entry.entry_id
|
|
|
|
)
|
|
|
|
assert device_entries == snapshot
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2023-04-22 15:22:05 +00:00
|
|
|
# Ensure entities are correctly registered
|
|
|
|
entity_entries = er.async_entries_for_config_entry(
|
|
|
|
entity_registry, config_entry.entry_id
|
|
|
|
)
|
|
|
|
assert entity_entries == snapshot
|
2021-10-19 07:10:26 +00:00
|
|
|
|
2021-12-03 17:25:22 +00:00
|
|
|
setup_owproxy_mock_devices(owproxy, Platform.SWITCH, [device_id])
|
2023-04-22 15:22:05 +00:00
|
|
|
# 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})
|
2021-10-19 07:10:26 +00:00
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2023-04-22 15:22:05 +00:00
|
|
|
# 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", ["05.111111111111"])
|
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
|
|
|
async def test_switch_toggle(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
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()
|
|
|
|
|
|
|
|
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
|