2021-02-14 16:40:30 +00:00
|
|
|
"""The tests for the Modbus cover component."""
|
2021-04-19 14:52:08 +00:00
|
|
|
|
2021-05-23 15:18:35 +00:00
|
|
|
from pymodbus.exceptions import ModbusException
|
2021-02-14 16:40:30 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
2021-05-02 08:41:36 +00:00
|
|
|
from homeassistant.components.modbus.const import (
|
|
|
|
CALL_TYPE_COIL,
|
|
|
|
CALL_TYPE_REGISTER_HOLDING,
|
2021-05-28 11:23:44 +00:00
|
|
|
CONF_INPUT_TYPE,
|
2021-08-21 13:49:50 +00:00
|
|
|
CONF_LAZY_ERROR,
|
2021-05-16 06:40:19 +00:00
|
|
|
CONF_STATE_CLOSED,
|
|
|
|
CONF_STATE_CLOSING,
|
|
|
|
CONF_STATE_OPEN,
|
|
|
|
CONF_STATE_OPENING,
|
|
|
|
CONF_STATUS_REGISTER,
|
2021-05-02 08:41:36 +00:00
|
|
|
CONF_STATUS_REGISTER_TYPE,
|
2022-03-11 17:58:18 +00:00
|
|
|
MODBUS_DOMAIN,
|
2021-05-02 08:41:36 +00:00
|
|
|
)
|
2021-02-14 16:40:30 +00:00
|
|
|
from homeassistant.const import (
|
2021-05-28 11:23:44 +00:00
|
|
|
CONF_ADDRESS,
|
2021-02-14 16:40:30 +00:00
|
|
|
CONF_COVERS,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
CONF_SLAVE,
|
2021-04-30 20:36:55 +00:00
|
|
|
STATE_CLOSED,
|
2021-05-16 06:40:19 +00:00
|
|
|
STATE_CLOSING,
|
2021-02-14 16:40:30 +00:00
|
|
|
STATE_OPEN,
|
2021-05-16 06:40:19 +00:00
|
|
|
STATE_OPENING,
|
2021-05-23 15:18:35 +00:00
|
|
|
STATE_UNAVAILABLE,
|
2021-02-14 16:40:30 +00:00
|
|
|
)
|
2021-05-16 06:40:19 +00:00
|
|
|
from homeassistant.core import State
|
2022-03-11 17:58:18 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2021-02-14 16:40:30 +00:00
|
|
|
|
2021-10-11 10:43:05 +00:00
|
|
|
from .conftest import TEST_ENTITY_NAME, ReadResult, do_next_cycle
|
2021-02-14 16:40:30 +00:00
|
|
|
|
2022-02-21 22:56:31 +00:00
|
|
|
ENTITY_ID = f"{COVER_DOMAIN}.{TEST_ENTITY_NAME}".replace(" ", "_")
|
|
|
|
ENTITY_ID2 = f"{ENTITY_ID}_2"
|
2021-05-16 06:40:19 +00:00
|
|
|
|
2021-02-14 16:40:30 +00:00
|
|
|
|
2021-03-31 09:20:14 +00:00
|
|
|
@pytest.mark.parametrize(
|
2021-06-17 10:27:05 +00:00
|
|
|
"do_config",
|
2021-03-31 09:20:14 +00:00
|
|
|
[
|
|
|
|
{
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
CONF_SLAVE: 10,
|
|
|
|
CONF_SCAN_INTERVAL: 20,
|
2021-08-21 13:49:50 +00:00
|
|
|
CONF_LAZY_ERROR: 10,
|
2021-06-17 10:27:05 +00:00
|
|
|
}
|
|
|
|
]
|
2021-03-31 09:20:14 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2021-06-17 10:27:05 +00:00
|
|
|
async def test_config_cover(hass, mock_modbus):
|
|
|
|
"""Run configuration test for cover."""
|
|
|
|
assert COVER_DOMAIN in hass.config.components
|
2021-02-14 16:40:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2021-08-22 17:59:38 +00:00
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_SLAVE: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"register_words,expected",
|
2021-02-14 16:40:30 +00:00
|
|
|
[
|
|
|
|
(
|
|
|
|
[0x00],
|
2021-04-30 20:36:55 +00:00
|
|
|
STATE_CLOSED,
|
2021-02-14 16:40:30 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
[0x80],
|
2021-04-30 20:36:55 +00:00
|
|
|
STATE_CLOSED,
|
2021-02-14 16:40:30 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
[0xFE],
|
2021-04-30 20:36:55 +00:00
|
|
|
STATE_CLOSED,
|
2021-02-14 16:40:30 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
[0xFF],
|
2021-04-30 20:36:55 +00:00
|
|
|
STATE_OPEN,
|
2021-02-14 16:40:30 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
[0x01],
|
2021-04-30 20:36:55 +00:00
|
|
|
STATE_OPEN,
|
2021-02-14 16:40:30 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2021-08-23 21:55:57 +00:00
|
|
|
async def test_coil_cover(hass, expected, mock_do_cycle):
|
2021-02-14 16:40:30 +00:00
|
|
|
"""Run test for given config."""
|
2021-08-22 17:59:38 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == expected
|
2021-02-14 16:40:30 +00:00
|
|
|
|
|
|
|
|
2021-10-11 10:43:05 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_SLAVE: 1,
|
|
|
|
CONF_SCAN_INTERVAL: 10,
|
|
|
|
CONF_LAZY_ERROR: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"register_words,do_exception, start_expect,end_expect",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
[0x00],
|
|
|
|
True,
|
|
|
|
STATE_OPEN,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_lazy_error_cover(hass, start_expect, end_expect, mock_do_cycle):
|
|
|
|
"""Run test for given config."""
|
|
|
|
now = mock_do_cycle
|
|
|
|
assert hass.states.get(ENTITY_ID).state == start_expect
|
|
|
|
now = await do_next_cycle(hass, now, 11)
|
|
|
|
assert hass.states.get(ENTITY_ID).state == start_expect
|
|
|
|
now = await do_next_cycle(hass, now, 11)
|
|
|
|
assert hass.states.get(ENTITY_ID).state == end_expect
|
|
|
|
|
|
|
|
|
2021-02-14 16:40:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
2021-08-22 17:59:38 +00:00
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_SLAVE: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"register_words,expected",
|
2021-02-14 16:40:30 +00:00
|
|
|
[
|
|
|
|
(
|
|
|
|
[0x00],
|
2021-04-30 20:36:55 +00:00
|
|
|
STATE_CLOSED,
|
2021-02-14 16:40:30 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
[0x80],
|
|
|
|
STATE_OPEN,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[0xFE],
|
|
|
|
STATE_OPEN,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[0xFF],
|
|
|
|
STATE_OPEN,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[0x01],
|
|
|
|
STATE_OPEN,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2021-08-23 21:55:57 +00:00
|
|
|
async def test_register_cover(hass, expected, mock_do_cycle):
|
2021-02-14 16:40:30 +00:00
|
|
|
"""Run test for given config."""
|
2021-08-22 17:59:38 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == expected
|
2021-04-19 14:52:08 +00:00
|
|
|
|
|
|
|
|
2021-08-11 10:42:28 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-08-11 10:42:28 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_STATUS_REGISTER_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_service_cover_update(hass, mock_modbus, mock_ha):
|
2021-05-02 08:41:36 +00:00
|
|
|
"""Run test for service homeassistant.update_entity."""
|
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"homeassistant", "update_entity", {"entity_id": ENTITY_ID}, blocking=True
|
2021-05-02 08:41:36 +00:00
|
|
|
)
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_CLOSED
|
2021-08-11 10:42:28 +00:00
|
|
|
mock_modbus.read_holding_registers.return_value = ReadResult([0x01])
|
2021-05-02 08:41:36 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"homeassistant", "update_entity", {"entity_id": ENTITY_ID}, blocking=True
|
2021-05-02 08:41:36 +00:00
|
|
|
)
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_OPEN
|
2021-05-16 06:40:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2021-06-29 05:21:04 +00:00
|
|
|
"mock_test_state",
|
|
|
|
[
|
2021-06-30 12:34:33 +00:00
|
|
|
(State(ENTITY_ID, STATE_CLOSED),),
|
|
|
|
(State(ENTITY_ID, STATE_CLOSING),),
|
|
|
|
(State(ENTITY_ID, STATE_OPENING),),
|
|
|
|
(State(ENTITY_ID, STATE_OPEN),),
|
2021-06-29 05:21:04 +00:00
|
|
|
],
|
|
|
|
indirect=True,
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-29 05:21:04 +00:00
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_STATE_OPEN: 1,
|
|
|
|
CONF_STATE_CLOSED: 0,
|
|
|
|
CONF_STATE_OPENING: 2,
|
|
|
|
CONF_STATE_CLOSING: 3,
|
|
|
|
CONF_STATUS_REGISTER: 1234,
|
|
|
|
CONF_STATUS_REGISTER_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
2021-07-21 05:48:02 +00:00
|
|
|
CONF_SCAN_INTERVAL: 0,
|
2021-06-29 05:21:04 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
],
|
2021-05-16 06:40:19 +00:00
|
|
|
)
|
2021-06-29 05:21:04 +00:00
|
|
|
async def test_restore_state_cover(hass, mock_test_state, mock_modbus):
|
2021-05-16 06:40:19 +00:00
|
|
|
"""Run test for cover restore state."""
|
2021-06-29 05:21:04 +00:00
|
|
|
test_state = mock_test_state[0].state
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == test_state
|
2021-05-23 15:18:35 +00:00
|
|
|
|
|
|
|
|
2021-08-11 10:42:28 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-08-11 10:42:28 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_STATUS_REGISTER_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
CONF_SCAN_INTERVAL: 0,
|
|
|
|
},
|
|
|
|
{
|
2022-02-21 22:56:31 +00:00
|
|
|
CONF_NAME: f"{TEST_ENTITY_NAME} 2",
|
2021-08-11 10:42:28 +00:00
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
2021-08-19 07:37:31 +00:00
|
|
|
CONF_ADDRESS: 1235,
|
2021-08-11 10:42:28 +00:00
|
|
|
CONF_SCAN_INTERVAL: 0,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_service_cover_move(hass, mock_modbus, mock_ha):
|
2021-05-23 15:18:35 +00:00
|
|
|
"""Run test for service homeassistant.update_entity."""
|
|
|
|
|
2021-08-11 10:42:28 +00:00
|
|
|
mock_modbus.read_holding_registers.return_value = ReadResult([0x01])
|
2021-05-23 15:18:35 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"cover", "open_cover", {"entity_id": ENTITY_ID}, blocking=True
|
2021-05-23 15:18:35 +00:00
|
|
|
)
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_OPEN
|
2021-05-23 15:18:35 +00:00
|
|
|
|
2021-08-11 10:42:28 +00:00
|
|
|
mock_modbus.read_holding_registers.return_value = ReadResult([0x00])
|
2021-05-23 15:18:35 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"cover", "close_cover", {"entity_id": ENTITY_ID}, blocking=True
|
2021-05-23 15:18:35 +00:00
|
|
|
)
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_CLOSED
|
2021-05-23 15:18:35 +00:00
|
|
|
|
2021-08-11 10:42:28 +00:00
|
|
|
mock_modbus.reset()
|
|
|
|
mock_modbus.read_holding_registers.side_effect = ModbusException("fail write_")
|
2021-05-23 15:18:35 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"cover", "close_cover", {"entity_id": ENTITY_ID}, blocking=True
|
2021-05-23 15:18:35 +00:00
|
|
|
)
|
2021-08-11 10:42:28 +00:00
|
|
|
assert mock_modbus.read_holding_registers.called
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_UNAVAILABLE
|
2021-05-23 15:18:35 +00:00
|
|
|
|
2021-08-11 10:42:28 +00:00
|
|
|
mock_modbus.read_coils.side_effect = ModbusException("fail write_")
|
2021-05-23 15:18:35 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"cover", "close_cover", {"entity_id": ENTITY_ID2}, blocking=True
|
2021-05-23 15:18:35 +00:00
|
|
|
)
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID2).state == STATE_UNAVAILABLE
|
2022-03-11 17:58:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_no_discovery_info_cover(hass, caplog):
|
|
|
|
"""Test setup without discovery info."""
|
|
|
|
assert COVER_DOMAIN not in hass.config.components
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
COVER_DOMAIN,
|
|
|
|
{COVER_DOMAIN: {"platform": MODBUS_DOMAIN}},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert COVER_DOMAIN in hass.config.components
|