2020-11-08 18:06:41 +00:00
|
|
|
"""Tests for 1-Wire devices connected on OWServer."""
|
|
|
|
import copy
|
2021-10-18 17:16:53 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.onewire.switch import DEVICE_SWITCHES
|
|
|
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
2021-10-18 17:16:53 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-11-08 18:06:41 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TOGGLE, STATE_OFF, STATE_ON
|
2021-10-18 17:16:53 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
from . import setup_owproxy_mock_devices
|
2021-04-01 13:06:47 +00:00
|
|
|
from .const import MOCK_OWPROXY_DEVICES
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
from tests.common import mock_registry
|
|
|
|
|
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def override_platforms():
|
|
|
|
"""Override PLATFORMS."""
|
|
|
|
with patch("homeassistant.components.onewire.PLATFORMS", [SWITCH_DOMAIN]):
|
|
|
|
yield
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
|
|
|
|
async def test_owserver_switch(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry, owproxy: MagicMock, device_id: str
|
|
|
|
):
|
2020-11-08 18:06:41 +00:00
|
|
|
"""Test for 1-Wire switch.
|
|
|
|
|
|
|
|
This test forces all entities to be enabled.
|
|
|
|
"""
|
|
|
|
entity_registry = mock_registry(hass)
|
|
|
|
|
2021-04-01 13:06:47 +00:00
|
|
|
setup_owproxy_mock_devices(owproxy, SWITCH_DOMAIN, [device_id])
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
mock_device = MOCK_OWPROXY_DEVICES[device_id]
|
|
|
|
expected_entities = mock_device.get(SWITCH_DOMAIN, [])
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
# Force enable switches
|
|
|
|
patch_device_switches = copy.deepcopy(DEVICE_SWITCHES)
|
2021-10-18 17:16:53 +00:00
|
|
|
if device_switch := patch_device_switches.get(device_id[0:2]):
|
|
|
|
for item in device_switch:
|
|
|
|
item.entity_registry_enabled_default = True
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-10-18 17:16:53 +00:00
|
|
|
with patch.dict(
|
2020-11-08 18:06:41 +00:00
|
|
|
"homeassistant.components.onewire.switch.DEVICE_SWITCHES", patch_device_switches
|
|
|
|
):
|
2021-10-18 17:16:53 +00:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
2020-11-08 18:06:41 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-04-01 13:06:47 +00:00
|
|
|
assert len(entity_registry.entities) == len(expected_entities)
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-04-01 13:06:47 +00:00
|
|
|
for expected_entity in expected_entities:
|
|
|
|
entity_id = expected_entity["entity_id"]
|
2020-11-08 18:06:41 +00:00
|
|
|
registry_entry = entity_registry.entities.get(entity_id)
|
|
|
|
assert registry_entry is not None
|
|
|
|
state = hass.states.get(entity_id)
|
2021-04-01 13:06:47 +00:00
|
|
|
assert state.state == expected_entity["result"]
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
if state.state == STATE_ON:
|
|
|
|
owproxy.return_value.read.side_effect = [b" 0"]
|
2021-04-01 13:06:47 +00:00
|
|
|
expected_entity["result"] = STATE_OFF
|
2020-11-08 18:06:41 +00:00
|
|
|
elif state.state == STATE_OFF:
|
|
|
|
owproxy.return_value.read.side_effect = [b" 1"]
|
2021-04-01 13:06:47 +00:00
|
|
|
expected_entity["result"] = STATE_ON
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
SWITCH_DOMAIN,
|
|
|
|
SERVICE_TOGGLE,
|
|
|
|
{ATTR_ENTITY_ID: entity_id},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity_id)
|
2021-04-01 13:06:47 +00:00
|
|
|
assert state.state == expected_entity["result"]
|
|
|
|
assert state.attributes["device_file"] == expected_entity.get(
|
2020-12-07 01:09:32 +00:00
|
|
|
"device_file", registry_entry.unique_id
|
|
|
|
)
|