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-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,
|
|
|
|
)
|
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
|
2021-02-14 16:40:30 +00:00
|
|
|
|
2021-06-29 05:21:04 +00:00
|
|
|
from .conftest import ReadResult, base_test, prepare_service_update
|
2021-02-14 16:40:30 +00:00
|
|
|
|
2021-06-30 12:34:33 +00:00
|
|
|
COVER_NAME = "test_cover"
|
|
|
|
ENTITY_ID = f"{COVER_DOMAIN}.{COVER_NAME}"
|
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-06-30 12:34:33 +00:00
|
|
|
CONF_NAME: COVER_NAME,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
2021-06-30 12:34:33 +00:00
|
|
|
CONF_NAME: COVER_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-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(
|
|
|
|
"regs,expected",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
[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
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_coil_cover(hass, regs, expected):
|
|
|
|
"""Run test for given config."""
|
|
|
|
state = await base_test(
|
|
|
|
hass,
|
|
|
|
{
|
2021-06-30 12:34:33 +00:00
|
|
|
CONF_NAME: COVER_NAME,
|
2021-05-28 11:23:44 +00:00
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
|
|
|
CONF_ADDRESS: 1234,
|
2021-02-14 16:40:30 +00:00
|
|
|
CONF_SLAVE: 1,
|
|
|
|
},
|
2021-06-30 12:34:33 +00:00
|
|
|
COVER_NAME,
|
2021-02-14 16:40:30 +00:00
|
|
|
COVER_DOMAIN,
|
|
|
|
CONF_COVERS,
|
|
|
|
None,
|
|
|
|
regs,
|
|
|
|
expected,
|
|
|
|
method_discovery=True,
|
|
|
|
scan_interval=5,
|
|
|
|
)
|
|
|
|
assert state == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"regs,expected",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
[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-04-19 14:52:08 +00:00
|
|
|
async def test_register_cover(hass, regs, expected):
|
2021-02-14 16:40:30 +00:00
|
|
|
"""Run test for given config."""
|
|
|
|
state = await base_test(
|
|
|
|
hass,
|
|
|
|
{
|
2021-06-30 12:34:33 +00:00
|
|
|
CONF_NAME: COVER_NAME,
|
2021-05-28 11:23:44 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
2021-02-14 16:40:30 +00:00
|
|
|
CONF_SLAVE: 1,
|
|
|
|
},
|
2021-06-30 12:34:33 +00:00
|
|
|
COVER_NAME,
|
2021-02-14 16:40:30 +00:00
|
|
|
COVER_DOMAIN,
|
|
|
|
CONF_COVERS,
|
|
|
|
None,
|
|
|
|
regs,
|
|
|
|
expected,
|
|
|
|
method_discovery=True,
|
|
|
|
scan_interval=5,
|
|
|
|
)
|
|
|
|
assert state == expected
|
2021-04-19 14:52:08 +00:00
|
|
|
|
|
|
|
|
2021-05-02 08:41:36 +00:00
|
|
|
async def test_service_cover_update(hass, mock_pymodbus):
|
|
|
|
"""Run test for service homeassistant.update_entity."""
|
|
|
|
|
|
|
|
config = {
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
2021-06-30 12:34:33 +00:00
|
|
|
CONF_NAME: COVER_NAME,
|
2021-05-28 11:23:44 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
2021-05-02 08:41:36 +00:00
|
|
|
CONF_STATUS_REGISTER_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
mock_pymodbus.read_holding_registers.return_value = ReadResult([0x00])
|
|
|
|
await prepare_service_update(
|
|
|
|
hass,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
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-05-02 08:41:36 +00:00
|
|
|
mock_pymodbus.read_holding_registers.return_value = ReadResult([0x01])
|
|
|
|
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-06-30 12:34:33 +00:00
|
|
|
CONF_NAME: COVER_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
|
|
|
|
|
|
|
|
|
|
|
async def test_service_cover_move(hass, mock_pymodbus):
|
|
|
|
"""Run test for service homeassistant.update_entity."""
|
|
|
|
|
2021-06-30 12:34:33 +00:00
|
|
|
ENTITY_ID2 = f"{ENTITY_ID}2"
|
2021-05-23 15:18:35 +00:00
|
|
|
config = {
|
|
|
|
CONF_COVERS: [
|
|
|
|
{
|
2021-06-30 12:34:33 +00:00
|
|
|
CONF_NAME: COVER_NAME,
|
2021-05-28 11:23:44 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
2021-05-23 15:18:35 +00:00
|
|
|
CONF_STATUS_REGISTER_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
2021-07-21 05:48:02 +00:00
|
|
|
CONF_SCAN_INTERVAL: 0,
|
2021-05-23 15:18:35 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-30 12:34:33 +00:00
|
|
|
CONF_NAME: f"{COVER_NAME}2",
|
2021-05-28 11:23:44 +00:00
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
|
|
|
CONF_ADDRESS: 1234,
|
2021-07-21 05:48:02 +00:00
|
|
|
CONF_SCAN_INTERVAL: 0,
|
2021-05-23 15:18:35 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
mock_pymodbus.read_holding_registers.return_value = ReadResult([0x01])
|
|
|
|
await prepare_service_update(
|
|
|
|
hass,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
mock_pymodbus.read_holding_registers.return_value = ReadResult([0x00])
|
|
|
|
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-05-28 11:23:44 +00:00
|
|
|
mock_pymodbus.reset()
|
2021-05-23 15:18:35 +00:00
|
|
|
mock_pymodbus.read_holding_registers.side_effect = ModbusException("fail write_")
|
|
|
|
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-05-28 11:23:44 +00:00
|
|
|
assert mock_pymodbus.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
|
|
|
|
|
|
|
mock_pymodbus.read_coils.side_effect = ModbusException("fail write_")
|
|
|
|
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
|